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); 869ca874cfSHans Petter Selasky 878452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 888452c1b3SSepherosa Ziehau "TCP LRO"); 898452c1b3SSepherosa Ziehau 90*4f9c93f1SGleb Smirnoff long tcplro_stacks_wanting_mbufq; 91e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue; 92e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue; 93e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed; 94e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken; 9569a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf; 9669a34e8dSRandall Stewart counter_u64_t tcp_would_have_but; 9769a34e8dSRandall Stewart counter_u64_t tcp_comp_total; 9869a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total; 99ca1a7e10SRandall Stewart counter_u64_t tcp_bad_csums; 100e57b2d0eSRandall Stewart 1018452c1b3SSepherosa Ziehau static unsigned tcp_lro_entries = TCP_LRO_ENTRIES; 1028452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, 1038452c1b3SSepherosa Ziehau CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0, 1048452c1b3SSepherosa Ziehau "default number of LRO entries"); 10569a34e8dSRandall Stewart 106d7955cc0SRandall Stewart static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH; 107d7955cc0SRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold, 108d7955cc0SRandall Stewart CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0, 10908c7f1b6SNavdeep Parhar "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?"); 110d7955cc0SRandall Stewart 1114e0ce82bSRandall Stewart static uint32_t tcp_less_accurate_lro_ts = 0; 1124e0ce82bSRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate, 1134e0ce82bSRandall Stewart CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0, 1144e0ce82bSRandall Stewart "Do we trade off efficency by doing less timestamp operations for time accuracy?"); 1154e0ce82bSRandall 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 * 2311d171e5aSRandall 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; 2611d171e5aSRandall 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 */ 2651d171e5aSRandall Stewart mlen -= ETHER_HDR_LEN; 2669ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + ETHER_HDR_LEN; 2679ca874cfSHans Petter Selasky } 2681d171e5aSRandall Stewart if (__predict_false(mlen <= 0)) 2691d171e5aSRandall 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; 2741d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip))) 2751d171e5aSRandall 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; 282abba5876SAndrew Gallatin /* .. and the packet has valid src/dst addrs */ 283abba5876SAndrew Gallatin if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY || 284abba5876SAndrew Gallatin parser->ip4->ip_dst.s_addr == INADDR_ANY)) 285abba5876SAndrew Gallatin break; 2869ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2); 2871d171e5aSRandall Stewart mlen -= sizeof(struct ip); 2889ca874cfSHans Petter Selasky if (update_data) { 2899ca874cfSHans Petter Selasky parser->data.s_addr.v4 = parser->ip4->ip_src; 2909ca874cfSHans Petter Selasky parser->data.d_addr.v4 = parser->ip4->ip_dst; 2919ca874cfSHans Petter Selasky } 2929ca874cfSHans Petter Selasky switch (parser->ip4->ip_p) { 2939ca874cfSHans Petter Selasky case IPPROTO_UDP: 2941d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 2951d171e5aSRandall Stewart return (NULL); 2969ca874cfSHans Petter Selasky parser->udp = ptr; 2979ca874cfSHans Petter Selasky if (update_data) { 2989ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_UDP; 2999ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 3009ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 3019ca874cfSHans Petter Selasky } else { 3029ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP); 3039ca874cfSHans Petter Selasky } 3049ca874cfSHans Petter Selasky ptr = ((uint8_t *)ptr + sizeof(*parser->udp)); 3059ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3069ca874cfSHans Petter Selasky return (ptr); 3079ca874cfSHans Petter Selasky case IPPROTO_TCP: 3089ca874cfSHans Petter Selasky parser->tcp = ptr; 3091d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 3101d171e5aSRandall Stewart return (NULL); 3119ca874cfSHans Petter Selasky if (update_data) { 3129ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_TCP; 3139ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3149ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3159ca874cfSHans Petter Selasky } else { 3169ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP); 3179ca874cfSHans Petter Selasky } 3181d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 3191d171e5aSRandall Stewart return (NULL); 3209ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3219ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3229ca874cfSHans Petter Selasky return (ptr); 3239ca874cfSHans Petter Selasky default: 3249ca874cfSHans Petter Selasky break; 3259ca874cfSHans Petter Selasky } 3269ca874cfSHans Petter Selasky break; 3279ca874cfSHans Petter Selasky #endif 3289ca874cfSHans Petter Selasky #ifdef INET6 3299ca874cfSHans Petter Selasky case htons(ETHERTYPE_IPV6): 3309ca874cfSHans Petter Selasky parser->ip6 = ptr; 3311d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip6_hdr))) 3321d171e5aSRandall Stewart return (NULL); 333abba5876SAndrew Gallatin /* Ensure the packet has valid src/dst addrs */ 334abba5876SAndrew Gallatin if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) || 335abba5876SAndrew Gallatin IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst))) 336abba5876SAndrew Gallatin return (NULL); 3379ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->ip6); 3389ca874cfSHans Petter Selasky if (update_data) { 3399ca874cfSHans Petter Selasky parser->data.s_addr.v6 = parser->ip6->ip6_src; 3409ca874cfSHans Petter Selasky parser->data.d_addr.v6 = parser->ip6->ip6_dst; 3419ca874cfSHans Petter Selasky } 3421d171e5aSRandall Stewart mlen -= sizeof(struct ip6_hdr); 3439ca874cfSHans Petter Selasky switch (parser->ip6->ip6_nxt) { 3449ca874cfSHans Petter Selasky case IPPROTO_UDP: 3451d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 3461d171e5aSRandall Stewart return (NULL); 3479ca874cfSHans Petter Selasky parser->udp = ptr; 3489ca874cfSHans Petter Selasky if (update_data) { 3499ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_UDP; 3509ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 3519ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 3529ca874cfSHans Petter Selasky } else { 3539ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP); 3549ca874cfSHans Petter Selasky } 3559ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->udp); 3569ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3579ca874cfSHans Petter Selasky return (ptr); 3589ca874cfSHans Petter Selasky case IPPROTO_TCP: 3591d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 3601d171e5aSRandall Stewart return (NULL); 3619ca874cfSHans Petter Selasky parser->tcp = ptr; 3629ca874cfSHans Petter Selasky if (update_data) { 3639ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_TCP; 3649ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3659ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3669ca874cfSHans Petter Selasky } else { 3679ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP); 3689ca874cfSHans Petter Selasky } 3691d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 3701d171e5aSRandall Stewart return (NULL); 3719ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3729ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3739ca874cfSHans Petter Selasky return (ptr); 3749ca874cfSHans Petter Selasky default: 3759ca874cfSHans Petter Selasky break; 3769ca874cfSHans Petter Selasky } 3779ca874cfSHans Petter Selasky break; 3789ca874cfSHans Petter Selasky #endif 3799ca874cfSHans Petter Selasky default: 3809ca874cfSHans Petter Selasky break; 3819ca874cfSHans Petter Selasky } 3829ca874cfSHans Petter Selasky /* Invalid packet - cannot parse */ 3839ca874cfSHans Petter Selasky return (NULL); 3849ca874cfSHans Petter Selasky } 3859ca874cfSHans Petter Selasky 3869ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID | 3879ca874cfSHans Petter Selasky CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID; 3889ca874cfSHans Petter Selasky 3899ca874cfSHans Petter Selasky static inline struct lro_parser * 3909ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data) 3919ca874cfSHans Petter Selasky { 3929ca874cfSHans Petter Selasky void *data_ptr; 3939ca874cfSHans Petter Selasky 3949ca874cfSHans Petter Selasky /* Try to parse outer headers first. */ 3951d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len); 3969ca874cfSHans Petter Selasky if (data_ptr == NULL || po->total_hdr_len > m->m_len) 3979ca874cfSHans Petter Selasky return (NULL); 3989ca874cfSHans Petter Selasky 3999ca874cfSHans Petter Selasky if (update_data) { 4009ca874cfSHans Petter Selasky /* Store VLAN ID, if any. */ 4019ca874cfSHans Petter Selasky if (__predict_false(m->m_flags & M_VLANTAG)) { 4029ca874cfSHans Petter Selasky po->data.vlan_id = 4039ca874cfSHans Petter Selasky htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK); 4049ca874cfSHans Petter Selasky } 405bb5cd80eSHans Petter Selasky /* Store decrypted flag, if any. */ 40610a62eb1SHans Petter Selasky if (__predict_false((m->m_pkthdr.csum_flags & 40710a62eb1SHans Petter Selasky CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED)) 408bb5cd80eSHans Petter Selasky po->data.lro_flags |= LRO_FLAG_DECRYPTED; 4099ca874cfSHans Petter Selasky } 4109ca874cfSHans Petter Selasky 4119ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4129ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 4139ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 4149ca874cfSHans Petter Selasky /* Check for VXLAN headers. */ 4159ca874cfSHans Petter Selasky if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum) 4169ca874cfSHans Petter Selasky break; 4179ca874cfSHans Petter Selasky 4189ca874cfSHans Petter Selasky /* Try to parse inner headers. */ 4191d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true, 4201d171e5aSRandall Stewart (m->m_len - ((caddr_t)data_ptr - m->m_data))); 4211d171e5aSRandall Stewart if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len) 4229ca874cfSHans Petter Selasky break; 4239ca874cfSHans Petter Selasky 4249ca874cfSHans Petter Selasky /* Verify supported header types. */ 4259ca874cfSHans Petter Selasky switch (pi->data.lro_type) { 4269ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4279ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4289ca874cfSHans Petter Selasky return (pi); 4299ca874cfSHans Petter Selasky default: 4309ca874cfSHans Petter Selasky break; 4319ca874cfSHans Petter Selasky } 4329ca874cfSHans Petter Selasky break; 4339ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4349ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4359ca874cfSHans Petter Selasky if (update_data) 4369ca874cfSHans Petter Selasky memset(pi, 0, sizeof(*pi)); 4379ca874cfSHans Petter Selasky return (po); 4389ca874cfSHans Petter Selasky default: 4399ca874cfSHans Petter Selasky break; 4409ca874cfSHans Petter Selasky } 4419ca874cfSHans Petter Selasky return (NULL); 4429ca874cfSHans Petter Selasky } 4439ca874cfSHans Petter Selasky 4449ca874cfSHans Petter Selasky static inline int 4459ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po) 4469ca874cfSHans Petter Selasky { 4479ca874cfSHans Petter Selasky int len; 4489ca874cfSHans Petter Selasky 4499ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4509ca874cfSHans Petter Selasky #ifdef INET 4519ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4529ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) + 4539ca874cfSHans Petter Selasky ntohs(po->ip4->ip_len); 4549ca874cfSHans Petter Selasky break; 4559ca874cfSHans Petter Selasky #endif 4569ca874cfSHans Petter Selasky #ifdef INET6 4579ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4589ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) + 4599ca874cfSHans Petter Selasky ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6); 4609ca874cfSHans Petter Selasky break; 4619ca874cfSHans Petter Selasky #endif 4629ca874cfSHans Petter Selasky default: 4639ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4649ca874cfSHans Petter Selasky } 4659ca874cfSHans Petter Selasky 4669ca874cfSHans Petter Selasky /* 4679ca874cfSHans Petter Selasky * If the frame is padded beyond the end of the IP packet, 4689ca874cfSHans Petter Selasky * then trim the extra bytes off: 4699ca874cfSHans Petter Selasky */ 4709ca874cfSHans Petter Selasky if (__predict_true(m->m_pkthdr.len == len)) { 4719ca874cfSHans Petter Selasky return (0); 4729ca874cfSHans Petter Selasky } else if (m->m_pkthdr.len > len) { 4739ca874cfSHans Petter Selasky m_adj(m, len - m->m_pkthdr.len); 4749ca874cfSHans Petter Selasky return (0); 4759ca874cfSHans Petter Selasky } 4769ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4779ca874cfSHans Petter Selasky } 4789ca874cfSHans Petter Selasky 47969a34e8dSRandall Stewart static void 48069a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m) 48169a34e8dSRandall Stewart { 48269a34e8dSRandall Stewart struct mbuf *save; 48369a34e8dSRandall Stewart 48469a34e8dSRandall Stewart while (m) { 48569a34e8dSRandall Stewart save = m->m_nextpkt; 48669a34e8dSRandall Stewart m->m_nextpkt = NULL; 48769a34e8dSRandall Stewart m_freem(m); 48869a34e8dSRandall Stewart m = save; 48969a34e8dSRandall Stewart } 49069a34e8dSRandall Stewart } 49169a34e8dSRandall Stewart 4926c5087a8SJack F Vogel void 49362b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc) 4946c5087a8SJack F Vogel { 49562b5b6ecSBjoern A. Zeeb struct lro_entry *le; 496e936121dSHans Petter Selasky unsigned x; 4976c5087a8SJack F Vogel 498e936121dSHans Petter Selasky /* reset LRO free list */ 4991ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_free); 500e936121dSHans Petter Selasky 501e936121dSHans Petter Selasky /* free active mbufs, if any */ 5021ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 50351e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 50469a34e8dSRandall Stewart lro_free_mbuf_chain(le->m_head); 5056c5087a8SJack F Vogel } 506e936121dSHans Petter Selasky 50705cde7efSSepherosa Ziehau /* free hash table */ 50805cde7efSSepherosa Ziehau free(lc->lro_hash, M_LRO); 50905cde7efSSepherosa Ziehau lc->lro_hash = NULL; 51005cde7efSSepherosa Ziehau lc->lro_hashsz = 0; 51105cde7efSSepherosa Ziehau 512e936121dSHans Petter Selasky /* free mbuf array, if any */ 513e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) 514fc271df3SHans Petter Selasky m_freem(lc->lro_mbuf_data[x].mb); 515e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 516e936121dSHans Petter Selasky 517e936121dSHans Petter Selasky /* free allocated memory, if any */ 518e936121dSHans Petter Selasky free(lc->lro_mbuf_data, M_LRO); 519e936121dSHans Petter Selasky lc->lro_mbuf_data = NULL; 5206c5087a8SJack F Vogel } 5216c5087a8SJack F Vogel 52262b5b6ecSBjoern A. Zeeb static uint16_t 5239ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th) 52462b5b6ecSBjoern A. Zeeb { 5259ca874cfSHans Petter Selasky const uint16_t *ptr; 5269ca874cfSHans Petter Selasky uint32_t csum; 5279ca874cfSHans Petter Selasky uint16_t len; 52862b5b6ecSBjoern A. Zeeb 5299ca874cfSHans Petter Selasky csum = -th->th_sum; /* exclude checksum field */ 5309ca874cfSHans Petter Selasky len = th->th_off; 5319ca874cfSHans Petter Selasky ptr = (const uint16_t *)th; 5329ca874cfSHans Petter Selasky while (len--) { 5339ca874cfSHans Petter Selasky csum += *ptr; 5349ca874cfSHans Petter Selasky ptr++; 5359ca874cfSHans Petter Selasky csum += *ptr; 5369ca874cfSHans Petter Selasky ptr++; 53762b5b6ecSBjoern A. Zeeb } 5389ca874cfSHans Petter Selasky while (csum > 0xffff) 5399ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 54062b5b6ecSBjoern A. Zeeb 5419ca874cfSHans Petter Selasky return (csum); 54262b5b6ecSBjoern A. Zeeb } 54362b5b6ecSBjoern A. Zeeb 54462b5b6ecSBjoern A. Zeeb static uint16_t 5459ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum) 54662b5b6ecSBjoern A. Zeeb { 54762b5b6ecSBjoern A. Zeeb uint32_t c; 54862b5b6ecSBjoern A. Zeeb uint16_t cs; 54962b5b6ecSBjoern A. Zeeb 5509ca874cfSHans Petter Selasky c = tcp_csum; 55162b5b6ecSBjoern A. Zeeb 5529ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 55362b5b6ecSBjoern A. Zeeb #ifdef INET6 5549ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 5559ca874cfSHans Petter Selasky /* Compute full pseudo IPv6 header checksum. */ 5569ca874cfSHans Petter Selasky cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0); 55762b5b6ecSBjoern A. Zeeb break; 55862b5b6ecSBjoern A. Zeeb #endif 55962b5b6ecSBjoern A. Zeeb #ifdef INET 5609ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 5619ca874cfSHans Petter Selasky /* Compute full pseudo IPv4 header checsum. */ 5629ca874cfSHans Petter Selasky cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP); 5639ca874cfSHans Petter Selasky cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs)); 56462b5b6ecSBjoern A. Zeeb break; 56562b5b6ecSBjoern A. Zeeb #endif 56662b5b6ecSBjoern A. Zeeb default: 56762b5b6ecSBjoern A. Zeeb cs = 0; /* Keep compiler happy. */ 5689ca874cfSHans Petter Selasky break; 56962b5b6ecSBjoern A. Zeeb } 57062b5b6ecSBjoern A. Zeeb 5719ca874cfSHans Petter Selasky /* Complement checksum. */ 57262b5b6ecSBjoern A. Zeeb cs = ~cs; 57362b5b6ecSBjoern A. Zeeb c += cs; 57462b5b6ecSBjoern A. Zeeb 5759ca874cfSHans Petter Selasky /* Remove TCP header checksum. */ 5769ca874cfSHans Petter Selasky cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp); 57762b5b6ecSBjoern A. Zeeb c += cs; 5789ca874cfSHans Petter Selasky 5799ca874cfSHans Petter Selasky /* Compute checksum remainder. */ 58062b5b6ecSBjoern A. Zeeb while (c > 0xffff) 58162b5b6ecSBjoern A. Zeeb c = (c >> 16) + (c & 0xffff); 58262b5b6ecSBjoern A. Zeeb 5839ca874cfSHans Petter Selasky return (c); 58462b5b6ecSBjoern A. Zeeb } 58562b5b6ecSBjoern A. Zeeb 5866dd38b87SSepherosa Ziehau static void 5876dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc) 5886dd38b87SSepherosa Ziehau { 5896dd38b87SSepherosa Ziehau struct lro_entry *le; 5906dd38b87SSepherosa Ziehau 5911ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 59251e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 5936dd38b87SSepherosa Ziehau tcp_lro_flush(lc, le); 5946dd38b87SSepherosa Ziehau } 5956dd38b87SSepherosa Ziehau } 5966dd38b87SSepherosa Ziehau 5974e0ce82bSRandall Stewart static void 5984e0ce82bSRandall Stewart tcp_lro_flush_active(struct lro_ctrl *lc) 5994e0ce82bSRandall Stewart { 6004e0ce82bSRandall Stewart struct lro_entry *le; 6014e0ce82bSRandall Stewart 6024e0ce82bSRandall Stewart /* 6034e0ce82bSRandall Stewart * Walk through the list of le entries, and 6044e0ce82bSRandall Stewart * any one that does have packets flush. This 6054e0ce82bSRandall Stewart * is called because we have an inbound packet 6064e0ce82bSRandall Stewart * (e.g. SYN) that has to have all others flushed 6074e0ce82bSRandall Stewart * in front of it. Note we have to do the remove 6084e0ce82bSRandall Stewart * because tcp_lro_flush() assumes that the entry 6094e0ce82bSRandall Stewart * is being freed. This is ok it will just get 6104e0ce82bSRandall Stewart * reallocated again like it was new. 6114e0ce82bSRandall Stewart */ 6124e0ce82bSRandall Stewart LIST_FOREACH(le, &lc->lro_active, next) { 6134e0ce82bSRandall Stewart if (le->m_head != NULL) { 6144e0ce82bSRandall Stewart tcp_lro_active_remove(le); 6154e0ce82bSRandall Stewart tcp_lro_flush(lc, le); 6164e0ce82bSRandall Stewart } 6174e0ce82bSRandall Stewart } 6184e0ce82bSRandall Stewart } 6194e0ce82bSRandall Stewart 6206c5087a8SJack F Vogel void 6217127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 6227127e6acSNavdeep Parhar { 6237127e6acSNavdeep Parhar struct lro_entry *le, *le_tmp; 624b45daaeaSRandall Stewart uint64_t now, tov; 625b45daaeaSRandall Stewart struct bintime bt; 6267127e6acSNavdeep Parhar 627dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 6281ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_active)) 6297127e6acSNavdeep Parhar return; 6307127e6acSNavdeep Parhar 631b45daaeaSRandall Stewart /* get timeout time and current time in ns */ 632b45daaeaSRandall Stewart binuptime(&bt); 633b45daaeaSRandall Stewart now = bintime2ns(&bt); 634b45daaeaSRandall Stewart tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000)); 6351ea44822SSepherosa Ziehau LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 636b45daaeaSRandall Stewart if (now >= (bintime2ns(&le->alloc_time) + tov)) { 63751e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 6387127e6acSNavdeep Parhar tcp_lro_flush(lc, le); 6397127e6acSNavdeep Parhar } 6407127e6acSNavdeep Parhar } 6417127e6acSNavdeep Parhar } 6427127e6acSNavdeep Parhar 643e57b2d0eSRandall Stewart #ifdef INET 644e57b2d0eSRandall Stewart static int 6459ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4) 646e57b2d0eSRandall Stewart { 647e57b2d0eSRandall Stewart uint16_t csum; 648e57b2d0eSRandall Stewart 649e57b2d0eSRandall Stewart /* Legacy IP has a header checksum that needs to be correct. */ 6509ca874cfSHans Petter Selasky if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 6519ca874cfSHans Petter Selasky if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) { 652e57b2d0eSRandall Stewart lc->lro_bad_csum++; 653e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 654e57b2d0eSRandall Stewart } 655e57b2d0eSRandall Stewart } else { 656e57b2d0eSRandall Stewart csum = in_cksum_hdr(ip4); 6579ca874cfSHans Petter Selasky if (__predict_false(csum != 0)) { 658e57b2d0eSRandall Stewart lc->lro_bad_csum++; 659e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 660e57b2d0eSRandall Stewart } 661e57b2d0eSRandall Stewart } 662e57b2d0eSRandall Stewart return (0); 663e57b2d0eSRandall Stewart } 664e57b2d0eSRandall Stewart #endif 665e57b2d0eSRandall Stewart 6669ca874cfSHans Petter Selasky static inline void 6679ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum) 668e57b2d0eSRandall Stewart { 6699ca874cfSHans Petter Selasky uint32_t csum; 6706c5087a8SJack F Vogel 6719ca874cfSHans Petter Selasky csum = 0xffff - *ptr + value; 6729ca874cfSHans Petter Selasky while (csum > 0xffff) 6739ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 6749ca874cfSHans Petter Selasky *ptr = value; 6759ca874cfSHans Petter Selasky *psum = csum; 67662b5b6ecSBjoern A. Zeeb } 67762b5b6ecSBjoern A. Zeeb 6789ca874cfSHans Petter Selasky static uint16_t 6799ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le, 6809ca874cfSHans Petter Selasky uint16_t payload_len, uint16_t delta_sum) 6819ca874cfSHans Petter Selasky { 6829ca874cfSHans Petter Selasky uint32_t csum; 6839ca874cfSHans Petter Selasky uint16_t tlen; 6849ca874cfSHans Petter Selasky uint16_t temp[5] = {}; 6859ca874cfSHans Petter Selasky 6869ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 6879ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 6889ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 6899ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len; 6909ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 6919ca874cfSHans Petter Selasky 6929ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 6939ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 6949ca874cfSHans Petter Selasky while (csum > 0xffff) 6959ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 6969ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 6979ca874cfSHans Petter Selasky goto update_tcp_header; 6989ca874cfSHans Petter Selasky 6999ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 7009ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7019ca874cfSHans Petter Selasky tlen = (pa->tcp->th_off << 2) + payload_len; 7029ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7039ca874cfSHans Petter Selasky goto update_tcp_header; 7049ca874cfSHans Petter Selasky 7059ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 7069ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 7079ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len; 7089ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 7099ca874cfSHans Petter Selasky 7109ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 7119ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 7129ca874cfSHans Petter Selasky while (csum > 0xffff) 7139ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7149ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 7159ca874cfSHans Petter Selasky goto update_udp_header; 7169ca874cfSHans Petter Selasky 7179ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 7189ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7199ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7209ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7219ca874cfSHans Petter Selasky goto update_udp_header; 7229ca874cfSHans Petter Selasky 72362b5b6ecSBjoern A. Zeeb default: 7249ca874cfSHans Petter Selasky return (0); 72562b5b6ecSBjoern A. Zeeb } 7269ca874cfSHans Petter Selasky 7279ca874cfSHans Petter Selasky update_tcp_header: 7289ca874cfSHans Petter Selasky /* Compute current TCP header checksum. */ 7299ca874cfSHans Petter Selasky temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp); 73062b5b6ecSBjoern A. Zeeb 73162b5b6ecSBjoern A. Zeeb /* Incorporate the latest ACK into the TCP header. */ 7329ca874cfSHans Petter Selasky pa->tcp->th_ack = le->ack_seq; 7339ca874cfSHans Petter Selasky pa->tcp->th_win = le->window; 7349ca874cfSHans Petter Selasky 73562b5b6ecSBjoern A. Zeeb /* Incorporate latest timestamp into the TCP header. */ 73662b5b6ecSBjoern A. Zeeb if (le->timestamp != 0) { 7376c5087a8SJack F Vogel uint32_t *ts_ptr; 7386c5087a8SJack F Vogel 7399ca874cfSHans Petter Selasky ts_ptr = (uint32_t *)(pa->tcp + 1); 74062b5b6ecSBjoern A. Zeeb ts_ptr[1] = htonl(le->tsval); 74162b5b6ecSBjoern A. Zeeb ts_ptr[2] = le->tsecr; 74262b5b6ecSBjoern A. Zeeb } 7439ca874cfSHans Petter Selasky 7449ca874cfSHans Petter Selasky /* Compute new TCP header checksum. */ 7459ca874cfSHans Petter Selasky temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp); 7469ca874cfSHans Petter Selasky 7479ca874cfSHans Petter Selasky /* Compute new TCP checksum. */ 7489ca874cfSHans Petter Selasky csum = pa->tcp->th_sum + 0xffff - delta_sum + 7499ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[3] + temp[2]; 7509ca874cfSHans Petter Selasky while (csum > 0xffff) 7519ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7529ca874cfSHans Petter Selasky 7539ca874cfSHans Petter Selasky /* Assign new TCP checksum. */ 7549ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]); 7559ca874cfSHans Petter Selasky 7569ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 7579ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + 0xffff - temp[2] + 7589ca874cfSHans Petter Selasky temp[3] + temp[4] + delta_sum; 7599ca874cfSHans Petter Selasky while (csum > 0xffff) 7609ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7619ca874cfSHans Petter Selasky 7629ca874cfSHans Petter Selasky /* Return delta checksum to next stage, if any. */ 7639ca874cfSHans Petter Selasky return (csum); 7649ca874cfSHans Petter Selasky 7659ca874cfSHans Petter Selasky update_udp_header: 7669ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7679ca874cfSHans Petter Selasky /* Assign new UDP length and compute checksum delta. */ 7689ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]); 7699ca874cfSHans Petter Selasky 7709ca874cfSHans Petter Selasky /* Check if there is a UDP checksum. */ 7719ca874cfSHans Petter Selasky if (__predict_false(pa->udp->uh_sum != 0)) { 7729ca874cfSHans Petter Selasky /* Compute new UDP checksum. */ 7739ca874cfSHans Petter Selasky csum = pa->udp->uh_sum + 0xffff - delta_sum + 7749ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[2]; 7759ca874cfSHans Petter Selasky while (csum > 0xffff) 7769ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7779ca874cfSHans Petter Selasky /* Assign new UDP checksum. */ 7789ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]); 779e57b2d0eSRandall Stewart } 7809ca874cfSHans Petter Selasky 7819ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 7829ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + temp[2] + temp[3] + 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 7909ca874cfSHans Petter Selasky static void 7919ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le) 7929ca874cfSHans Petter Selasky { 7939ca874cfSHans Petter Selasky /* Check if we need to recompute any checksums. */ 79424fe6643SRyan Stone if (le->needs_merge) { 7959ca874cfSHans Petter Selasky uint16_t csum; 7969ca874cfSHans Petter Selasky 7979ca874cfSHans Petter Selasky switch (le->inner.data.lro_type) { 7989ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 7999ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 8009ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8019ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8029ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8039ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8049ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8059ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8069ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 8079ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 80810a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 80910a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8109ca874cfSHans Petter Selasky break; 8119ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8129ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 8139ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8149ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8159ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8169ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8179ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8189ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8199ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR; 8209ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 82110a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 82210a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8239ca874cfSHans Petter Selasky break; 8249ca874cfSHans Petter Selasky case LRO_TYPE_NONE: 8259ca874cfSHans Petter Selasky switch (le->outer.data.lro_type) { 8269ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 8279ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8289ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8299ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8309ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8319ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 8329ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 83310a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 83410a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8359ca874cfSHans Petter Selasky break; 8369ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8379ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8389ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8399ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_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; 84310a62eb1SHans Petter Selasky if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 84410a62eb1SHans Petter Selasky le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 8459ca874cfSHans Petter Selasky break; 8469ca874cfSHans Petter Selasky default: 8479ca874cfSHans Petter Selasky break; 8489ca874cfSHans Petter Selasky } 8499ca874cfSHans Petter Selasky break; 8509ca874cfSHans Petter Selasky default: 8519ca874cfSHans Petter Selasky break; 8529ca874cfSHans Petter Selasky } 8539ca874cfSHans Petter Selasky } 8549ca874cfSHans Petter Selasky 855e57b2d0eSRandall Stewart /* 856e57b2d0eSRandall Stewart * Break any chain, this is not set to NULL on the singleton 857e57b2d0eSRandall Stewart * case m_nextpkt points to m_head. Other case set them 858e57b2d0eSRandall Stewart * m_nextpkt to NULL in push_and_replace. 859e57b2d0eSRandall Stewart */ 860e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 8619ca874cfSHans Petter Selasky lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs; 862e57b2d0eSRandall Stewart (*lc->ifp->if_input)(lc->ifp, le->m_head); 86362b5b6ecSBjoern A. Zeeb } 8646c5087a8SJack F Vogel 865e57b2d0eSRandall Stewart static void 8669ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, 8679ca874cfSHans Petter Selasky struct mbuf *m, struct tcphdr *th) 868e57b2d0eSRandall Stewart { 869e57b2d0eSRandall Stewart uint32_t *ts_ptr; 870e57b2d0eSRandall Stewart uint16_t tcp_data_len; 8719ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 872e57b2d0eSRandall Stewart 873e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 8749ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 8759ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 8769ca874cfSHans Petter Selasky 8779ca874cfSHans Petter Selasky /* Check if there is a timestamp option. */ 8789ca874cfSHans Petter Selasky if (tcp_opt_len == 0 || 8799ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 8809ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 8819ca874cfSHans Petter Selasky /* We failed to find the timestamp option. */ 8829ca874cfSHans Petter Selasky le->timestamp = 0; 8839ca874cfSHans Petter Selasky } else { 884e57b2d0eSRandall Stewart le->timestamp = 1; 885e57b2d0eSRandall Stewart le->tsval = ntohl(*(ts_ptr + 1)); 886e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 8879ca874cfSHans Petter Selasky } 8889ca874cfSHans Petter Selasky 8899ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 8909ca874cfSHans Petter Selasky 8919ca874cfSHans Petter Selasky /* Pull out TCP sequence numbers and window size. */ 892e57b2d0eSRandall Stewart le->next_seq = ntohl(th->th_seq) + tcp_data_len; 893e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 894e57b2d0eSRandall Stewart le->window = th->th_win; 8951ebf4607SRichard Scheffenegger le->flags = tcp_get_flags(th); 89624fe6643SRyan Stone le->needs_merge = 0; 8979ca874cfSHans Petter Selasky 8989ca874cfSHans Petter Selasky /* Setup new data pointers. */ 899e57b2d0eSRandall Stewart le->m_head = m; 900e57b2d0eSRandall Stewart le->m_tail = m_last(m); 901e57b2d0eSRandall Stewart } 902e57b2d0eSRandall Stewart 903e57b2d0eSRandall Stewart static void 9049ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m) 905e57b2d0eSRandall Stewart { 9069ca874cfSHans Petter Selasky struct lro_parser *pa; 9079ca874cfSHans Petter Selasky 908e57b2d0eSRandall Stewart /* 9099ca874cfSHans Petter Selasky * Push up the stack of the current entry 9109ca874cfSHans Petter Selasky * and replace it with "m". 911e57b2d0eSRandall Stewart */ 912e57b2d0eSRandall Stewart struct mbuf *msave; 913e57b2d0eSRandall Stewart 914e57b2d0eSRandall Stewart /* Grab off the next and save it */ 915e57b2d0eSRandall Stewart msave = le->m_head->m_nextpkt; 916e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 9179ca874cfSHans Petter Selasky 9189ca874cfSHans Petter Selasky /* Now push out the old entry */ 9199ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 9209ca874cfSHans Petter Selasky 9219ca874cfSHans Petter Selasky /* Re-parse new header, should not fail. */ 9229ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &le->outer, &le->inner, false); 9239ca874cfSHans Petter Selasky KASSERT(pa != NULL, 9249ca874cfSHans Petter Selasky ("tcp_push_and_replace: LRO parser failed on m=%p\n", m)); 9259ca874cfSHans Petter Selasky 926e57b2d0eSRandall Stewart /* 9279ca874cfSHans Petter Selasky * Now to replace the data properly in the entry 9289ca874cfSHans Petter Selasky * we have to reset the TCP header and 929e57b2d0eSRandall Stewart * other fields. 930e57b2d0eSRandall Stewart */ 9319ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, pa->tcp); 9329ca874cfSHans Petter Selasky 933e57b2d0eSRandall Stewart /* Restore the next list */ 934e57b2d0eSRandall Stewart m->m_nextpkt = msave; 935e57b2d0eSRandall Stewart } 936e57b2d0eSRandall Stewart 937e57b2d0eSRandall Stewart static void 93824fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p) 9399ca874cfSHans Petter Selasky { 94024fe6643SRyan Stone struct mbuf *m; 9419ca874cfSHans Petter Selasky uint32_t csum; 9429ca874cfSHans Petter Selasky 94324fe6643SRyan Stone m = le->m_head; 9449ca874cfSHans Petter Selasky if (m->m_pkthdr.lro_nsegs == 1) { 9459ca874cfSHans Petter Selasky /* Compute relative checksum. */ 9469ca874cfSHans Petter Selasky csum = p->m_pkthdr.lro_tcp_d_csum; 9479ca874cfSHans Petter Selasky } else { 9489ca874cfSHans Petter Selasky /* Merge TCP data checksums. */ 9499ca874cfSHans Petter Selasky csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum + 9509ca874cfSHans Petter Selasky (uint32_t)p->m_pkthdr.lro_tcp_d_csum; 9519ca874cfSHans Petter Selasky while (csum > 0xffff) 9529ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 9539ca874cfSHans Petter Selasky } 9549ca874cfSHans Petter Selasky 9559ca874cfSHans Petter Selasky /* Update various counters. */ 9569ca874cfSHans Petter Selasky m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len; 9579ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = csum; 9589ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len; 9599ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs; 96024fe6643SRyan Stone le->needs_merge = 1; 9619ca874cfSHans Petter Selasky } 9629ca874cfSHans Petter Selasky 9639ca874cfSHans Petter Selasky static void 9649ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le) 965e57b2d0eSRandall Stewart { 966e57b2d0eSRandall Stewart /* 967e57b2d0eSRandall Stewart * Walk through the mbuf chain we 968e57b2d0eSRandall Stewart * have on tap and compress/condense 969e57b2d0eSRandall Stewart * as required. 970e57b2d0eSRandall Stewart */ 971e57b2d0eSRandall Stewart uint32_t *ts_ptr; 972e57b2d0eSRandall Stewart struct mbuf *m; 973e57b2d0eSRandall Stewart struct tcphdr *th; 9749ca874cfSHans Petter Selasky uint32_t tcp_data_len_total; 9759ca874cfSHans Petter Selasky uint32_t tcp_data_seg_total; 9769ca874cfSHans Petter Selasky uint16_t tcp_data_len; 9779ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 978e57b2d0eSRandall Stewart 979e57b2d0eSRandall Stewart /* 980e57b2d0eSRandall Stewart * First we must check the lead (m_head) 981e57b2d0eSRandall Stewart * we must make sure that it is *not* 982e57b2d0eSRandall Stewart * something that should be sent up 983e57b2d0eSRandall Stewart * right away (sack etc). 984e57b2d0eSRandall Stewart */ 985e57b2d0eSRandall Stewart again: 986e57b2d0eSRandall Stewart m = le->m_head->m_nextpkt; 987e57b2d0eSRandall Stewart if (m == NULL) { 9889ca874cfSHans Petter Selasky /* Just one left. */ 989e57b2d0eSRandall Stewart return; 990e57b2d0eSRandall Stewart } 9919ca874cfSHans Petter Selasky 9929ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 9939ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 9949ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 995e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 9969ca874cfSHans Petter Selasky 9979ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 9989ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 999e57b2d0eSRandall Stewart /* 1000e57b2d0eSRandall Stewart * Its not the timestamp. We can't 1001e57b2d0eSRandall Stewart * use this guy as the head. 1002e57b2d0eSRandall Stewart */ 1003e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10049ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1005e57b2d0eSRandall Stewart goto again; 1006e57b2d0eSRandall Stewart } 10071ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 1008e57b2d0eSRandall Stewart /* 10091dadeab3SGordon Bergling * Make sure that previously seen segments/ACKs are delivered 1010e57b2d0eSRandall Stewart * before this segment, e.g. FIN. 1011e57b2d0eSRandall Stewart */ 1012e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10139ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1014e57b2d0eSRandall Stewart goto again; 1015e57b2d0eSRandall Stewart } 1016e57b2d0eSRandall Stewart while((m = le->m_head->m_nextpkt) != NULL) { 1017e57b2d0eSRandall Stewart /* 1018e57b2d0eSRandall Stewart * condense m into le, first 1019e57b2d0eSRandall Stewart * pull m out of the list. 1020e57b2d0eSRandall Stewart */ 1021e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 1022e57b2d0eSRandall Stewart m->m_nextpkt = NULL; 1023e57b2d0eSRandall Stewart /* Setup my data */ 10249ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 10259ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 1026e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 10279ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 10289ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 10299ca874cfSHans Petter Selasky tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len; 10309ca874cfSHans Petter Selasky tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs; 10319ca874cfSHans Petter Selasky 10329ca874cfSHans Petter Selasky if (tcp_data_seg_total >= lc->lro_ackcnt_lim || 10339ca874cfSHans Petter Selasky tcp_data_len_total >= lc->lro_length_lim) { 1034e57b2d0eSRandall Stewart /* Flush now if appending will result in overflow. */ 10359ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1036e57b2d0eSRandall Stewart goto again; 1037e57b2d0eSRandall Stewart } 10389ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && 10399ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 10409ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 1041e57b2d0eSRandall Stewart /* 1042e57b2d0eSRandall Stewart * Maybe a sack in the new one? We need to 1043e57b2d0eSRandall Stewart * start all over after flushing the 1044e57b2d0eSRandall Stewart * current le. We will go up to the beginning 1045e57b2d0eSRandall Stewart * and flush it (calling the replace again possibly 1046e57b2d0eSRandall Stewart * or just returning). 1047e57b2d0eSRandall Stewart */ 10489ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1049e57b2d0eSRandall Stewart goto again; 1050e57b2d0eSRandall Stewart } 10511ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 10529ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1053e57b2d0eSRandall Stewart goto again; 1054e57b2d0eSRandall Stewart } 10559ca874cfSHans Petter Selasky if (tcp_opt_len != 0) { 1056e57b2d0eSRandall Stewart uint32_t tsval = ntohl(*(ts_ptr + 1)); 1057e57b2d0eSRandall Stewart /* Make sure timestamp values are increasing. */ 1058e57b2d0eSRandall Stewart if (TSTMP_GT(le->tsval, tsval)) { 10599ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1060e57b2d0eSRandall Stewart goto again; 1061e57b2d0eSRandall Stewart } 1062e57b2d0eSRandall Stewart le->tsval = tsval; 1063e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 1064e57b2d0eSRandall Stewart } 1065e57b2d0eSRandall Stewart /* Try to append the new segment. */ 1066e57b2d0eSRandall Stewart if (__predict_false(ntohl(th->th_seq) != le->next_seq || 10671ebf4607SRichard Scheffenegger ((tcp_get_flags(th) & TH_ACK) != 10683284f492SRyan Stone (le->flags & TH_ACK)) || 1069e57b2d0eSRandall Stewart (tcp_data_len == 0 && 1070e57b2d0eSRandall Stewart le->ack_seq == th->th_ack && 1071e57b2d0eSRandall Stewart le->window == th->th_win))) { 10723284f492SRyan Stone /* Out of order packet, non-ACK + ACK or dup ACK. */ 10739ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1074e57b2d0eSRandall Stewart goto again; 1075e57b2d0eSRandall Stewart } 10769ca874cfSHans Petter Selasky if (tcp_data_len != 0 || 10779ca874cfSHans Petter Selasky SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1078e57b2d0eSRandall Stewart le->next_seq += tcp_data_len; 1079e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 1080e57b2d0eSRandall Stewart le->window = th->th_win; 108124fe6643SRyan Stone le->needs_merge = 1; 1082e57b2d0eSRandall Stewart } else if (th->th_ack == le->ack_seq) { 108324fe6643SRyan Stone if (WIN_GT(th->th_win, le->window)) { 108424fe6643SRyan Stone le->window = th->th_win; 108524fe6643SRyan Stone le->needs_merge = 1; 108624fe6643SRyan Stone } 1087e57b2d0eSRandall Stewart } 10889ca874cfSHans Petter Selasky 1089e57b2d0eSRandall Stewart if (tcp_data_len == 0) { 1090e57b2d0eSRandall Stewart m_freem(m); 1091e57b2d0eSRandall Stewart continue; 1092e57b2d0eSRandall Stewart } 10939ca874cfSHans Petter Selasky 10949ca874cfSHans Petter Selasky /* Merge TCP data checksum and length to head mbuf. */ 109524fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(le, m); 10969ca874cfSHans Petter Selasky 1097e57b2d0eSRandall Stewart /* 1098e57b2d0eSRandall Stewart * Adjust the mbuf so that m_data points to the first byte of 1099e57b2d0eSRandall Stewart * the ULP payload. Adjust the mbuf to avoid complications and 1100e57b2d0eSRandall Stewart * append new segment to existing mbuf chain. 1101e57b2d0eSRandall Stewart */ 1102e57b2d0eSRandall Stewart m_adj(m, m->m_pkthdr.len - tcp_data_len); 1103e57b2d0eSRandall Stewart m_demote_pkthdr(m); 1104e57b2d0eSRandall Stewart le->m_tail->m_next = m; 1105e57b2d0eSRandall Stewart le->m_tail = m_last(m); 1106e57b2d0eSRandall Stewart } 1107e57b2d0eSRandall Stewart } 1108e57b2d0eSRandall Stewart 110969a34e8dSRandall Stewart void 111069a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 111169a34e8dSRandall Stewart { 11129ca874cfSHans Petter Selasky /* Only optimise if there are multiple packets waiting. */ 111369a34e8dSRandall Stewart #ifdef TCPHPTS 11149ca874cfSHans Petter Selasky int error; 1115dc6ab77dSMichael Tuexen #endif 111669a34e8dSRandall Stewart 1117dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 1118dc6ab77dSMichael Tuexen #ifdef TCPHPTS 111969a34e8dSRandall Stewart CURVNET_SET(lc->ifp->if_vnet); 11209ca874cfSHans Petter Selasky error = tcp_lro_flush_tcphpts(lc, le); 11219ca874cfSHans Petter Selasky CURVNET_RESTORE(); 11229ca874cfSHans Petter Selasky if (error != 0) { 11239ca874cfSHans Petter Selasky #endif 11249ca874cfSHans Petter Selasky tcp_lro_condense(lc, le); 11259ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 1126e57b2d0eSRandall Stewart #ifdef TCPHPTS 1127e57b2d0eSRandall Stewart } 1128e57b2d0eSRandall Stewart #endif 112962b5b6ecSBjoern A. Zeeb lc->lro_flushed++; 113062b5b6ecSBjoern A. Zeeb bzero(le, sizeof(*le)); 11311ea44822SSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_free, le, next); 113262b5b6ecSBjoern A. Zeeb } 11336c5087a8SJack F Vogel 1134fc271df3SHans Petter Selasky #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1)) 1135e936121dSHans Petter Selasky 1136fc271df3SHans Petter Selasky /* 1137fc271df3SHans Petter Selasky * The tcp_lro_sort() routine is comparable to qsort(), except it has 1138fc271df3SHans Petter Selasky * a worst case complexity limit of O(MIN(N,64)*N), where N is the 1139fc271df3SHans Petter Selasky * number of elements to sort and 64 is the number of sequence bits 1140fc271df3SHans Petter Selasky * available. The algorithm is bit-slicing the 64-bit sequence number, 1141fc271df3SHans Petter Selasky * sorting one bit at a time from the most significant bit until the 1142ec668905SHans Petter Selasky * least significant one, skipping the constant bits. This is 1143ec668905SHans Petter Selasky * typically called a radix sort. 1144fc271df3SHans Petter Selasky */ 1145fc271df3SHans Petter Selasky static void 1146fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size) 1147fc271df3SHans Petter Selasky { 1148fc271df3SHans Petter Selasky struct lro_mbuf_sort temp; 1149fc271df3SHans Petter Selasky uint64_t ones; 1150fc271df3SHans Petter Selasky uint64_t zeros; 1151fc271df3SHans Petter Selasky uint32_t x; 1152fc271df3SHans Petter Selasky uint32_t y; 1153e936121dSHans Petter Selasky 1154fc271df3SHans Petter Selasky repeat: 1155ec668905SHans Petter Selasky /* for small arrays insertion sort is faster */ 1156fc271df3SHans Petter Selasky if (size <= 12) { 1157ec668905SHans Petter Selasky for (x = 1; x < size; x++) { 1158fc271df3SHans Petter Selasky temp = parray[x]; 1159ec668905SHans Petter Selasky for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--) 1160ec668905SHans Petter Selasky parray[y] = parray[y - 1]; 1161fc271df3SHans Petter Selasky parray[y] = temp; 1162fc271df3SHans Petter Selasky } 1163fc271df3SHans Petter Selasky return; 1164fc271df3SHans Petter Selasky } 1165e936121dSHans Petter Selasky 1166fc271df3SHans Petter Selasky /* compute sequence bits which are constant */ 1167fc271df3SHans Petter Selasky ones = 0; 1168fc271df3SHans Petter Selasky zeros = 0; 1169fc271df3SHans Petter Selasky for (x = 0; x != size; x++) { 1170fc271df3SHans Petter Selasky ones |= parray[x].seq; 1171fc271df3SHans Petter Selasky zeros |= ~parray[x].seq; 1172fc271df3SHans Petter Selasky } 1173fc271df3SHans Petter Selasky 1174fc271df3SHans Petter Selasky /* compute bits which are not constant into "ones" */ 1175fc271df3SHans Petter Selasky ones &= zeros; 1176fc271df3SHans Petter Selasky if (ones == 0) 1177fc271df3SHans Petter Selasky return; 1178fc271df3SHans Petter Selasky 1179fc271df3SHans Petter Selasky /* pick the most significant bit which is not constant */ 1180fc271df3SHans Petter Selasky ones = tcp_lro_msb_64(ones); 1181fc271df3SHans Petter Selasky 1182fc271df3SHans Petter Selasky /* 1183fc271df3SHans Petter Selasky * Move entries having cleared sequence bits to the beginning 1184fc271df3SHans Petter Selasky * of the array: 1185fc271df3SHans Petter Selasky */ 1186fc271df3SHans Petter Selasky for (x = y = 0; y != size; y++) { 1187fc271df3SHans Petter Selasky /* skip set bits */ 1188fc271df3SHans Petter Selasky if (parray[y].seq & ones) 1189fc271df3SHans Petter Selasky continue; 1190fc271df3SHans Petter Selasky /* swap entries */ 1191fc271df3SHans Petter Selasky temp = parray[x]; 1192fc271df3SHans Petter Selasky parray[x] = parray[y]; 1193fc271df3SHans Petter Selasky parray[y] = temp; 1194fc271df3SHans Petter Selasky x++; 1195fc271df3SHans Petter Selasky } 1196fc271df3SHans Petter Selasky 1197fc271df3SHans Petter Selasky KASSERT(x != 0 && x != size, ("Memory is corrupted\n")); 1198fc271df3SHans Petter Selasky 1199fc271df3SHans Petter Selasky /* sort zeros */ 1200fc271df3SHans Petter Selasky tcp_lro_sort(parray, x); 1201fc271df3SHans Petter Selasky 1202fc271df3SHans Petter Selasky /* sort ones */ 1203fc271df3SHans Petter Selasky parray += x; 1204fc271df3SHans Petter Selasky size -= x; 1205fc271df3SHans Petter Selasky goto repeat; 1206e936121dSHans Petter Selasky } 1207e936121dSHans Petter Selasky 1208e936121dSHans Petter Selasky void 1209e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc) 1210e936121dSHans Petter Selasky { 1211fc271df3SHans Petter Selasky uint64_t seq; 1212fc271df3SHans Petter Selasky uint64_t nseq; 1213e936121dSHans Petter Selasky unsigned x; 1214e936121dSHans Petter Selasky 1215dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 1216e936121dSHans Petter Selasky /* check if no mbufs to flush */ 12176dd38b87SSepherosa Ziehau if (lc->lro_mbuf_count == 0) 1218e936121dSHans Petter Selasky goto done; 1219d7955cc0SRandall Stewart if (lc->lro_cpu_is_set == 0) { 1220d7955cc0SRandall Stewart if (lc->lro_last_cpu == curcpu) { 1221d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu++; 1222d7955cc0SRandall Stewart /* Have we reached the threshold to declare a cpu? */ 1223d7955cc0SRandall Stewart if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh) 1224d7955cc0SRandall Stewart lc->lro_cpu_is_set = 1; 1225d7955cc0SRandall Stewart } else { 1226d7955cc0SRandall Stewart lc->lro_last_cpu = curcpu; 1227d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu = 0; 1228d7955cc0SRandall Stewart } 1229d7955cc0SRandall Stewart } 1230a9b66dbdSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 1231a9b66dbdSHans Petter Selasky 12329ca874cfSHans Petter Selasky /* get current time */ 1233b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 12349ca874cfSHans Petter Selasky 1235e936121dSHans Petter Selasky /* sort all mbufs according to stream */ 1236fc271df3SHans Petter Selasky tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count); 1237e936121dSHans Petter Selasky 1238e936121dSHans Petter Selasky /* input data into LRO engine, stream by stream */ 1239fc271df3SHans Petter Selasky seq = 0; 1240e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) { 1241e936121dSHans Petter Selasky struct mbuf *mb; 1242e936121dSHans Petter Selasky 1243fc271df3SHans Petter Selasky /* get mbuf */ 1244fc271df3SHans Petter Selasky mb = lc->lro_mbuf_data[x].mb; 1245fc271df3SHans Petter Selasky 1246fc271df3SHans Petter Selasky /* get sequence number, masking away the packet index */ 1247fc271df3SHans Petter Selasky nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24); 1248e936121dSHans Petter Selasky 1249e936121dSHans Petter Selasky /* check for new stream */ 1250fc271df3SHans Petter Selasky if (seq != nseq) { 1251fc271df3SHans Petter Selasky seq = nseq; 1252e936121dSHans Petter Selasky 1253e936121dSHans Petter Selasky /* flush active streams */ 12546dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 1255e936121dSHans Petter Selasky } 1256fc271df3SHans Petter Selasky 1257e936121dSHans Petter Selasky /* add packet to LRO engine */ 12589ca874cfSHans Petter Selasky if (tcp_lro_rx_common(lc, mb, 0, false) != 0) { 12594e0ce82bSRandall Stewart /* Flush anything we have acummulated */ 12604e0ce82bSRandall Stewart tcp_lro_flush_active(lc); 1261e936121dSHans Petter Selasky /* input packet to network layer */ 1262e936121dSHans Petter Selasky (*lc->ifp->if_input)(lc->ifp, mb); 1263e936121dSHans Petter Selasky lc->lro_queued++; 1264e936121dSHans Petter Selasky lc->lro_flushed++; 1265e936121dSHans Petter Selasky } 1266e936121dSHans Petter Selasky } 1267a9b66dbdSHans Petter Selasky CURVNET_RESTORE(); 1268e936121dSHans Petter Selasky done: 1269e936121dSHans Petter Selasky /* flush active streams */ 12706dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 12716dd38b87SSepherosa Ziehau 1272d7955cc0SRandall Stewart #ifdef TCPHPTS 1273d7955cc0SRandall Stewart tcp_run_hpts(); 1274d7955cc0SRandall Stewart #endif 1275e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 127662b5b6ecSBjoern A. Zeeb } 12776c5087a8SJack F Vogel 12789ca874cfSHans Petter Selasky static struct lro_head * 12799ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser) 12809ca874cfSHans Petter Selasky { 12819ca874cfSHans Petter Selasky u_long hash; 12829ca874cfSHans Petter Selasky 12839ca874cfSHans Petter Selasky if (M_HASHTYPE_ISHASH(m)) { 12849ca874cfSHans Petter Selasky hash = m->m_pkthdr.flowid; 12859ca874cfSHans Petter Selasky } else { 12869ca874cfSHans Petter Selasky for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++) 12879ca874cfSHans Petter Selasky hash += parser->data.raw[i]; 128869a34e8dSRandall Stewart } 12899ca874cfSHans Petter Selasky return (&lc->lro_hash[hash % lc->lro_hashsz]); 12909ca874cfSHans Petter Selasky } 129169a34e8dSRandall Stewart 129205cde7efSSepherosa Ziehau static int 12939ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash) 129462b5b6ecSBjoern A. Zeeb { 12959ca874cfSHans Petter Selasky struct lro_parser pi; /* inner address data */ 12969ca874cfSHans Petter Selasky struct lro_parser po; /* outer address data */ 12979ca874cfSHans Petter Selasky struct lro_parser *pa; /* current parser for TCP stream */ 129862b5b6ecSBjoern A. Zeeb struct lro_entry *le; 129905cde7efSSepherosa Ziehau struct lro_head *bucket; 13009ca874cfSHans Petter Selasky struct tcphdr *th; 13019ca874cfSHans Petter Selasky int tcp_data_len; 13029ca874cfSHans Petter Selasky int tcp_opt_len; 13039ca874cfSHans Petter Selasky int error; 13049ca874cfSHans Petter Selasky uint16_t tcp_data_sum; 13056c5087a8SJack F Vogel 13069ca874cfSHans Petter Selasky #ifdef INET 13079ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 13089ca874cfSHans Petter Selasky if (__predict_false(V_ipforwarding != 0)) 13099ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 13109ca874cfSHans Petter Selasky #endif 13119ca874cfSHans Petter Selasky #ifdef INET6 13129ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 13139ca874cfSHans Petter Selasky if (__predict_false(V_ip6_forwarding != 0)) 13149ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 13159ca874cfSHans Petter Selasky #endif 1316ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1317ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1318ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1319ca1a7e10SRandall Stewart /* 1320ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1321ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1322ca1a7e10SRandall Stewart * a packet. 1323ca1a7e10SRandall Stewart */ 1324ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1325ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1326ca1a7e10SRandall Stewart } 132762b5b6ecSBjoern A. Zeeb /* We expect a contiguous header [eh, ip, tcp]. */ 13289ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &po, &pi, true); 13299ca874cfSHans Petter Selasky if (__predict_false(pa == NULL)) 13309ca874cfSHans Petter Selasky return (TCP_LRO_NOT_SUPPORTED); 13319ca874cfSHans Petter Selasky 13329ca874cfSHans Petter Selasky /* We don't expect any padding. */ 13339ca874cfSHans Petter Selasky error = tcp_lro_trim_mbuf_chain(m, pa); 13349ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 13359ca874cfSHans Petter Selasky return (error); 13369ca874cfSHans Petter Selasky 13379ca874cfSHans Petter Selasky #ifdef INET 13389ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 13399ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 13409ca874cfSHans Petter Selasky error = tcp_lro_rx_ipv4(lc, m, pa->ip4); 13419ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 13429ca874cfSHans Petter Selasky return (error); 13439ca874cfSHans Petter Selasky break; 13449ca874cfSHans Petter Selasky default: 13459ca874cfSHans Petter Selasky break; 13469ca874cfSHans Petter Selasky } 13479ca874cfSHans Petter Selasky #endif 13489ca874cfSHans Petter Selasky /* If no hardware or arrival stamp on the packet add timestamp */ 1349e57b2d0eSRandall Stewart if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) { 1350b45daaeaSRandall Stewart m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1351e57b2d0eSRandall Stewart m->m_flags |= M_TSTMP_LRO; 1352e57b2d0eSRandall Stewart } 13536c5087a8SJack F Vogel 13549ca874cfSHans Petter Selasky /* Get pointer to TCP header. */ 13559ca874cfSHans Petter Selasky th = pa->tcp; 13569ca874cfSHans Petter Selasky 13579ca874cfSHans Petter Selasky /* Don't process SYN packets. */ 13581ebf4607SRichard Scheffenegger if (__predict_false(tcp_get_flags(th) & TH_SYN)) 135962b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 136062b5b6ecSBjoern A. Zeeb 13619ca874cfSHans Petter Selasky /* Get total TCP header length and compute payload length. */ 13629ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 13639ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th - 13649ca874cfSHans Petter Selasky (uint8_t *)m->m_data) - tcp_opt_len; 13659ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 13669ca874cfSHans Petter Selasky 13679ca874cfSHans Petter Selasky /* Don't process invalid TCP headers. */ 13689ca874cfSHans Petter Selasky if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0)) 136962b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 13709ca874cfSHans Petter Selasky 13719ca874cfSHans Petter Selasky /* Compute TCP data only checksum. */ 13729ca874cfSHans Petter Selasky if (tcp_data_len == 0) 13739ca874cfSHans Petter Selasky tcp_data_sum = 0; /* no data, no checksum */ 13749ca874cfSHans Petter Selasky else if (__predict_false(csum != 0)) 13759ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum); 13769ca874cfSHans Petter Selasky else 13779ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum); 13789ca874cfSHans Petter Selasky 13799ca874cfSHans Petter Selasky /* Save TCP info in mbuf. */ 13809ca874cfSHans Petter Selasky m->m_nextpkt = NULL; 13819ca874cfSHans Petter Selasky m->m_pkthdr.rcvif = lc->ifp; 13829ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum; 13839ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len = tcp_data_len; 13849ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data); 13859ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs = 1; 13869ca874cfSHans Petter Selasky 13879ca874cfSHans Petter Selasky /* Get hash bucket. */ 138805cde7efSSepherosa Ziehau if (!use_hash) { 138905cde7efSSepherosa Ziehau bucket = &lc->lro_hash[0]; 139005cde7efSSepherosa Ziehau } else { 13919ca874cfSHans Petter Selasky bucket = tcp_lro_rx_get_bucket(lc, m, pa); 139205cde7efSSepherosa Ziehau } 139305cde7efSSepherosa Ziehau 139462b5b6ecSBjoern A. Zeeb /* Try to find a matching previous segment. */ 139505cde7efSSepherosa Ziehau LIST_FOREACH(le, bucket, hash_next) { 13969ca874cfSHans Petter Selasky /* Compare addresses and ports. */ 13979ca874cfSHans Petter Selasky if (lro_address_compare(&po.data, &le->outer.data) == false || 13989ca874cfSHans Petter Selasky lro_address_compare(&pi.data, &le->inner.data) == false) 139962b5b6ecSBjoern A. Zeeb continue; 14009ca874cfSHans Petter Selasky 14019ca874cfSHans Petter Selasky /* Check if no data and old ACK. */ 14029ca874cfSHans Petter Selasky if (tcp_data_len == 0 && 14039ca874cfSHans Petter Selasky SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1404d7fb35d1SSean Bruno m_freem(m); 1405d7fb35d1SSean Bruno return (0); 1406d7fb35d1SSean Bruno } 140769a34e8dSRandall Stewart 14089ca874cfSHans Petter Selasky /* Mark "m" in the last spot. */ 1409e57b2d0eSRandall Stewart le->m_last_mbuf->m_nextpkt = m; 14109ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1411e57b2d0eSRandall Stewart le->m_last_mbuf = m; 141262b5b6ecSBjoern A. Zeeb return (0); 14136c5087a8SJack F Vogel } 14149ca874cfSHans Petter Selasky 141562b5b6ecSBjoern A. Zeeb /* Try to find an empty slot. */ 14161ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_free)) 1417489f0c3cSSepherosa Ziehau return (TCP_LRO_NO_ENTRIES); 141862b5b6ecSBjoern A. Zeeb 141962b5b6ecSBjoern A. Zeeb /* Start a new segment chain. */ 14201ea44822SSepherosa Ziehau le = LIST_FIRST(&lc->lro_free); 14211ea44822SSepherosa Ziehau LIST_REMOVE(le, next); 142205cde7efSSepherosa Ziehau tcp_lro_active_insert(lc, bucket, le); 142362b5b6ecSBjoern A. Zeeb 14249ca874cfSHans Petter Selasky /* Make sure the headers are set. */ 14259ca874cfSHans Petter Selasky le->inner = pi; 14269ca874cfSHans Petter Selasky le->outer = po; 142762b5b6ecSBjoern A. Zeeb 14289ca874cfSHans Petter Selasky /* Store time this entry was allocated. */ 14299ca874cfSHans Petter Selasky le->alloc_time = lc->lro_last_queue_time; 143069a34e8dSRandall Stewart 14319ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, th); 143269a34e8dSRandall Stewart 14339ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1434e57b2d0eSRandall Stewart le->m_last_mbuf = m; 14359ca874cfSHans Petter Selasky 143662b5b6ecSBjoern A. Zeeb return (0); 143762b5b6ecSBjoern A. Zeeb } 143862b5b6ecSBjoern A. Zeeb 143905cde7efSSepherosa Ziehau int 144005cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 144105cde7efSSepherosa Ziehau { 14429ca874cfSHans Petter Selasky int error; 144305cde7efSSepherosa Ziehau 1444ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1445ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1446ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1447ca1a7e10SRandall Stewart /* 1448ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1449ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1450ca1a7e10SRandall Stewart * a packet. 1451ca1a7e10SRandall Stewart */ 1452ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1453ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1454ca1a7e10SRandall Stewart } 14559ca874cfSHans Petter Selasky /* get current time */ 1456b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 14579ca874cfSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 14589ca874cfSHans Petter Selasky error = tcp_lro_rx_common(lc, m, csum, true); 14594e0ce82bSRandall Stewart if (__predict_false(error != 0)) { 14604e0ce82bSRandall Stewart /* 14614e0ce82bSRandall Stewart * Flush anything we have acummulated 14624e0ce82bSRandall Stewart * ahead of this packet that can't 14634e0ce82bSRandall Stewart * be LRO'd. This preserves order. 14644e0ce82bSRandall Stewart */ 14654e0ce82bSRandall Stewart tcp_lro_flush_active(lc); 14664e0ce82bSRandall Stewart } 14679ca874cfSHans Petter Selasky CURVNET_RESTORE(); 14689ca874cfSHans Petter Selasky 14699ca874cfSHans Petter Selasky return (error); 147005cde7efSSepherosa Ziehau } 147105cde7efSSepherosa Ziehau 1472e936121dSHans Petter Selasky void 1473e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 1474e936121dSHans Petter Selasky { 1475dc6ab77dSMichael Tuexen NET_EPOCH_ASSERT(); 1476e936121dSHans Petter Selasky /* sanity checks */ 1477e936121dSHans Petter Selasky if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 1478e936121dSHans Petter Selasky lc->lro_mbuf_max == 0)) { 1479e936121dSHans Petter Selasky /* packet drop */ 1480e936121dSHans Petter Selasky m_freem(mb); 1481e936121dSHans Petter Selasky return; 1482e936121dSHans Petter Selasky } 1483e936121dSHans Petter Selasky 1484e936121dSHans Petter Selasky /* check if packet is not LRO capable */ 1485ca1a7e10SRandall Stewart if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 1486e936121dSHans Petter Selasky /* input packet to network layer */ 1487e936121dSHans Petter Selasky (*lc->ifp->if_input) (lc->ifp, mb); 1488e936121dSHans Petter Selasky return; 1489e936121dSHans Petter Selasky } 1490e936121dSHans Petter Selasky 14914e0ce82bSRandall Stewart /* If no hardware or arrival stamp on the packet add timestamp */ 14924e0ce82bSRandall Stewart if ((tcplro_stacks_wanting_mbufq > 0) && 14934e0ce82bSRandall Stewart (tcp_less_accurate_lro_ts == 0) && 14944e0ce82bSRandall Stewart ((mb->m_flags & M_TSTMP) == 0)) { 14954e0ce82bSRandall Stewart /* Add in an LRO time since no hardware */ 14964e0ce82bSRandall Stewart binuptime(&lc->lro_last_queue_time); 14974e0ce82bSRandall Stewart mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 14984e0ce82bSRandall Stewart mb->m_flags |= M_TSTMP_LRO; 14994e0ce82bSRandall Stewart } 15004e0ce82bSRandall Stewart 1501fc271df3SHans Petter Selasky /* create sequence number */ 1502fc271df3SHans Petter Selasky lc->lro_mbuf_data[lc->lro_mbuf_count].seq = 1503fc271df3SHans Petter Selasky (((uint64_t)M_HASHTYPE_GET(mb)) << 56) | 1504fc271df3SHans Petter Selasky (((uint64_t)mb->m_pkthdr.flowid) << 24) | 1505fc271df3SHans Petter Selasky ((uint64_t)lc->lro_mbuf_count); 1506e936121dSHans Petter Selasky 1507e936121dSHans Petter Selasky /* enter mbuf */ 1508f8acc03eSNavdeep Parhar lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb; 1509f8acc03eSNavdeep Parhar 1510f8acc03eSNavdeep Parhar /* flush if array is full */ 1511f8acc03eSNavdeep Parhar if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max)) 1512f8acc03eSNavdeep Parhar tcp_lro_flush_all(lc); 1513e936121dSHans Petter Selasky } 1514e936121dSHans Petter Selasky 151562b5b6ecSBjoern A. Zeeb /* end */ 1516