127f190a3SBjoern A. Zeeb /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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 #include "opt_inet.h" 3762b5b6ecSBjoern A. Zeeb #include "opt_inet6.h" 3862b5b6ecSBjoern A. Zeeb 396c5087a8SJack F Vogel #include <sys/param.h> 406c5087a8SJack F Vogel #include <sys/systm.h> 416c5087a8SJack F Vogel #include <sys/kernel.h> 428ec07310SGleb Smirnoff #include <sys/malloc.h> 438ec07310SGleb Smirnoff #include <sys/mbuf.h> 446c5087a8SJack F Vogel #include <sys/socket.h> 45e57b2d0eSRandall Stewart #include <sys/socketvar.h> 46e57b2d0eSRandall Stewart #include <sys/sockbuf.h> 478452c1b3SSepherosa Ziehau #include <sys/sysctl.h> 486c5087a8SJack F Vogel 496c5087a8SJack F Vogel #include <net/if.h> 5062b5b6ecSBjoern A. Zeeb #include <net/if_var.h> 516c5087a8SJack F Vogel #include <net/ethernet.h> 5269a34e8dSRandall Stewart #include <net/bpf.h> 535fa2656eSBjoern A. Zeeb #include <net/vnet.h> 542c6ff1d6SAndrew Gallatin #include <net/if_dl.h> 552c6ff1d6SAndrew Gallatin #include <net/if_media.h> 563d0d5b21SJustin Hibbits #include <net/if_private.h> 572c6ff1d6SAndrew Gallatin #include <net/if_types.h> 582c6ff1d6SAndrew Gallatin #include <net/infiniband.h> 592c6ff1d6SAndrew Gallatin #include <net/if_lagg.h> 606c5087a8SJack F Vogel 616c5087a8SJack F Vogel #include <netinet/in_systm.h> 626c5087a8SJack F Vogel #include <netinet/in.h> 6362b5b6ecSBjoern A. Zeeb #include <netinet/ip6.h> 646c5087a8SJack F Vogel #include <netinet/ip.h> 6531bfc56eSBjoern A. Zeeb #include <netinet/ip_var.h> 66e57b2d0eSRandall Stewart #include <netinet/in_pcb.h> 67e57b2d0eSRandall Stewart #include <netinet6/in6_pcb.h> 686c5087a8SJack F Vogel #include <netinet/tcp.h> 69d7fb35d1SSean Bruno #include <netinet/tcp_seq.h> 706c5087a8SJack F Vogel #include <netinet/tcp_lro.h> 718452c1b3SSepherosa Ziehau #include <netinet/tcp_var.h> 7269a34e8dSRandall Stewart #include <netinet/tcpip.h> 73e57b2d0eSRandall Stewart #include <netinet/tcp_hpts.h> 74e57b2d0eSRandall Stewart #include <netinet/tcp_log_buf.h> 7531bc602fSRandall Stewart #include <netinet/tcp_fsm.h> 769ca874cfSHans Petter Selasky #include <netinet/udp.h> 7731bfc56eSBjoern A. Zeeb #include <netinet6/ip6_var.h> 7831bfc56eSBjoern A. Zeeb 796c5087a8SJack F Vogel #include <machine/in_cksum.h> 806c5087a8SJack F Vogel 81e936121dSHans Petter Selasky static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures"); 826c5087a8SJack F Vogel 836dd38b87SSepherosa Ziehau static void tcp_lro_rx_done(struct lro_ctrl *lc); 849ca874cfSHans Petter Selasky static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, 859ca874cfSHans Petter Selasky uint32_t csum, bool use_hash); 86*e06cf0fcSMichael Tuexen static void tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le); 879ca874cfSHans Petter Selasky 888452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 898452c1b3SSepherosa Ziehau "TCP LRO"); 908452c1b3SSepherosa Ziehau 914f9c93f1SGleb Smirnoff long tcplro_stacks_wanting_mbufq; 922c6fc36aSGleb Smirnoff int (*tcp_lro_flush_tcphpts)(struct lro_ctrl *lc, struct lro_entry *le); 932c6fc36aSGleb Smirnoff 94e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue; 95e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue; 96e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed; 97e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken; 9869a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf; 9969a34e8dSRandall Stewart counter_u64_t tcp_would_have_but; 10069a34e8dSRandall Stewart counter_u64_t tcp_comp_total; 10169a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total; 102ca1a7e10SRandall Stewart counter_u64_t tcp_bad_csums; 103e57b2d0eSRandall Stewart 1048452c1b3SSepherosa Ziehau static unsigned tcp_lro_entries = TCP_LRO_ENTRIES; 1058452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, 1068452c1b3SSepherosa Ziehau CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0, 1078452c1b3SSepherosa Ziehau "default number of LRO entries"); 10869a34e8dSRandall Stewart 109d7955cc0SRandall Stewart static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH; 110d7955cc0SRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold, 111d7955cc0SRandall Stewart CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0, 11208c7f1b6SNavdeep Parhar "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?"); 113d7955cc0SRandall Stewart 1144e0ce82bSRandall Stewart static uint32_t tcp_less_accurate_lro_ts = 0; 1154e0ce82bSRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate, 1164e0ce82bSRandall Stewart CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0, 1174e0ce82bSRandall Stewart "Do we trade off efficency by doing less timestamp operations for time accuracy?"); 1184e0ce82bSRandall Stewart 119e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD, 120e57b2d0eSRandall Stewart &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport"); 121e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD, 122e57b2d0eSRandall Stewart &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts"); 123e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD, 124e57b2d0eSRandall Stewart &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport"); 125e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD, 126e57b2d0eSRandall Stewart &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken"); 12769a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD, 12869a34e8dSRandall Stewart &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp"); 12969a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD, 1309ca874cfSHans Petter Selasky &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed"); 13169a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD, 13269a34e8dSRandall Stewart &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set"); 13369a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD, 13469a34e8dSRandall Stewart &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP"); 135ca1a7e10SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD, 136ca1a7e10SRandall Stewart &tcp_bad_csums, "Number of packets that the common code saw with bad csums"); 137e57b2d0eSRandall Stewart 138e57b2d0eSRandall Stewart void 139e57b2d0eSRandall Stewart tcp_lro_reg_mbufq(void) 140e57b2d0eSRandall Stewart { 141e57b2d0eSRandall Stewart atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1); 142e57b2d0eSRandall Stewart } 143e57b2d0eSRandall Stewart 144e57b2d0eSRandall Stewart void 145e57b2d0eSRandall Stewart tcp_lro_dereg_mbufq(void) 146e57b2d0eSRandall Stewart { 147e57b2d0eSRandall Stewart atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1); 148e57b2d0eSRandall Stewart } 1498452c1b3SSepherosa Ziehau 15051e3c20dSSepherosa Ziehau static __inline void 15105cde7efSSepherosa Ziehau tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket, 15205cde7efSSepherosa Ziehau struct lro_entry *le) 15351e3c20dSSepherosa Ziehau { 15451e3c20dSSepherosa Ziehau 15551e3c20dSSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_active, le, next); 15605cde7efSSepherosa Ziehau LIST_INSERT_HEAD(bucket, le, hash_next); 15751e3c20dSSepherosa Ziehau } 15851e3c20dSSepherosa Ziehau 15951e3c20dSSepherosa Ziehau static __inline void 16051e3c20dSSepherosa Ziehau tcp_lro_active_remove(struct lro_entry *le) 16151e3c20dSSepherosa Ziehau { 16251e3c20dSSepherosa Ziehau 16305cde7efSSepherosa Ziehau LIST_REMOVE(le, next); /* active list */ 16405cde7efSSepherosa Ziehau LIST_REMOVE(le, hash_next); /* hash bucket */ 16551e3c20dSSepherosa Ziehau } 16651e3c20dSSepherosa Ziehau 1676c5087a8SJack F Vogel int 16862b5b6ecSBjoern A. Zeeb tcp_lro_init(struct lro_ctrl *lc) 1696c5087a8SJack F Vogel { 1708452c1b3SSepherosa Ziehau return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0)); 171e936121dSHans Petter Selasky } 172e936121dSHans Petter Selasky 173e936121dSHans Petter Selasky int 174e936121dSHans Petter Selasky tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, 175e936121dSHans Petter Selasky unsigned lro_entries, unsigned lro_mbufs) 176e936121dSHans Petter Selasky { 17762b5b6ecSBjoern A. Zeeb struct lro_entry *le; 178e936121dSHans Petter Selasky size_t size; 179aa6c490bSMichael Tuexen unsigned i; 1806c5087a8SJack F Vogel 18162b5b6ecSBjoern A. Zeeb lc->lro_bad_csum = 0; 18262b5b6ecSBjoern A. Zeeb lc->lro_queued = 0; 18362b5b6ecSBjoern A. Zeeb lc->lro_flushed = 0; 184e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 185e936121dSHans Petter Selasky lc->lro_mbuf_max = lro_mbufs; 186e936121dSHans Petter Selasky lc->lro_cnt = lro_entries; 1877ae3d4bfSSepherosa Ziehau lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX; 1887ae3d4bfSSepherosa Ziehau lc->lro_length_lim = TCP_LRO_LENGTH_MAX; 189e936121dSHans Petter Selasky lc->ifp = ifp; 1901ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_free); 1911ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_active); 1926c5087a8SJack F Vogel 19305cde7efSSepherosa Ziehau /* create hash table to accelerate entry lookup */ 194aa6c490bSMichael Tuexen lc->lro_hash = phashinit_flags(lro_entries, M_LRO, &lc->lro_hashsz, 19505cde7efSSepherosa Ziehau HASH_NOWAIT); 19605cde7efSSepherosa Ziehau if (lc->lro_hash == NULL) { 19705cde7efSSepherosa Ziehau memset(lc, 0, sizeof(*lc)); 19805cde7efSSepherosa Ziehau return (ENOMEM); 19905cde7efSSepherosa Ziehau } 20005cde7efSSepherosa Ziehau 201e936121dSHans Petter Selasky /* compute size to allocate */ 202fc271df3SHans Petter Selasky size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) + 203e936121dSHans Petter Selasky (lro_entries * sizeof(*le)); 204fc271df3SHans Petter Selasky lc->lro_mbuf_data = (struct lro_mbuf_sort *) 205e936121dSHans Petter Selasky malloc(size, M_LRO, M_NOWAIT | M_ZERO); 2066c5087a8SJack F Vogel 207e936121dSHans Petter Selasky /* check for out of memory */ 208e936121dSHans Petter Selasky if (lc->lro_mbuf_data == NULL) { 209a3927369SNavdeep Parhar free(lc->lro_hash, M_LRO); 210e936121dSHans Petter Selasky memset(lc, 0, sizeof(*lc)); 211e936121dSHans Petter Selasky return (ENOMEM); 212e936121dSHans Petter Selasky } 213e936121dSHans Petter Selasky /* compute offset for LRO entries */ 214e936121dSHans Petter Selasky le = (struct lro_entry *) 215e936121dSHans Petter Selasky (lc->lro_mbuf_data + lro_mbufs); 216e936121dSHans Petter Selasky 217e936121dSHans Petter Selasky /* setup linked list */ 218e936121dSHans Petter Selasky for (i = 0; i != lro_entries; i++) 2191ea44822SSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_free, le + i, next); 220e936121dSHans Petter Selasky 221e936121dSHans Petter Selasky return (0); 2226c5087a8SJack F Vogel } 2236c5087a8SJack F Vogel 2249ca874cfSHans Petter Selasky struct vxlan_header { 2259ca874cfSHans Petter Selasky uint32_t vxlh_flags; 2269ca874cfSHans Petter Selasky uint32_t vxlh_vni; 2279ca874cfSHans Petter Selasky }; 228e57b2d0eSRandall Stewart 2299ca874cfSHans Petter Selasky static inline void * 2301d171e5aSRandall Stewart tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen) 2319ca874cfSHans Petter Selasky { 2329ca874cfSHans Petter Selasky const struct ether_vlan_header *eh; 2339ca874cfSHans Petter Selasky void *old; 2349ca874cfSHans Petter Selasky uint16_t eth_type; 2359ca874cfSHans Petter Selasky 2369ca874cfSHans Petter Selasky if (update_data) 2379ca874cfSHans Petter Selasky memset(parser, 0, sizeof(*parser)); 2389ca874cfSHans Petter Selasky 2399ca874cfSHans Petter Selasky old = ptr; 2409ca874cfSHans Petter Selasky 2419ca874cfSHans Petter Selasky if (is_vxlan) { 2429ca874cfSHans Petter Selasky const struct vxlan_header *vxh; 2439ca874cfSHans Petter Selasky vxh = ptr; 2449ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*vxh); 2459ca874cfSHans Petter Selasky if (update_data) { 2469ca874cfSHans Petter Selasky parser->data.vxlan_vni = 2479ca874cfSHans Petter Selasky vxh->vxlh_vni & htonl(0xffffff00); 248e57b2d0eSRandall Stewart } 2499ca874cfSHans Petter Selasky } 2509ca874cfSHans Petter Selasky 2519ca874cfSHans Petter Selasky eh = ptr; 2529ca874cfSHans Petter Selasky if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) { 2539ca874cfSHans Petter Selasky eth_type = eh->evl_proto; 2549ca874cfSHans Petter Selasky if (update_data) { 2559ca874cfSHans Petter Selasky /* strip priority and keep VLAN ID only */ 2569ca874cfSHans Petter Selasky parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK); 2579ca874cfSHans Petter Selasky } 2589ca874cfSHans Petter Selasky /* advance to next header */ 2599ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 2601d171e5aSRandall Stewart mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 2619ca874cfSHans Petter Selasky } else { 2629ca874cfSHans Petter Selasky eth_type = eh->evl_encap_proto; 2639ca874cfSHans Petter Selasky /* advance to next header */ 2641d171e5aSRandall Stewart mlen -= ETHER_HDR_LEN; 2659ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + ETHER_HDR_LEN; 2669ca874cfSHans Petter Selasky } 2671d171e5aSRandall Stewart if (__predict_false(mlen <= 0)) 2681d171e5aSRandall Stewart return (NULL); 2699ca874cfSHans Petter Selasky switch (eth_type) { 2709ca874cfSHans Petter Selasky #ifdef INET 2719ca874cfSHans Petter Selasky case htons(ETHERTYPE_IP): 2729ca874cfSHans Petter Selasky parser->ip4 = ptr; 2731d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip))) 2741d171e5aSRandall Stewart return (NULL); 2759ca874cfSHans Petter Selasky /* Ensure there are no IPv4 options. */ 2769ca874cfSHans Petter Selasky if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4)) 2779ca874cfSHans Petter Selasky break; 2789ca874cfSHans Petter Selasky /* .. and the packet is not fragmented. */ 2799ca874cfSHans Petter Selasky if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK)) 2809ca874cfSHans Petter Selasky break; 281abba5876SAndrew Gallatin /* .. and the packet has valid src/dst addrs */ 282abba5876SAndrew Gallatin if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY || 283abba5876SAndrew Gallatin parser->ip4->ip_dst.s_addr == INADDR_ANY)) 284abba5876SAndrew Gallatin break; 2859ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2); 2861d171e5aSRandall Stewart mlen -= sizeof(struct ip); 2879ca874cfSHans Petter Selasky if (update_data) { 2889ca874cfSHans Petter Selasky parser->data.s_addr.v4 = parser->ip4->ip_src; 2899ca874cfSHans Petter Selasky parser->data.d_addr.v4 = parser->ip4->ip_dst; 2909ca874cfSHans Petter Selasky } 2919ca874cfSHans Petter Selasky switch (parser->ip4->ip_p) { 2929ca874cfSHans Petter Selasky case IPPROTO_UDP: 2931d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 2941d171e5aSRandall Stewart return (NULL); 2959ca874cfSHans Petter Selasky parser->udp = ptr; 2969ca874cfSHans Petter Selasky if (update_data) { 2979ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_UDP; 2989ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 2999ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 3009ca874cfSHans Petter Selasky } else { 3019ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP); 3029ca874cfSHans Petter Selasky } 3039ca874cfSHans Petter Selasky ptr = ((uint8_t *)ptr + sizeof(*parser->udp)); 3049ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3059ca874cfSHans Petter Selasky return (ptr); 3069ca874cfSHans Petter Selasky case IPPROTO_TCP: 3079ca874cfSHans Petter Selasky parser->tcp = ptr; 3081d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 3091d171e5aSRandall Stewart return (NULL); 3109ca874cfSHans Petter Selasky if (update_data) { 3119ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_TCP; 3129ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3139ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3149ca874cfSHans Petter Selasky } else { 3159ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP); 3169ca874cfSHans Petter Selasky } 3171d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 3181d171e5aSRandall Stewart return (NULL); 3199ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3209ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3219ca874cfSHans Petter Selasky return (ptr); 3229ca874cfSHans Petter Selasky default: 3239ca874cfSHans Petter Selasky break; 3249ca874cfSHans Petter Selasky } 3259ca874cfSHans Petter Selasky break; 3269ca874cfSHans Petter Selasky #endif 3279ca874cfSHans Petter Selasky #ifdef INET6 3289ca874cfSHans Petter Selasky case htons(ETHERTYPE_IPV6): 3299ca874cfSHans Petter Selasky parser->ip6 = ptr; 3301d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip6_hdr))) 3311d171e5aSRandall Stewart return (NULL); 332abba5876SAndrew Gallatin /* Ensure the packet has valid src/dst addrs */ 333abba5876SAndrew Gallatin if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) || 334abba5876SAndrew Gallatin IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst))) 335abba5876SAndrew Gallatin return (NULL); 3369ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->ip6); 3379ca874cfSHans Petter Selasky if (update_data) { 3389ca874cfSHans Petter Selasky parser->data.s_addr.v6 = parser->ip6->ip6_src; 3399ca874cfSHans Petter Selasky parser->data.d_addr.v6 = parser->ip6->ip6_dst; 3409ca874cfSHans Petter Selasky } 3411d171e5aSRandall Stewart mlen -= sizeof(struct ip6_hdr); 3429ca874cfSHans Petter Selasky switch (parser->ip6->ip6_nxt) { 3439ca874cfSHans Petter Selasky case IPPROTO_UDP: 3441d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 3451d171e5aSRandall Stewart return (NULL); 3469ca874cfSHans Petter Selasky parser->udp = ptr; 3479ca874cfSHans Petter Selasky if (update_data) { 3489ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_UDP; 3499ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 3509ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 3519ca874cfSHans Petter Selasky } else { 3529ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP); 3539ca874cfSHans Petter Selasky } 3549ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->udp); 3559ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3569ca874cfSHans Petter Selasky return (ptr); 3579ca874cfSHans Petter Selasky case IPPROTO_TCP: 3581d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 3591d171e5aSRandall Stewart return (NULL); 3609ca874cfSHans Petter Selasky parser->tcp = ptr; 3619ca874cfSHans Petter Selasky if (update_data) { 3629ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_TCP; 3639ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3649ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3659ca874cfSHans Petter Selasky } else { 3669ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP); 3679ca874cfSHans Petter Selasky } 3681d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 3691d171e5aSRandall Stewart return (NULL); 3709ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3719ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3729ca874cfSHans Petter Selasky return (ptr); 3739ca874cfSHans Petter Selasky default: 3749ca874cfSHans Petter Selasky break; 3759ca874cfSHans Petter Selasky } 3769ca874cfSHans Petter Selasky break; 3779ca874cfSHans Petter Selasky #endif 3789ca874cfSHans Petter Selasky default: 3799ca874cfSHans Petter Selasky break; 3809ca874cfSHans Petter Selasky } 3819ca874cfSHans Petter Selasky /* Invalid packet - cannot parse */ 3829ca874cfSHans Petter Selasky return (NULL); 3839ca874cfSHans Petter Selasky } 3849ca874cfSHans Petter Selasky 3859ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID | 3869ca874cfSHans Petter Selasky CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID; 3879ca874cfSHans Petter Selasky 3889ca874cfSHans Petter Selasky static inline struct lro_parser * 3899ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data) 3909ca874cfSHans Petter Selasky { 3919ca874cfSHans Petter Selasky void *data_ptr; 3929ca874cfSHans Petter Selasky 3939ca874cfSHans Petter Selasky /* Try to parse outer headers first. */ 3941d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len); 3959ca874cfSHans Petter Selasky if (data_ptr == NULL || po->total_hdr_len > m->m_len) 3969ca874cfSHans Petter Selasky return (NULL); 3979ca874cfSHans Petter Selasky 3989ca874cfSHans Petter Selasky if (update_data) { 3999ca874cfSHans Petter Selasky /* Store VLAN ID, if any. */ 4009ca874cfSHans Petter Selasky if (__predict_false(m->m_flags & M_VLANTAG)) { 4019ca874cfSHans Petter Selasky po->data.vlan_id = 4029ca874cfSHans Petter Selasky htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK); 4039ca874cfSHans Petter Selasky } 404bb5cd80eSHans Petter Selasky /* Store decrypted flag, if any. */ 40510a62eb1SHans Petter Selasky if (__predict_false((m->m_pkthdr.csum_flags & 40610a62eb1SHans Petter Selasky CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED)) 407bb5cd80eSHans Petter Selasky po->data.lro_flags |= LRO_FLAG_DECRYPTED; 4089ca874cfSHans Petter Selasky } 4099ca874cfSHans Petter Selasky 4109ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4119ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 4129ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 4139ca874cfSHans Petter Selasky /* Check for VXLAN headers. */ 4149ca874cfSHans Petter Selasky if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum) 4159ca874cfSHans Petter Selasky break; 4169ca874cfSHans Petter Selasky 4179ca874cfSHans Petter Selasky /* Try to parse inner headers. */ 4181d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true, 4191d171e5aSRandall Stewart (m->m_len - ((caddr_t)data_ptr - m->m_data))); 4201d171e5aSRandall Stewart if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len) 4219ca874cfSHans Petter Selasky break; 4229ca874cfSHans Petter Selasky 4239ca874cfSHans Petter Selasky /* Verify supported header types. */ 4249ca874cfSHans Petter Selasky switch (pi->data.lro_type) { 4259ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4269ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4279ca874cfSHans Petter Selasky return (pi); 4289ca874cfSHans Petter Selasky default: 4299ca874cfSHans Petter Selasky break; 4309ca874cfSHans Petter Selasky } 4319ca874cfSHans Petter Selasky break; 4329ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4339ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4349ca874cfSHans Petter Selasky if (update_data) 4359ca874cfSHans Petter Selasky memset(pi, 0, sizeof(*pi)); 4369ca874cfSHans Petter Selasky return (po); 4379ca874cfSHans Petter Selasky default: 4389ca874cfSHans Petter Selasky break; 4399ca874cfSHans Petter Selasky } 4409ca874cfSHans Petter Selasky return (NULL); 4419ca874cfSHans Petter Selasky } 4429ca874cfSHans Petter Selasky 4439ca874cfSHans Petter Selasky static inline int 4449ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po) 4459ca874cfSHans Petter Selasky { 4469ca874cfSHans Petter Selasky int len; 4479ca874cfSHans Petter Selasky 4489ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4499ca874cfSHans Petter Selasky #ifdef INET 4509ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4519ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) + 4529ca874cfSHans Petter Selasky ntohs(po->ip4->ip_len); 4539ca874cfSHans Petter Selasky break; 4549ca874cfSHans Petter Selasky #endif 4559ca874cfSHans Petter Selasky #ifdef INET6 4569ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4579ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) + 4589ca874cfSHans Petter Selasky ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6); 4599ca874cfSHans Petter Selasky break; 4609ca874cfSHans Petter Selasky #endif 4619ca874cfSHans Petter Selasky default: 4629ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4639ca874cfSHans Petter Selasky } 4649ca874cfSHans Petter Selasky 4659ca874cfSHans Petter Selasky /* 4669ca874cfSHans Petter Selasky * If the frame is padded beyond the end of the IP packet, 4679ca874cfSHans Petter Selasky * then trim the extra bytes off: 4689ca874cfSHans Petter Selasky */ 4699ca874cfSHans Petter Selasky if (__predict_true(m->m_pkthdr.len == len)) { 4709ca874cfSHans Petter Selasky return (0); 4719ca874cfSHans Petter Selasky } else if (m->m_pkthdr.len > len) { 4729ca874cfSHans Petter Selasky m_adj(m, len - m->m_pkthdr.len); 4739ca874cfSHans Petter Selasky return (0); 4749ca874cfSHans Petter Selasky } 4759ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4769ca874cfSHans Petter Selasky } 4779ca874cfSHans Petter Selasky 47869a34e8dSRandall Stewart static void 47969a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m) 48069a34e8dSRandall Stewart { 48169a34e8dSRandall Stewart struct mbuf *save; 48269a34e8dSRandall Stewart 48369a34e8dSRandall Stewart while (m) { 48469a34e8dSRandall Stewart save = m->m_nextpkt; 48569a34e8dSRandall Stewart m->m_nextpkt = NULL; 48669a34e8dSRandall Stewart m_freem(m); 48769a34e8dSRandall Stewart m = save; 48869a34e8dSRandall Stewart } 48969a34e8dSRandall Stewart } 49069a34e8dSRandall Stewart 4916c5087a8SJack F Vogel void 49262b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc) 4936c5087a8SJack F Vogel { 49462b5b6ecSBjoern A. Zeeb struct lro_entry *le; 495e936121dSHans Petter Selasky unsigned x; 4966c5087a8SJack F Vogel 497e936121dSHans Petter Selasky /* reset LRO free list */ 4981ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_free); 499e936121dSHans Petter Selasky 500e936121dSHans Petter Selasky /* free active mbufs, if any */ 5011ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 50251e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 50369a34e8dSRandall Stewart lro_free_mbuf_chain(le->m_head); 5046c5087a8SJack F Vogel } 505e936121dSHans Petter Selasky 50605cde7efSSepherosa Ziehau /* free hash table */ 50705cde7efSSepherosa Ziehau free(lc->lro_hash, M_LRO); 50805cde7efSSepherosa Ziehau lc->lro_hash = NULL; 50905cde7efSSepherosa Ziehau lc->lro_hashsz = 0; 51005cde7efSSepherosa Ziehau 511e936121dSHans Petter Selasky /* free mbuf array, if any */ 512e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) 513fc271df3SHans Petter Selasky m_freem(lc->lro_mbuf_data[x].mb); 514e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 515e936121dSHans Petter Selasky 516e936121dSHans Petter Selasky /* free allocated memory, if any */ 517e936121dSHans Petter Selasky free(lc->lro_mbuf_data, M_LRO); 518e936121dSHans Petter Selasky lc->lro_mbuf_data = NULL; 5196c5087a8SJack F Vogel } 5206c5087a8SJack F Vogel 52162b5b6ecSBjoern A. Zeeb static uint16_t 5229ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th) 52362b5b6ecSBjoern A. Zeeb { 5249ca874cfSHans Petter Selasky const uint16_t *ptr; 5259ca874cfSHans Petter Selasky uint32_t csum; 5269ca874cfSHans Petter Selasky uint16_t len; 52762b5b6ecSBjoern A. Zeeb 5289ca874cfSHans Petter Selasky csum = -th->th_sum; /* exclude checksum field */ 5299ca874cfSHans Petter Selasky len = th->th_off; 5309ca874cfSHans Petter Selasky ptr = (const uint16_t *)th; 5319ca874cfSHans Petter Selasky while (len--) { 5329ca874cfSHans Petter Selasky csum += *ptr; 5339ca874cfSHans Petter Selasky ptr++; 5349ca874cfSHans Petter Selasky csum += *ptr; 5359ca874cfSHans Petter Selasky ptr++; 53662b5b6ecSBjoern A. Zeeb } 5379ca874cfSHans Petter Selasky while (csum > 0xffff) 5389ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 53962b5b6ecSBjoern A. Zeeb 5409ca874cfSHans Petter Selasky return (csum); 54162b5b6ecSBjoern A. Zeeb } 54262b5b6ecSBjoern A. Zeeb 54362b5b6ecSBjoern A. Zeeb static uint16_t 5449ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum) 54562b5b6ecSBjoern A. Zeeb { 54662b5b6ecSBjoern A. Zeeb uint32_t c; 54762b5b6ecSBjoern A. Zeeb uint16_t cs; 54862b5b6ecSBjoern A. Zeeb 5499ca874cfSHans Petter Selasky c = tcp_csum; 55062b5b6ecSBjoern A. Zeeb 5519ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 55262b5b6ecSBjoern A. Zeeb #ifdef INET6 5539ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 5549ca874cfSHans Petter Selasky /* Compute full pseudo IPv6 header checksum. */ 5559ca874cfSHans Petter Selasky cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0); 55662b5b6ecSBjoern A. Zeeb break; 55762b5b6ecSBjoern A. Zeeb #endif 55862b5b6ecSBjoern A. Zeeb #ifdef INET 5599ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 5609ca874cfSHans Petter Selasky /* Compute full pseudo IPv4 header checsum. */ 5619ca874cfSHans Petter Selasky cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP); 5629ca874cfSHans Petter Selasky cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs)); 56362b5b6ecSBjoern A. Zeeb break; 56462b5b6ecSBjoern A. Zeeb #endif 56562b5b6ecSBjoern A. Zeeb default: 56662b5b6ecSBjoern A. Zeeb cs = 0; /* Keep compiler happy. */ 5679ca874cfSHans Petter Selasky break; 56862b5b6ecSBjoern A. Zeeb } 56962b5b6ecSBjoern A. Zeeb 5709ca874cfSHans Petter Selasky /* Complement checksum. */ 57162b5b6ecSBjoern A. Zeeb cs = ~cs; 57262b5b6ecSBjoern A. Zeeb c += cs; 57362b5b6ecSBjoern A. Zeeb 5749ca874cfSHans Petter Selasky /* Remove TCP header checksum. */ 5759ca874cfSHans Petter Selasky cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp); 57662b5b6ecSBjoern A. Zeeb c += cs; 5779ca874cfSHans Petter Selasky 5789ca874cfSHans Petter Selasky /* Compute checksum remainder. */ 57962b5b6ecSBjoern A. Zeeb while (c > 0xffff) 58062b5b6ecSBjoern A. Zeeb c = (c >> 16) + (c & 0xffff); 58162b5b6ecSBjoern A. Zeeb 5829ca874cfSHans Petter Selasky return (c); 58362b5b6ecSBjoern A. Zeeb } 58462b5b6ecSBjoern A. Zeeb 5856dd38b87SSepherosa Ziehau static void 5866dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc) 5876dd38b87SSepherosa Ziehau { 5886dd38b87SSepherosa Ziehau struct lro_entry *le; 5896dd38b87SSepherosa Ziehau 5901ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 59151e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 5926dd38b87SSepherosa Ziehau tcp_lro_flush(lc, le); 5936dd38b87SSepherosa Ziehau } 5946dd38b87SSepherosa Ziehau } 5956dd38b87SSepherosa Ziehau 5964e0ce82bSRandall Stewart static void 5974e0ce82bSRandall Stewart tcp_lro_flush_active(struct lro_ctrl *lc) 5984e0ce82bSRandall Stewart { 59964443828SMichael Tuexen struct lro_entry *le, *le_tmp; 6004e0ce82bSRandall Stewart 6014e0ce82bSRandall Stewart /* 6024e0ce82bSRandall Stewart * Walk through the list of le entries, and 6034e0ce82bSRandall Stewart * any one that does have packets flush. This 6044e0ce82bSRandall Stewart * is called because we have an inbound packet 6054e0ce82bSRandall Stewart * (e.g. SYN) that has to have all others flushed 6064e0ce82bSRandall Stewart * in front of it. Note we have to do the remove 6074e0ce82bSRandall Stewart * because tcp_lro_flush() assumes that the entry 6084e0ce82bSRandall Stewart * is being freed. This is ok it will just get 6094e0ce82bSRandall Stewart * reallocated again like it was new. 6104e0ce82bSRandall Stewart */ 61164443828SMichael Tuexen LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 6124e0ce82bSRandall Stewart if (le->m_head != NULL) { 6134e0ce82bSRandall Stewart tcp_lro_active_remove(le); 6144e0ce82bSRandall Stewart tcp_lro_flush(lc, le); 6154e0ce82bSRandall Stewart } 6164e0ce82bSRandall Stewart } 6174e0ce82bSRandall Stewart } 6184e0ce82bSRandall Stewart 6196c5087a8SJack F Vogel void 6207127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 6217127e6acSNavdeep Parhar { 6227127e6acSNavdeep Parhar struct lro_entry *le, *le_tmp; 623b45daaeaSRandall Stewart uint64_t now, tov; 624b45daaeaSRandall Stewart struct bintime bt; 6257127e6acSNavdeep Parhar 626dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 6271ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_active)) 6287127e6acSNavdeep Parhar return; 6297127e6acSNavdeep Parhar 630b45daaeaSRandall Stewart /* get timeout time and current time in ns */ 631b45daaeaSRandall Stewart binuptime(&bt); 632b45daaeaSRandall Stewart now = bintime2ns(&bt); 633b45daaeaSRandall Stewart tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000)); 6341ea44822SSepherosa Ziehau LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 635b45daaeaSRandall Stewart if (now >= (bintime2ns(&le->alloc_time) + tov)) { 63651e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 6377127e6acSNavdeep Parhar tcp_lro_flush(lc, le); 6387127e6acSNavdeep Parhar } 6397127e6acSNavdeep Parhar } 6407127e6acSNavdeep Parhar } 6417127e6acSNavdeep Parhar 642e57b2d0eSRandall Stewart #ifdef INET 643e57b2d0eSRandall Stewart static int 6449ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4) 645e57b2d0eSRandall Stewart { 646e57b2d0eSRandall Stewart uint16_t csum; 647e57b2d0eSRandall Stewart 648e57b2d0eSRandall Stewart /* Legacy IP has a header checksum that needs to be correct. */ 6499ca874cfSHans Petter Selasky if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 6509ca874cfSHans Petter Selasky if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) { 651e57b2d0eSRandall Stewart lc->lro_bad_csum++; 652e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 653e57b2d0eSRandall Stewart } 654e57b2d0eSRandall Stewart } else { 655e57b2d0eSRandall Stewart csum = in_cksum_hdr(ip4); 6569ca874cfSHans Petter Selasky if (__predict_false(csum != 0)) { 657e57b2d0eSRandall Stewart lc->lro_bad_csum++; 658e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 659e57b2d0eSRandall Stewart } 660e57b2d0eSRandall Stewart } 661e57b2d0eSRandall Stewart return (0); 662e57b2d0eSRandall Stewart } 663e57b2d0eSRandall Stewart #endif 664e57b2d0eSRandall Stewart 6659ca874cfSHans Petter Selasky static inline void 6669ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum) 667e57b2d0eSRandall Stewart { 6689ca874cfSHans Petter Selasky uint32_t csum; 6696c5087a8SJack F Vogel 6709ca874cfSHans Petter Selasky csum = 0xffff - *ptr + value; 6719ca874cfSHans Petter Selasky while (csum > 0xffff) 6729ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 6739ca874cfSHans Petter Selasky *ptr = value; 6749ca874cfSHans Petter Selasky *psum = csum; 67562b5b6ecSBjoern A. Zeeb } 67662b5b6ecSBjoern A. Zeeb 6779ca874cfSHans Petter Selasky static uint16_t 6789ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le, 6799ca874cfSHans Petter Selasky uint16_t payload_len, uint16_t delta_sum) 6809ca874cfSHans Petter Selasky { 6819ca874cfSHans Petter Selasky uint32_t csum; 6829ca874cfSHans Petter Selasky uint16_t tlen; 6839ca874cfSHans Petter Selasky uint16_t temp[5] = {}; 6849ca874cfSHans Petter Selasky 6859ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 6869ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 6879ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 6889ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len; 6899ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 6909ca874cfSHans Petter Selasky 6919ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 6929ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 6939ca874cfSHans Petter Selasky while (csum > 0xffff) 6949ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 6959ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 6969ca874cfSHans Petter Selasky goto update_tcp_header; 6979ca874cfSHans Petter Selasky 6989ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 6999ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7009ca874cfSHans Petter Selasky tlen = (pa->tcp->th_off << 2) + payload_len; 7019ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7029ca874cfSHans Petter Selasky goto update_tcp_header; 7039ca874cfSHans Petter Selasky 7049ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 7059ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 7069ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len; 7079ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 7089ca874cfSHans Petter Selasky 7099ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 7109ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 7119ca874cfSHans Petter Selasky while (csum > 0xffff) 7129ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7139ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 7149ca874cfSHans Petter Selasky goto update_udp_header; 7159ca874cfSHans Petter Selasky 7169ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 7179ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7189ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7199ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7209ca874cfSHans Petter Selasky goto update_udp_header; 7219ca874cfSHans Petter Selasky 72262b5b6ecSBjoern A. Zeeb default: 7239ca874cfSHans Petter Selasky return (0); 72462b5b6ecSBjoern A. Zeeb } 7259ca874cfSHans Petter Selasky 7269ca874cfSHans Petter Selasky update_tcp_header: 7279ca874cfSHans Petter Selasky /* Compute current TCP header checksum. */ 7289ca874cfSHans Petter Selasky temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp); 72962b5b6ecSBjoern A. Zeeb 73062b5b6ecSBjoern A. Zeeb /* Incorporate the latest ACK into the TCP header. */ 7319ca874cfSHans Petter Selasky pa->tcp->th_ack = le->ack_seq; 7329ca874cfSHans Petter Selasky pa->tcp->th_win = le->window; 7339ca874cfSHans Petter Selasky 73462b5b6ecSBjoern A. Zeeb /* Incorporate latest timestamp into the TCP header. */ 73562b5b6ecSBjoern A. Zeeb if (le->timestamp != 0) { 7366c5087a8SJack F Vogel uint32_t *ts_ptr; 7376c5087a8SJack F Vogel 7389ca874cfSHans Petter Selasky ts_ptr = (uint32_t *)(pa->tcp + 1); 73962b5b6ecSBjoern A. Zeeb ts_ptr[1] = htonl(le->tsval); 74062b5b6ecSBjoern A. Zeeb ts_ptr[2] = le->tsecr; 74162b5b6ecSBjoern A. Zeeb } 7429ca874cfSHans Petter Selasky 7439ca874cfSHans Petter Selasky /* Compute new TCP header checksum. */ 7449ca874cfSHans Petter Selasky temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp); 7459ca874cfSHans Petter Selasky 7469ca874cfSHans Petter Selasky /* Compute new TCP checksum. */ 7479ca874cfSHans Petter Selasky csum = pa->tcp->th_sum + 0xffff - delta_sum + 7489ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[3] + temp[2]; 7499ca874cfSHans Petter Selasky while (csum > 0xffff) 7509ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7519ca874cfSHans Petter Selasky 7529ca874cfSHans Petter Selasky /* Assign new TCP checksum. */ 7539ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]); 7549ca874cfSHans Petter Selasky 7559ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 7569ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + 0xffff - temp[2] + 7579ca874cfSHans Petter Selasky temp[3] + temp[4] + delta_sum; 7589ca874cfSHans Petter Selasky while (csum > 0xffff) 7599ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7609ca874cfSHans Petter Selasky 7619ca874cfSHans Petter Selasky /* Return delta checksum to next stage, if any. */ 7629ca874cfSHans Petter Selasky return (csum); 7639ca874cfSHans Petter Selasky 7649ca874cfSHans Petter Selasky update_udp_header: 7659ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7669ca874cfSHans Petter Selasky /* Assign new UDP length and compute checksum delta. */ 7679ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]); 7689ca874cfSHans Petter Selasky 7699ca874cfSHans Petter Selasky /* Check if there is a UDP checksum. */ 7709ca874cfSHans Petter Selasky if (__predict_false(pa->udp->uh_sum != 0)) { 7719ca874cfSHans Petter Selasky /* Compute new UDP checksum. */ 7729ca874cfSHans Petter Selasky csum = pa->udp->uh_sum + 0xffff - delta_sum + 7739ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[2]; 7749ca874cfSHans Petter Selasky while (csum > 0xffff) 7759ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7769ca874cfSHans Petter Selasky /* Assign new UDP checksum. */ 7779ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]); 778e57b2d0eSRandall Stewart } 7799ca874cfSHans Petter Selasky 7809ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 7819ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum; 7829ca874cfSHans Petter Selasky while (csum > 0xffff) 7839ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7849ca874cfSHans Petter Selasky 7859ca874cfSHans Petter Selasky /* Return delta checksum to next stage, if any. */ 7869ca874cfSHans Petter Selasky return (csum); 7879ca874cfSHans Petter Selasky } 7889ca874cfSHans Petter Selasky 7899ca874cfSHans Petter Selasky static void 7909ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le) 7919ca874cfSHans Petter Selasky { 7929ca874cfSHans Petter Selasky /* Check if we need to recompute any checksums. */ 79324fe6643SRyan Stone if (le->needs_merge) { 7949ca874cfSHans Petter Selasky uint16_t csum; 7959ca874cfSHans Petter Selasky 7969ca874cfSHans Petter Selasky switch (le->inner.data.lro_type) { 7979ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 7989ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 7999ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8009ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8019ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8029ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8039ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8049ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8059ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 8069ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 80710a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 80810a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8099ca874cfSHans Petter Selasky break; 8109ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8119ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 8129ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8139ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8149ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8159ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8169ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8179ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8189ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR; 8199ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 82010a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 82110a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8229ca874cfSHans Petter Selasky break; 8239ca874cfSHans Petter Selasky case LRO_TYPE_NONE: 8249ca874cfSHans Petter Selasky switch (le->outer.data.lro_type) { 8259ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 8269ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8279ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8289ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_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; 83210a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 83310a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8349ca874cfSHans Petter Selasky break; 8359ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8369ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8379ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8389ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8399ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8409ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR; 8419ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 84210a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 84310a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8449ca874cfSHans Petter Selasky break; 8459ca874cfSHans Petter Selasky default: 8469ca874cfSHans Petter Selasky break; 8479ca874cfSHans Petter Selasky } 8489ca874cfSHans Petter Selasky break; 8499ca874cfSHans Petter Selasky default: 8509ca874cfSHans Petter Selasky break; 8519ca874cfSHans Petter Selasky } 8529ca874cfSHans Petter Selasky } 8539ca874cfSHans Petter Selasky 854e57b2d0eSRandall Stewart /* 855e57b2d0eSRandall Stewart * Break any chain, this is not set to NULL on the singleton 856e57b2d0eSRandall Stewart * case m_nextpkt points to m_head. Other case set them 857e57b2d0eSRandall Stewart * m_nextpkt to NULL in push_and_replace. 858e57b2d0eSRandall Stewart */ 859e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 8609ca874cfSHans Petter Selasky lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs; 861e57b2d0eSRandall Stewart (*lc->ifp->if_input)(lc->ifp, le->m_head); 86262b5b6ecSBjoern A. Zeeb } 8636c5087a8SJack F Vogel 864e57b2d0eSRandall Stewart static void 8659ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, 8669ca874cfSHans Petter Selasky struct mbuf *m, struct tcphdr *th) 867e57b2d0eSRandall Stewart { 868e57b2d0eSRandall Stewart uint32_t *ts_ptr; 869e57b2d0eSRandall Stewart uint16_t tcp_data_len; 8709ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 871e57b2d0eSRandall Stewart 872e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 8739ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 8749ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 8759ca874cfSHans Petter Selasky 8769ca874cfSHans Petter Selasky /* Check if there is a timestamp option. */ 8779ca874cfSHans Petter Selasky if (tcp_opt_len == 0 || 8789ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 8799ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 8809ca874cfSHans Petter Selasky /* We failed to find the timestamp option. */ 8819ca874cfSHans Petter Selasky le->timestamp = 0; 8829ca874cfSHans Petter Selasky } else { 883e57b2d0eSRandall Stewart le->timestamp = 1; 884e57b2d0eSRandall Stewart le->tsval = ntohl(*(ts_ptr + 1)); 885e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 8869ca874cfSHans Petter Selasky } 8879ca874cfSHans Petter Selasky 8889ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 8899ca874cfSHans Petter Selasky 8909ca874cfSHans Petter Selasky /* Pull out TCP sequence numbers and window size. */ 891e57b2d0eSRandall Stewart le->next_seq = ntohl(th->th_seq) + tcp_data_len; 892e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 893e57b2d0eSRandall Stewart le->window = th->th_win; 8941ebf4607SRichard Scheffenegger le->flags = tcp_get_flags(th); 89524fe6643SRyan Stone le->needs_merge = 0; 8969ca874cfSHans Petter Selasky 8979ca874cfSHans Petter Selasky /* Setup new data pointers. */ 898e57b2d0eSRandall Stewart le->m_head = m; 899e57b2d0eSRandall Stewart le->m_tail = m_last(m); 900e57b2d0eSRandall Stewart } 901e57b2d0eSRandall Stewart 902e57b2d0eSRandall Stewart static void 9039ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m) 904e57b2d0eSRandall Stewart { 9059ca874cfSHans Petter Selasky struct lro_parser *pa; 9069ca874cfSHans Petter Selasky 907e57b2d0eSRandall Stewart /* 9089ca874cfSHans Petter Selasky * Push up the stack of the current entry 9099ca874cfSHans Petter Selasky * and replace it with "m". 910e57b2d0eSRandall Stewart */ 911e57b2d0eSRandall Stewart struct mbuf *msave; 912e57b2d0eSRandall Stewart 913e57b2d0eSRandall Stewart /* Grab off the next and save it */ 914e57b2d0eSRandall Stewart msave = le->m_head->m_nextpkt; 915e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 9169ca874cfSHans Petter Selasky 9179ca874cfSHans Petter Selasky /* Now push out the old entry */ 9189ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 9199ca874cfSHans Petter Selasky 9209ca874cfSHans Petter Selasky /* Re-parse new header, should not fail. */ 9219ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &le->outer, &le->inner, false); 9229ca874cfSHans Petter Selasky KASSERT(pa != NULL, 9239ca874cfSHans Petter Selasky ("tcp_push_and_replace: LRO parser failed on m=%p\n", m)); 9249ca874cfSHans Petter Selasky 925e57b2d0eSRandall Stewart /* 9269ca874cfSHans Petter Selasky * Now to replace the data properly in the entry 9279ca874cfSHans Petter Selasky * we have to reset the TCP header and 928e57b2d0eSRandall Stewart * other fields. 929e57b2d0eSRandall Stewart */ 9309ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, pa->tcp); 9319ca874cfSHans Petter Selasky 932e57b2d0eSRandall Stewart /* Restore the next list */ 933e57b2d0eSRandall Stewart m->m_nextpkt = msave; 934e57b2d0eSRandall Stewart } 935e57b2d0eSRandall Stewart 936e57b2d0eSRandall Stewart static void 93724fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p) 9389ca874cfSHans Petter Selasky { 93924fe6643SRyan Stone struct mbuf *m; 9409ca874cfSHans Petter Selasky uint32_t csum; 9419ca874cfSHans Petter Selasky 94224fe6643SRyan Stone m = le->m_head; 9439ca874cfSHans Petter Selasky if (m->m_pkthdr.lro_nsegs == 1) { 9449ca874cfSHans Petter Selasky /* Compute relative checksum. */ 9459ca874cfSHans Petter Selasky csum = p->m_pkthdr.lro_tcp_d_csum; 9469ca874cfSHans Petter Selasky } else { 9479ca874cfSHans Petter Selasky /* Merge TCP data checksums. */ 9489ca874cfSHans Petter Selasky csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum + 9499ca874cfSHans Petter Selasky (uint32_t)p->m_pkthdr.lro_tcp_d_csum; 9509ca874cfSHans Petter Selasky while (csum > 0xffff) 9519ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 9529ca874cfSHans Petter Selasky } 9539ca874cfSHans Petter Selasky 9549ca874cfSHans Petter Selasky /* Update various counters. */ 9559ca874cfSHans Petter Selasky m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len; 9569ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = csum; 9579ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len; 9589ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs; 95924fe6643SRyan Stone le->needs_merge = 1; 9609ca874cfSHans Petter Selasky } 9619ca874cfSHans Petter Selasky 9629ca874cfSHans Petter Selasky static void 9639ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le) 964e57b2d0eSRandall Stewart { 965e57b2d0eSRandall Stewart /* 966e57b2d0eSRandall Stewart * Walk through the mbuf chain we 967e57b2d0eSRandall Stewart * have on tap and compress/condense 968e57b2d0eSRandall Stewart * as required. 969e57b2d0eSRandall Stewart */ 970e57b2d0eSRandall Stewart uint32_t *ts_ptr; 971e57b2d0eSRandall Stewart struct mbuf *m; 972e57b2d0eSRandall Stewart struct tcphdr *th; 9739ca874cfSHans Petter Selasky uint32_t tcp_data_len_total; 9749ca874cfSHans Petter Selasky uint32_t tcp_data_seg_total; 9759ca874cfSHans Petter Selasky uint16_t tcp_data_len; 9769ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 977e57b2d0eSRandall Stewart 978e57b2d0eSRandall Stewart /* 979e57b2d0eSRandall Stewart * First we must check the lead (m_head) 980e57b2d0eSRandall Stewart * we must make sure that it is *not* 981e57b2d0eSRandall Stewart * something that should be sent up 982e57b2d0eSRandall Stewart * right away (sack etc). 983e57b2d0eSRandall Stewart */ 984e57b2d0eSRandall Stewart again: 985e57b2d0eSRandall Stewart m = le->m_head->m_nextpkt; 986e57b2d0eSRandall Stewart if (m == NULL) { 9879ca874cfSHans Petter Selasky /* Just one left. */ 988e57b2d0eSRandall Stewart return; 989e57b2d0eSRandall Stewart } 9909ca874cfSHans Petter Selasky 9919ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 9929ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 9939ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 994e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 9959ca874cfSHans Petter Selasky 9969ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 9979ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 998e57b2d0eSRandall Stewart /* 999e57b2d0eSRandall Stewart * Its not the timestamp. We can't 1000e57b2d0eSRandall Stewart * use this guy as the head. 1001e57b2d0eSRandall Stewart */ 1002e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10039ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1004e57b2d0eSRandall Stewart goto again; 1005e57b2d0eSRandall Stewart } 10061ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 1007e57b2d0eSRandall Stewart /* 10081dadeab3SGordon Bergling * Make sure that previously seen segments/ACKs are delivered 1009e57b2d0eSRandall Stewart * before this segment, e.g. FIN. 1010e57b2d0eSRandall Stewart */ 1011e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10129ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1013e57b2d0eSRandall Stewart goto again; 1014e57b2d0eSRandall Stewart } 1015e57b2d0eSRandall Stewart while((m = le->m_head->m_nextpkt) != NULL) { 1016e57b2d0eSRandall Stewart /* 1017e57b2d0eSRandall Stewart * condense m into le, first 1018e57b2d0eSRandall Stewart * pull m out of the list. 1019e57b2d0eSRandall Stewart */ 1020e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 1021e57b2d0eSRandall Stewart m->m_nextpkt = NULL; 1022e57b2d0eSRandall Stewart /* Setup my data */ 10239ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 10249ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 1025e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 10269ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 10279ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 10289ca874cfSHans Petter Selasky tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len; 10299ca874cfSHans Petter Selasky tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs; 10309ca874cfSHans Petter Selasky 10319ca874cfSHans Petter Selasky if (tcp_data_seg_total >= lc->lro_ackcnt_lim || 10329ca874cfSHans Petter Selasky tcp_data_len_total >= lc->lro_length_lim) { 1033e57b2d0eSRandall Stewart /* Flush now if appending will result in overflow. */ 10349ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1035e57b2d0eSRandall Stewart goto again; 1036e57b2d0eSRandall Stewart } 10379ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && 10389ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 10399ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 1040e57b2d0eSRandall Stewart /* 1041e57b2d0eSRandall Stewart * Maybe a sack in the new one? We need to 1042e57b2d0eSRandall Stewart * start all over after flushing the 1043e57b2d0eSRandall Stewart * current le. We will go up to the beginning 1044e57b2d0eSRandall Stewart * and flush it (calling the replace again possibly 1045e57b2d0eSRandall Stewart * or just returning). 1046e57b2d0eSRandall Stewart */ 10479ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1048e57b2d0eSRandall Stewart goto again; 1049e57b2d0eSRandall Stewart } 10501ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 10519ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1052e57b2d0eSRandall Stewart goto again; 1053e57b2d0eSRandall Stewart } 10549ca874cfSHans Petter Selasky if (tcp_opt_len != 0) { 1055e57b2d0eSRandall Stewart uint32_t tsval = ntohl(*(ts_ptr + 1)); 1056e57b2d0eSRandall Stewart /* Make sure timestamp values are increasing. */ 1057e57b2d0eSRandall Stewart if (TSTMP_GT(le->tsval, tsval)) { 10589ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1059e57b2d0eSRandall Stewart goto again; 1060e57b2d0eSRandall Stewart } 1061e57b2d0eSRandall Stewart le->tsval = tsval; 1062e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 1063e57b2d0eSRandall Stewart } 1064e57b2d0eSRandall Stewart /* Try to append the new segment. */ 1065e57b2d0eSRandall Stewart if (__predict_false(ntohl(th->th_seq) != le->next_seq || 10661ebf4607SRichard Scheffenegger ((tcp_get_flags(th) & TH_ACK) != 10673284f492SRyan Stone (le->flags & TH_ACK)) || 1068e57b2d0eSRandall Stewart (tcp_data_len == 0 && 1069e57b2d0eSRandall Stewart le->ack_seq == th->th_ack && 1070e57b2d0eSRandall Stewart le->window == th->th_win))) { 10713284f492SRyan Stone /* Out of order packet, non-ACK + ACK or dup ACK. */ 10729ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1073e57b2d0eSRandall Stewart goto again; 1074e57b2d0eSRandall Stewart } 10759ca874cfSHans Petter Selasky if (tcp_data_len != 0 || 10769ca874cfSHans Petter Selasky SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1077e57b2d0eSRandall Stewart le->next_seq += tcp_data_len; 1078e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 1079e57b2d0eSRandall Stewart le->window = th->th_win; 108024fe6643SRyan Stone le->needs_merge = 1; 1081e57b2d0eSRandall Stewart } else if (th->th_ack == le->ack_seq) { 108224fe6643SRyan Stone if (WIN_GT(th->th_win, le->window)) { 108324fe6643SRyan Stone le->window = th->th_win; 108424fe6643SRyan Stone le->needs_merge = 1; 108524fe6643SRyan Stone } 1086e57b2d0eSRandall Stewart } 10879ca874cfSHans Petter Selasky 1088e57b2d0eSRandall Stewart if (tcp_data_len == 0) { 1089e57b2d0eSRandall Stewart m_freem(m); 1090e57b2d0eSRandall Stewart continue; 1091e57b2d0eSRandall Stewart } 10929ca874cfSHans Petter Selasky 10939ca874cfSHans Petter Selasky /* Merge TCP data checksum and length to head mbuf. */ 109424fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(le, m); 10959ca874cfSHans Petter Selasky 1096e57b2d0eSRandall Stewart /* 1097e57b2d0eSRandall Stewart * Adjust the mbuf so that m_data points to the first byte of 1098e57b2d0eSRandall Stewart * the ULP payload. Adjust the mbuf to avoid complications and 1099e57b2d0eSRandall Stewart * append new segment to existing mbuf chain. 1100e57b2d0eSRandall Stewart */ 1101e57b2d0eSRandall Stewart m_adj(m, m->m_pkthdr.len - tcp_data_len); 1102e57b2d0eSRandall Stewart m_demote_pkthdr(m); 1103e57b2d0eSRandall Stewart le->m_tail->m_next = m; 1104e57b2d0eSRandall Stewart le->m_tail = m_last(m); 1105e57b2d0eSRandall Stewart } 1106e57b2d0eSRandall Stewart } 1107e57b2d0eSRandall Stewart 1108*e06cf0fcSMichael Tuexen static void 110969a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 111069a34e8dSRandall Stewart { 111169a34e8dSRandall Stewart 11122c6fc36aSGleb Smirnoff /* Only optimise if there are multiple packets waiting. */ 1113dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 11142c6fc36aSGleb Smirnoff if (tcp_lro_flush_tcphpts == NULL || 11152c6fc36aSGleb Smirnoff tcp_lro_flush_tcphpts(lc, le) != 0) { 11169ca874cfSHans Petter Selasky tcp_lro_condense(lc, le); 11179ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 1118e57b2d0eSRandall Stewart } 111962b5b6ecSBjoern A. Zeeb lc->lro_flushed++; 112062b5b6ecSBjoern A. Zeeb bzero(le, sizeof(*le)); 11211ea44822SSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_free, le, next); 112262b5b6ecSBjoern A. Zeeb } 11236c5087a8SJack F Vogel 1124fc271df3SHans Petter Selasky #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1)) 1125e936121dSHans Petter Selasky 1126fc271df3SHans Petter Selasky /* 1127fc271df3SHans Petter Selasky * The tcp_lro_sort() routine is comparable to qsort(), except it has 1128fc271df3SHans Petter Selasky * a worst case complexity limit of O(MIN(N,64)*N), where N is the 1129fc271df3SHans Petter Selasky * number of elements to sort and 64 is the number of sequence bits 1130fc271df3SHans Petter Selasky * available. The algorithm is bit-slicing the 64-bit sequence number, 1131fc271df3SHans Petter Selasky * sorting one bit at a time from the most significant bit until the 1132ec668905SHans Petter Selasky * least significant one, skipping the constant bits. This is 1133ec668905SHans Petter Selasky * typically called a radix sort. 1134fc271df3SHans Petter Selasky */ 1135fc271df3SHans Petter Selasky static void 1136fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size) 1137fc271df3SHans Petter Selasky { 1138fc271df3SHans Petter Selasky struct lro_mbuf_sort temp; 1139fc271df3SHans Petter Selasky uint64_t ones; 1140fc271df3SHans Petter Selasky uint64_t zeros; 1141fc271df3SHans Petter Selasky uint32_t x; 1142fc271df3SHans Petter Selasky uint32_t y; 1143e936121dSHans Petter Selasky 1144fc271df3SHans Petter Selasky repeat: 1145ec668905SHans Petter Selasky /* for small arrays insertion sort is faster */ 1146fc271df3SHans Petter Selasky if (size <= 12) { 1147ec668905SHans Petter Selasky for (x = 1; x < size; x++) { 1148fc271df3SHans Petter Selasky temp = parray[x]; 1149ec668905SHans Petter Selasky for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--) 1150ec668905SHans Petter Selasky parray[y] = parray[y - 1]; 1151fc271df3SHans Petter Selasky parray[y] = temp; 1152fc271df3SHans Petter Selasky } 1153fc271df3SHans Petter Selasky return; 1154fc271df3SHans Petter Selasky } 1155e936121dSHans Petter Selasky 1156fc271df3SHans Petter Selasky /* compute sequence bits which are constant */ 1157fc271df3SHans Petter Selasky ones = 0; 1158fc271df3SHans Petter Selasky zeros = 0; 1159fc271df3SHans Petter Selasky for (x = 0; x != size; x++) { 1160fc271df3SHans Petter Selasky ones |= parray[x].seq; 1161fc271df3SHans Petter Selasky zeros |= ~parray[x].seq; 1162fc271df3SHans Petter Selasky } 1163fc271df3SHans Petter Selasky 1164fc271df3SHans Petter Selasky /* compute bits which are not constant into "ones" */ 1165fc271df3SHans Petter Selasky ones &= zeros; 1166fc271df3SHans Petter Selasky if (ones == 0) 1167fc271df3SHans Petter Selasky return; 1168fc271df3SHans Petter Selasky 1169fc271df3SHans Petter Selasky /* pick the most significant bit which is not constant */ 1170fc271df3SHans Petter Selasky ones = tcp_lro_msb_64(ones); 1171fc271df3SHans Petter Selasky 1172fc271df3SHans Petter Selasky /* 1173fc271df3SHans Petter Selasky * Move entries having cleared sequence bits to the beginning 1174fc271df3SHans Petter Selasky * of the array: 1175fc271df3SHans Petter Selasky */ 1176fc271df3SHans Petter Selasky for (x = y = 0; y != size; y++) { 1177fc271df3SHans Petter Selasky /* skip set bits */ 1178fc271df3SHans Petter Selasky if (parray[y].seq & ones) 1179fc271df3SHans Petter Selasky continue; 1180fc271df3SHans Petter Selasky /* swap entries */ 1181fc271df3SHans Petter Selasky temp = parray[x]; 1182fc271df3SHans Petter Selasky parray[x] = parray[y]; 1183fc271df3SHans Petter Selasky parray[y] = temp; 1184fc271df3SHans Petter Selasky x++; 1185fc271df3SHans Petter Selasky } 1186fc271df3SHans Petter Selasky 1187fc271df3SHans Petter Selasky KASSERT(x != 0 && x != size, ("Memory is corrupted\n")); 1188fc271df3SHans Petter Selasky 1189fc271df3SHans Petter Selasky /* sort zeros */ 1190fc271df3SHans Petter Selasky tcp_lro_sort(parray, x); 1191fc271df3SHans Petter Selasky 1192fc271df3SHans Petter Selasky /* sort ones */ 1193fc271df3SHans Petter Selasky parray += x; 1194fc271df3SHans Petter Selasky size -= x; 1195fc271df3SHans Petter Selasky goto repeat; 1196e936121dSHans Petter Selasky } 1197e936121dSHans Petter Selasky 1198e936121dSHans Petter Selasky void 1199e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc) 1200e936121dSHans Petter Selasky { 1201fc271df3SHans Petter Selasky uint64_t seq; 1202fc271df3SHans Petter Selasky uint64_t nseq; 1203e936121dSHans Petter Selasky unsigned x; 1204e936121dSHans Petter Selasky 1205dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 1206e936121dSHans Petter Selasky /* check if no mbufs to flush */ 12076dd38b87SSepherosa Ziehau if (lc->lro_mbuf_count == 0) 1208e936121dSHans Petter Selasky goto done; 1209d7955cc0SRandall Stewart if (lc->lro_cpu_is_set == 0) { 1210d7955cc0SRandall Stewart if (lc->lro_last_cpu == curcpu) { 1211d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu++; 1212d7955cc0SRandall Stewart /* Have we reached the threshold to declare a cpu? */ 1213d7955cc0SRandall Stewart if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh) 1214d7955cc0SRandall Stewart lc->lro_cpu_is_set = 1; 1215d7955cc0SRandall Stewart } else { 1216d7955cc0SRandall Stewart lc->lro_last_cpu = curcpu; 1217d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu = 0; 1218d7955cc0SRandall Stewart } 1219d7955cc0SRandall Stewart } 1220a9b66dbdSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 1221a9b66dbdSHans Petter Selasky 12229ca874cfSHans Petter Selasky /* get current time */ 1223b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 12249ca874cfSHans Petter Selasky 1225e936121dSHans Petter Selasky /* sort all mbufs according to stream */ 1226fc271df3SHans Petter Selasky tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count); 1227e936121dSHans Petter Selasky 1228e936121dSHans Petter Selasky /* input data into LRO engine, stream by stream */ 1229fc271df3SHans Petter Selasky seq = 0; 1230e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) { 1231e936121dSHans Petter Selasky struct mbuf *mb; 1232e936121dSHans Petter Selasky 1233fc271df3SHans Petter Selasky /* get mbuf */ 1234fc271df3SHans Petter Selasky mb = lc->lro_mbuf_data[x].mb; 1235fc271df3SHans Petter Selasky 1236fc271df3SHans Petter Selasky /* get sequence number, masking away the packet index */ 1237fc271df3SHans Petter Selasky nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24); 1238e936121dSHans Petter Selasky 1239e936121dSHans Petter Selasky /* check for new stream */ 1240fc271df3SHans Petter Selasky if (seq != nseq) { 1241fc271df3SHans Petter Selasky seq = nseq; 1242e936121dSHans Petter Selasky 1243e936121dSHans Petter Selasky /* flush active streams */ 12446dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 1245e936121dSHans Petter Selasky } 1246fc271df3SHans Petter Selasky 1247e936121dSHans Petter Selasky /* add packet to LRO engine */ 12489ca874cfSHans Petter Selasky if (tcp_lro_rx_common(lc, mb, 0, false) != 0) { 12494e0ce82bSRandall Stewart /* Flush anything we have acummulated */ 12504e0ce82bSRandall Stewart tcp_lro_flush_active(lc); 1251e936121dSHans Petter Selasky /* input packet to network layer */ 1252e936121dSHans Petter Selasky (*lc->ifp->if_input)(lc->ifp, mb); 1253e936121dSHans Petter Selasky lc->lro_queued++; 1254e936121dSHans Petter Selasky lc->lro_flushed++; 1255e936121dSHans Petter Selasky } 1256e936121dSHans Petter Selasky } 1257a9b66dbdSHans Petter Selasky CURVNET_RESTORE(); 1258e936121dSHans Petter Selasky done: 1259e936121dSHans Petter Selasky /* flush active streams */ 12606dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 12612c6fc36aSGleb Smirnoff tcp_hpts_softclock(); 1262e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 126362b5b6ecSBjoern A. Zeeb } 12646c5087a8SJack F Vogel 12659ca874cfSHans Petter Selasky static struct lro_head * 12669ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser) 12679ca874cfSHans Petter Selasky { 12689ca874cfSHans Petter Selasky u_long hash; 12699ca874cfSHans Petter Selasky 12709ca874cfSHans Petter Selasky if (M_HASHTYPE_ISHASH(m)) { 12719ca874cfSHans Petter Selasky hash = m->m_pkthdr.flowid; 12729ca874cfSHans Petter Selasky } else { 12739ca874cfSHans Petter Selasky for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++) 12749ca874cfSHans Petter Selasky hash += parser->data.raw[i]; 127569a34e8dSRandall Stewart } 12769ca874cfSHans Petter Selasky return (&lc->lro_hash[hash % lc->lro_hashsz]); 12779ca874cfSHans Petter Selasky } 127869a34e8dSRandall Stewart 127905cde7efSSepherosa Ziehau static int 12809ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash) 128162b5b6ecSBjoern A. Zeeb { 12829ca874cfSHans Petter Selasky struct lro_parser pi; /* inner address data */ 12839ca874cfSHans Petter Selasky struct lro_parser po; /* outer address data */ 12849ca874cfSHans Petter Selasky struct lro_parser *pa; /* current parser for TCP stream */ 128562b5b6ecSBjoern A. Zeeb struct lro_entry *le; 128605cde7efSSepherosa Ziehau struct lro_head *bucket; 12879ca874cfSHans Petter Selasky struct tcphdr *th; 12889ca874cfSHans Petter Selasky int tcp_data_len; 12899ca874cfSHans Petter Selasky int tcp_opt_len; 12909ca874cfSHans Petter Selasky int error; 12919ca874cfSHans Petter Selasky uint16_t tcp_data_sum; 12926c5087a8SJack F Vogel 12939ca874cfSHans Petter Selasky #ifdef INET 12949ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 12959ca874cfSHans Petter Selasky if (__predict_false(V_ipforwarding != 0)) 12969ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 12979ca874cfSHans Petter Selasky #endif 12989ca874cfSHans Petter Selasky #ifdef INET6 12999ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 13009ca874cfSHans Petter Selasky if (__predict_false(V_ip6_forwarding != 0)) 13019ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 13029ca874cfSHans Petter Selasky #endif 1303ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1304ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1305ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1306ca1a7e10SRandall Stewart /* 1307ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1308ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1309ca1a7e10SRandall Stewart * a packet. 1310ca1a7e10SRandall Stewart */ 1311ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1312ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1313ca1a7e10SRandall Stewart } 131462b5b6ecSBjoern A. Zeeb /* We expect a contiguous header [eh, ip, tcp]. */ 13159ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &po, &pi, true); 13169ca874cfSHans Petter Selasky if (__predict_false(pa == NULL)) 13179ca874cfSHans Petter Selasky return (TCP_LRO_NOT_SUPPORTED); 13189ca874cfSHans Petter Selasky 13199ca874cfSHans Petter Selasky /* We don't expect any padding. */ 13209ca874cfSHans Petter Selasky error = tcp_lro_trim_mbuf_chain(m, pa); 13219ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 13229ca874cfSHans Petter Selasky return (error); 13239ca874cfSHans Petter Selasky 13249ca874cfSHans Petter Selasky #ifdef INET 13259ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 13269ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 13279ca874cfSHans Petter Selasky error = tcp_lro_rx_ipv4(lc, m, pa->ip4); 13289ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 13299ca874cfSHans Petter Selasky return (error); 13309ca874cfSHans Petter Selasky break; 13319ca874cfSHans Petter Selasky default: 13329ca874cfSHans Petter Selasky break; 13339ca874cfSHans Petter Selasky } 13349ca874cfSHans Petter Selasky #endif 13359ca874cfSHans Petter Selasky /* If no hardware or arrival stamp on the packet add timestamp */ 1336e57b2d0eSRandall Stewart if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) { 1337b45daaeaSRandall Stewart m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1338e57b2d0eSRandall Stewart m->m_flags |= M_TSTMP_LRO; 1339e57b2d0eSRandall Stewart } 13406c5087a8SJack F Vogel 13419ca874cfSHans Petter Selasky /* Get pointer to TCP header. */ 13429ca874cfSHans Petter Selasky th = pa->tcp; 13439ca874cfSHans Petter Selasky 13449ca874cfSHans Petter Selasky /* Don't process SYN packets. */ 13451ebf4607SRichard Scheffenegger if (__predict_false(tcp_get_flags(th) & TH_SYN)) 134662b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 134762b5b6ecSBjoern A. Zeeb 13489ca874cfSHans Petter Selasky /* Get total TCP header length and compute payload length. */ 13499ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 13509ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th - 13519ca874cfSHans Petter Selasky (uint8_t *)m->m_data) - tcp_opt_len; 13529ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 13539ca874cfSHans Petter Selasky 13549ca874cfSHans Petter Selasky /* Don't process invalid TCP headers. */ 13559ca874cfSHans Petter Selasky if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0)) 135662b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 13579ca874cfSHans Petter Selasky 13589ca874cfSHans Petter Selasky /* Compute TCP data only checksum. */ 13599ca874cfSHans Petter Selasky if (tcp_data_len == 0) 13609ca874cfSHans Petter Selasky tcp_data_sum = 0; /* no data, no checksum */ 13619ca874cfSHans Petter Selasky else if (__predict_false(csum != 0)) 13629ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum); 13639ca874cfSHans Petter Selasky else 13649ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum); 13659ca874cfSHans Petter Selasky 13669ca874cfSHans Petter Selasky /* Save TCP info in mbuf. */ 13679ca874cfSHans Petter Selasky m->m_nextpkt = NULL; 13689ca874cfSHans Petter Selasky m->m_pkthdr.rcvif = lc->ifp; 13699ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum; 13709ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len = tcp_data_len; 13719ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data); 13729ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs = 1; 13739ca874cfSHans Petter Selasky 13749ca874cfSHans Petter Selasky /* Get hash bucket. */ 137505cde7efSSepherosa Ziehau if (!use_hash) { 137605cde7efSSepherosa Ziehau bucket = &lc->lro_hash[0]; 137705cde7efSSepherosa Ziehau } else { 13789ca874cfSHans Petter Selasky bucket = tcp_lro_rx_get_bucket(lc, m, pa); 137905cde7efSSepherosa Ziehau } 138005cde7efSSepherosa Ziehau 138162b5b6ecSBjoern A. Zeeb /* Try to find a matching previous segment. */ 138205cde7efSSepherosa Ziehau LIST_FOREACH(le, bucket, hash_next) { 13839ca874cfSHans Petter Selasky /* Compare addresses and ports. */ 13849ca874cfSHans Petter Selasky if (lro_address_compare(&po.data, &le->outer.data) == false || 13859ca874cfSHans Petter Selasky lro_address_compare(&pi.data, &le->inner.data) == false) 138662b5b6ecSBjoern A. Zeeb continue; 13879ca874cfSHans Petter Selasky 13889ca874cfSHans Petter Selasky /* Check if no data and old ACK. */ 13899ca874cfSHans Petter Selasky if (tcp_data_len == 0 && 13909ca874cfSHans Petter Selasky SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1391d7fb35d1SSean Bruno m_freem(m); 1392d7fb35d1SSean Bruno return (0); 1393d7fb35d1SSean Bruno } 139469a34e8dSRandall Stewart 13959ca874cfSHans Petter Selasky /* Mark "m" in the last spot. */ 1396e57b2d0eSRandall Stewart le->m_last_mbuf->m_nextpkt = m; 13979ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1398e57b2d0eSRandall Stewart le->m_last_mbuf = m; 139962b5b6ecSBjoern A. Zeeb return (0); 14006c5087a8SJack F Vogel } 14019ca874cfSHans Petter Selasky 140262b5b6ecSBjoern A. Zeeb /* Try to find an empty slot. */ 14031ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_free)) 1404489f0c3cSSepherosa Ziehau return (TCP_LRO_NO_ENTRIES); 140562b5b6ecSBjoern A. Zeeb 140662b5b6ecSBjoern A. Zeeb /* Start a new segment chain. */ 14071ea44822SSepherosa Ziehau le = LIST_FIRST(&lc->lro_free); 14081ea44822SSepherosa Ziehau LIST_REMOVE(le, next); 140905cde7efSSepherosa Ziehau tcp_lro_active_insert(lc, bucket, le); 141062b5b6ecSBjoern A. Zeeb 14119ca874cfSHans Petter Selasky /* Make sure the headers are set. */ 14129ca874cfSHans Petter Selasky le->inner = pi; 14139ca874cfSHans Petter Selasky le->outer = po; 141462b5b6ecSBjoern A. Zeeb 14159ca874cfSHans Petter Selasky /* Store time this entry was allocated. */ 14169ca874cfSHans Petter Selasky le->alloc_time = lc->lro_last_queue_time; 141769a34e8dSRandall Stewart 14189ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, th); 141969a34e8dSRandall Stewart 14209ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1421e57b2d0eSRandall Stewart le->m_last_mbuf = m; 14229ca874cfSHans Petter Selasky 142362b5b6ecSBjoern A. Zeeb return (0); 142462b5b6ecSBjoern A. Zeeb } 142562b5b6ecSBjoern A. Zeeb 142605cde7efSSepherosa Ziehau int 142705cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 142805cde7efSSepherosa Ziehau { 14299ca874cfSHans Petter Selasky int error; 143005cde7efSSepherosa Ziehau 1431ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1432ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1433ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1434ca1a7e10SRandall Stewart /* 1435ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1436ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1437ca1a7e10SRandall Stewart * a packet. 1438ca1a7e10SRandall Stewart */ 1439ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1440ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1441ca1a7e10SRandall Stewart } 14429ca874cfSHans Petter Selasky /* get current time */ 1443b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 14449ca874cfSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 14459ca874cfSHans Petter Selasky error = tcp_lro_rx_common(lc, m, csum, true); 14464e0ce82bSRandall Stewart if (__predict_false(error != 0)) { 14474e0ce82bSRandall Stewart /* 14484e0ce82bSRandall Stewart * Flush anything we have acummulated 14494e0ce82bSRandall Stewart * ahead of this packet that can't 14504e0ce82bSRandall Stewart * be LRO'd. This preserves order. 14514e0ce82bSRandall Stewart */ 14524e0ce82bSRandall Stewart tcp_lro_flush_active(lc); 14534e0ce82bSRandall Stewart } 14549ca874cfSHans Petter Selasky CURVNET_RESTORE(); 14559ca874cfSHans Petter Selasky 14569ca874cfSHans Petter Selasky return (error); 145705cde7efSSepherosa Ziehau } 145805cde7efSSepherosa Ziehau 1459e936121dSHans Petter Selasky void 1460e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 1461e936121dSHans Petter Selasky { 1462dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 1463e936121dSHans Petter Selasky /* sanity checks */ 1464e936121dSHans Petter Selasky if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 1465e936121dSHans Petter Selasky lc->lro_mbuf_max == 0)) { 1466e936121dSHans Petter Selasky /* packet drop */ 1467e936121dSHans Petter Selasky m_freem(mb); 1468e936121dSHans Petter Selasky return; 1469e936121dSHans Petter Selasky } 1470e936121dSHans Petter Selasky 1471e936121dSHans Petter Selasky /* check if packet is not LRO capable */ 1472ca1a7e10SRandall Stewart if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 1473e936121dSHans Petter Selasky /* input packet to network layer */ 1474e936121dSHans Petter Selasky (*lc->ifp->if_input) (lc->ifp, mb); 1475e936121dSHans Petter Selasky return; 1476e936121dSHans Petter Selasky } 1477e936121dSHans Petter Selasky 14784e0ce82bSRandall Stewart /* If no hardware or arrival stamp on the packet add timestamp */ 14794e0ce82bSRandall Stewart if ((tcplro_stacks_wanting_mbufq > 0) && 14804e0ce82bSRandall Stewart (tcp_less_accurate_lro_ts == 0) && 14814e0ce82bSRandall Stewart ((mb->m_flags & M_TSTMP) == 0)) { 14824e0ce82bSRandall Stewart /* Add in an LRO time since no hardware */ 14834e0ce82bSRandall Stewart binuptime(&lc->lro_last_queue_time); 14844e0ce82bSRandall Stewart mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 14854e0ce82bSRandall Stewart mb->m_flags |= M_TSTMP_LRO; 14864e0ce82bSRandall Stewart } 14874e0ce82bSRandall Stewart 1488fc271df3SHans Petter Selasky /* create sequence number */ 1489fc271df3SHans Petter Selasky lc->lro_mbuf_data[lc->lro_mbuf_count].seq = 1490fc271df3SHans Petter Selasky (((uint64_t)M_HASHTYPE_GET(mb)) << 56) | 1491fc271df3SHans Petter Selasky (((uint64_t)mb->m_pkthdr.flowid) << 24) | 1492fc271df3SHans Petter Selasky ((uint64_t)lc->lro_mbuf_count); 1493e936121dSHans Petter Selasky 1494e936121dSHans Petter Selasky /* enter mbuf */ 1495f8acc03eSNavdeep Parhar lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb; 1496f8acc03eSNavdeep Parhar 1497f8acc03eSNavdeep Parhar /* flush if array is full */ 1498f8acc03eSNavdeep Parhar if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max)) 1499f8acc03eSNavdeep Parhar tcp_lro_flush_all(lc); 1500e936121dSHans Petter Selasky } 1501e936121dSHans Petter Selasky 150262b5b6ecSBjoern A. Zeeb /* end */ 1503