1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007, Myricom Inc. 5 * Copyright (c) 2008, Intel Corporation. 6 * Copyright (c) 2012 The FreeBSD Foundation 7 * Copyright (c) 2016-2021 Mellanox Technologies. 8 * All rights reserved. 9 * 10 * Portions of this software were developed by Bjoern Zeeb 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/sockbuf.h> 47 #include <sys/sysctl.h> 48 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/ethernet.h> 52 #include <net/bpf.h> 53 #include <net/vnet.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 #include <net/if_private.h> 57 #include <net/if_types.h> 58 #include <net/infiniband.h> 59 #include <net/if_lagg.h> 60 61 #include <netinet/in_systm.h> 62 #include <netinet/in.h> 63 #include <netinet/ip6.h> 64 #include <netinet/ip.h> 65 #include <netinet/ip_var.h> 66 #include <netinet/in_pcb.h> 67 #include <netinet6/in6_pcb.h> 68 #include <netinet/tcp.h> 69 #include <netinet/tcp_seq.h> 70 #include <netinet/tcp_lro.h> 71 #include <netinet/tcp_var.h> 72 #include <netinet/tcpip.h> 73 #include <netinet/tcp_hpts.h> 74 #include <netinet/tcp_log_buf.h> 75 #include <netinet/tcp_fsm.h> 76 #include <netinet/udp.h> 77 #include <netinet6/ip6_var.h> 78 79 #include <machine/in_cksum.h> 80 81 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures"); 82 83 static void tcp_lro_rx_done(struct lro_ctrl *lc); 84 static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, 85 uint32_t csum, bool use_hash); 86 static void tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le); 87 88 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 89 "TCP LRO"); 90 91 long tcplro_stacks_wanting_mbufq; 92 int (*tcp_lro_flush_tcphpts)(struct lro_ctrl *lc, struct lro_entry *le); 93 94 counter_u64_t tcp_inp_lro_direct_queue; 95 counter_u64_t tcp_inp_lro_wokeup_queue; 96 counter_u64_t tcp_inp_lro_compressed; 97 counter_u64_t tcp_inp_lro_locks_taken; 98 counter_u64_t tcp_extra_mbuf; 99 counter_u64_t tcp_would_have_but; 100 counter_u64_t tcp_comp_total; 101 counter_u64_t tcp_uncomp_total; 102 counter_u64_t tcp_bad_csums; 103 104 static unsigned tcp_lro_entries = TCP_LRO_ENTRIES; 105 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, 106 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0, 107 "default number of LRO entries"); 108 109 static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH; 110 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold, 111 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0, 112 "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?"); 113 114 static uint32_t tcp_less_accurate_lro_ts = 0; 115 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate, 116 CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0, 117 "Do we trade off efficency by doing less timestamp operations for time accuracy?"); 118 119 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD, 120 &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport"); 121 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD, 122 &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts"); 123 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD, 124 &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport"); 125 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD, 126 &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken"); 127 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD, 128 &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp"); 129 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD, 130 &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed"); 131 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD, 132 &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set"); 133 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD, 134 &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP"); 135 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD, 136 &tcp_bad_csums, "Number of packets that the common code saw with bad csums"); 137 138 void 139 tcp_lro_reg_mbufq(void) 140 { 141 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1); 142 } 143 144 void 145 tcp_lro_dereg_mbufq(void) 146 { 147 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1); 148 } 149 150 static __inline void 151 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket, 152 struct lro_entry *le) 153 { 154 155 LIST_INSERT_HEAD(&lc->lro_active, le, next); 156 LIST_INSERT_HEAD(bucket, le, hash_next); 157 } 158 159 static __inline void 160 tcp_lro_active_remove(struct lro_entry *le) 161 { 162 163 LIST_REMOVE(le, next); /* active list */ 164 LIST_REMOVE(le, hash_next); /* hash bucket */ 165 } 166 167 int 168 tcp_lro_init(struct lro_ctrl *lc) 169 { 170 return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0)); 171 } 172 173 int 174 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, 175 unsigned lro_entries, unsigned lro_mbufs) 176 { 177 struct lro_entry *le; 178 size_t size; 179 unsigned i; 180 181 lc->lro_bad_csum = 0; 182 lc->lro_queued = 0; 183 lc->lro_flushed = 0; 184 lc->lro_mbuf_count = 0; 185 lc->lro_mbuf_max = lro_mbufs; 186 lc->lro_cnt = lro_entries; 187 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX; 188 lc->lro_length_lim = TCP_LRO_LENGTH_MAX; 189 lc->ifp = ifp; 190 LIST_INIT(&lc->lro_free); 191 LIST_INIT(&lc->lro_active); 192 193 /* create hash table to accelerate entry lookup */ 194 lc->lro_hash = phashinit_flags(lro_entries, M_LRO, &lc->lro_hashsz, 195 HASH_NOWAIT); 196 if (lc->lro_hash == NULL) { 197 memset(lc, 0, sizeof(*lc)); 198 return (ENOMEM); 199 } 200 201 /* compute size to allocate */ 202 size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) + 203 (lro_entries * sizeof(*le)); 204 lc->lro_mbuf_data = (struct lro_mbuf_sort *) 205 malloc(size, M_LRO, M_NOWAIT | M_ZERO); 206 207 /* check for out of memory */ 208 if (lc->lro_mbuf_data == NULL) { 209 free(lc->lro_hash, M_LRO); 210 memset(lc, 0, sizeof(*lc)); 211 return (ENOMEM); 212 } 213 /* compute offset for LRO entries */ 214 le = (struct lro_entry *) 215 (lc->lro_mbuf_data + lro_mbufs); 216 217 /* setup linked list */ 218 for (i = 0; i != lro_entries; i++) 219 LIST_INSERT_HEAD(&lc->lro_free, le + i, next); 220 221 return (0); 222 } 223 224 struct vxlan_header { 225 uint32_t vxlh_flags; 226 uint32_t vxlh_vni; 227 }; 228 229 static inline void * 230 tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen) 231 { 232 const struct ether_vlan_header *eh; 233 void *old; 234 uint16_t eth_type; 235 236 if (update_data) 237 memset(parser, 0, sizeof(*parser)); 238 239 old = ptr; 240 241 if (is_vxlan) { 242 const struct vxlan_header *vxh; 243 vxh = ptr; 244 ptr = (uint8_t *)ptr + sizeof(*vxh); 245 if (update_data) { 246 parser->data.vxlan_vni = 247 vxh->vxlh_vni & htonl(0xffffff00); 248 } 249 } 250 251 eh = ptr; 252 if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) { 253 eth_type = eh->evl_proto; 254 if (update_data) { 255 /* strip priority and keep VLAN ID only */ 256 parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK); 257 } 258 /* advance to next header */ 259 ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 260 mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 261 } else { 262 eth_type = eh->evl_encap_proto; 263 /* advance to next header */ 264 mlen -= ETHER_HDR_LEN; 265 ptr = (uint8_t *)ptr + ETHER_HDR_LEN; 266 } 267 if (__predict_false(mlen <= 0)) 268 return (NULL); 269 switch (eth_type) { 270 #ifdef INET 271 case htons(ETHERTYPE_IP): 272 parser->ip4 = ptr; 273 if (__predict_false(mlen < sizeof(struct ip))) 274 return (NULL); 275 /* Ensure there are no IPv4 options. */ 276 if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4)) 277 break; 278 /* .. and the packet is not fragmented. */ 279 if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK)) 280 break; 281 /* .. and the packet has valid src/dst addrs */ 282 if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY || 283 parser->ip4->ip_dst.s_addr == INADDR_ANY)) 284 break; 285 ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2); 286 mlen -= sizeof(struct ip); 287 if (update_data) { 288 parser->data.s_addr.v4 = parser->ip4->ip_src; 289 parser->data.d_addr.v4 = parser->ip4->ip_dst; 290 } 291 switch (parser->ip4->ip_p) { 292 case IPPROTO_UDP: 293 if (__predict_false(mlen < sizeof(struct udphdr))) 294 return (NULL); 295 parser->udp = ptr; 296 if (update_data) { 297 parser->data.lro_type = LRO_TYPE_IPV4_UDP; 298 parser->data.s_port = parser->udp->uh_sport; 299 parser->data.d_port = parser->udp->uh_dport; 300 } else { 301 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP); 302 } 303 ptr = ((uint8_t *)ptr + sizeof(*parser->udp)); 304 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 305 return (ptr); 306 case IPPROTO_TCP: 307 parser->tcp = ptr; 308 if (__predict_false(mlen < sizeof(struct tcphdr))) 309 return (NULL); 310 if (update_data) { 311 parser->data.lro_type = LRO_TYPE_IPV4_TCP; 312 parser->data.s_port = parser->tcp->th_sport; 313 parser->data.d_port = parser->tcp->th_dport; 314 } else { 315 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP); 316 } 317 if (__predict_false(mlen < (parser->tcp->th_off << 2))) 318 return (NULL); 319 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 320 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 321 return (ptr); 322 default: 323 break; 324 } 325 break; 326 #endif 327 #ifdef INET6 328 case htons(ETHERTYPE_IPV6): 329 parser->ip6 = ptr; 330 if (__predict_false(mlen < sizeof(struct ip6_hdr))) 331 return (NULL); 332 /* Ensure the packet has valid src/dst addrs */ 333 if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) || 334 IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst))) 335 return (NULL); 336 ptr = (uint8_t *)ptr + sizeof(*parser->ip6); 337 if (update_data) { 338 parser->data.s_addr.v6 = parser->ip6->ip6_src; 339 parser->data.d_addr.v6 = parser->ip6->ip6_dst; 340 } 341 mlen -= sizeof(struct ip6_hdr); 342 switch (parser->ip6->ip6_nxt) { 343 case IPPROTO_UDP: 344 if (__predict_false(mlen < sizeof(struct udphdr))) 345 return (NULL); 346 parser->udp = ptr; 347 if (update_data) { 348 parser->data.lro_type = LRO_TYPE_IPV6_UDP; 349 parser->data.s_port = parser->udp->uh_sport; 350 parser->data.d_port = parser->udp->uh_dport; 351 } else { 352 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP); 353 } 354 ptr = (uint8_t *)ptr + sizeof(*parser->udp); 355 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 356 return (ptr); 357 case IPPROTO_TCP: 358 if (__predict_false(mlen < sizeof(struct tcphdr))) 359 return (NULL); 360 parser->tcp = ptr; 361 if (update_data) { 362 parser->data.lro_type = LRO_TYPE_IPV6_TCP; 363 parser->data.s_port = parser->tcp->th_sport; 364 parser->data.d_port = parser->tcp->th_dport; 365 } else { 366 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP); 367 } 368 if (__predict_false(mlen < (parser->tcp->th_off << 2))) 369 return (NULL); 370 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 371 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 372 return (ptr); 373 default: 374 break; 375 } 376 break; 377 #endif 378 default: 379 break; 380 } 381 /* Invalid packet - cannot parse */ 382 return (NULL); 383 } 384 385 static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID | 386 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID; 387 388 static inline struct lro_parser * 389 tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data) 390 { 391 void *data_ptr; 392 393 /* Try to parse outer headers first. */ 394 data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len); 395 if (data_ptr == NULL || po->total_hdr_len > m->m_len) 396 return (NULL); 397 398 if (update_data) { 399 /* Store VLAN ID, if any. */ 400 if (__predict_false(m->m_flags & M_VLANTAG)) { 401 po->data.vlan_id = 402 htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK); 403 } 404 /* Store decrypted flag, if any. */ 405 if (__predict_false((m->m_pkthdr.csum_flags & 406 CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED)) 407 po->data.lro_flags |= LRO_FLAG_DECRYPTED; 408 } 409 410 switch (po->data.lro_type) { 411 case LRO_TYPE_IPV4_UDP: 412 case LRO_TYPE_IPV6_UDP: 413 /* Check for VXLAN headers. */ 414 if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum) 415 break; 416 417 /* Try to parse inner headers. */ 418 data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true, 419 (m->m_len - ((caddr_t)data_ptr - m->m_data))); 420 if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len) 421 break; 422 423 /* Verify supported header types. */ 424 switch (pi->data.lro_type) { 425 case LRO_TYPE_IPV4_TCP: 426 case LRO_TYPE_IPV6_TCP: 427 return (pi); 428 default: 429 break; 430 } 431 break; 432 case LRO_TYPE_IPV4_TCP: 433 case LRO_TYPE_IPV6_TCP: 434 if (update_data) 435 memset(pi, 0, sizeof(*pi)); 436 return (po); 437 default: 438 break; 439 } 440 return (NULL); 441 } 442 443 static inline int 444 tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po) 445 { 446 int len; 447 448 switch (po->data.lro_type) { 449 #ifdef INET 450 case LRO_TYPE_IPV4_TCP: 451 len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) + 452 ntohs(po->ip4->ip_len); 453 break; 454 #endif 455 #ifdef INET6 456 case LRO_TYPE_IPV6_TCP: 457 len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) + 458 ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6); 459 break; 460 #endif 461 default: 462 return (TCP_LRO_CANNOT); 463 } 464 465 /* 466 * If the frame is padded beyond the end of the IP packet, 467 * then trim the extra bytes off: 468 */ 469 if (__predict_true(m->m_pkthdr.len == len)) { 470 return (0); 471 } else if (m->m_pkthdr.len > len) { 472 m_adj(m, len - m->m_pkthdr.len); 473 return (0); 474 } 475 return (TCP_LRO_CANNOT); 476 } 477 478 static void 479 lro_free_mbuf_chain(struct mbuf *m) 480 { 481 struct mbuf *save; 482 483 while (m) { 484 save = m->m_nextpkt; 485 m->m_nextpkt = NULL; 486 m_freem(m); 487 m = save; 488 } 489 } 490 491 void 492 tcp_lro_free(struct lro_ctrl *lc) 493 { 494 struct lro_entry *le; 495 unsigned x; 496 497 /* reset LRO free list */ 498 LIST_INIT(&lc->lro_free); 499 500 /* free active mbufs, if any */ 501 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 502 tcp_lro_active_remove(le); 503 lro_free_mbuf_chain(le->m_head); 504 } 505 506 /* free hash table */ 507 free(lc->lro_hash, M_LRO); 508 lc->lro_hash = NULL; 509 lc->lro_hashsz = 0; 510 511 /* free mbuf array, if any */ 512 for (x = 0; x != lc->lro_mbuf_count; x++) 513 m_freem(lc->lro_mbuf_data[x].mb); 514 lc->lro_mbuf_count = 0; 515 516 /* free allocated memory, if any */ 517 free(lc->lro_mbuf_data, M_LRO); 518 lc->lro_mbuf_data = NULL; 519 } 520 521 static uint16_t 522 tcp_lro_rx_csum_tcphdr(const struct tcphdr *th) 523 { 524 const uint16_t *ptr; 525 uint32_t csum; 526 uint16_t len; 527 528 csum = -th->th_sum; /* exclude checksum field */ 529 len = th->th_off; 530 ptr = (const uint16_t *)th; 531 while (len--) { 532 csum += *ptr; 533 ptr++; 534 csum += *ptr; 535 ptr++; 536 } 537 while (csum > 0xffff) 538 csum = (csum >> 16) + (csum & 0xffff); 539 540 return (csum); 541 } 542 543 static uint16_t 544 tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum) 545 { 546 uint32_t c; 547 uint16_t cs; 548 549 c = tcp_csum; 550 551 switch (pa->data.lro_type) { 552 #ifdef INET6 553 case LRO_TYPE_IPV6_TCP: 554 /* Compute full pseudo IPv6 header checksum. */ 555 cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0); 556 break; 557 #endif 558 #ifdef INET 559 case LRO_TYPE_IPV4_TCP: 560 /* Compute full pseudo IPv4 header checsum. */ 561 cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP); 562 cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs)); 563 break; 564 #endif 565 default: 566 cs = 0; /* Keep compiler happy. */ 567 break; 568 } 569 570 /* Complement checksum. */ 571 cs = ~cs; 572 c += cs; 573 574 /* Remove TCP header checksum. */ 575 cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp); 576 c += cs; 577 578 /* Compute checksum remainder. */ 579 while (c > 0xffff) 580 c = (c >> 16) + (c & 0xffff); 581 582 return (c); 583 } 584 585 static void 586 tcp_lro_rx_done(struct lro_ctrl *lc) 587 { 588 struct lro_entry *le; 589 590 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 591 tcp_lro_active_remove(le); 592 tcp_lro_flush(lc, le); 593 } 594 } 595 596 static void 597 tcp_lro_flush_active(struct lro_ctrl *lc) 598 { 599 struct lro_entry *le, *le_tmp; 600 601 /* 602 * Walk through the list of le entries, and 603 * any one that does have packets flush. This 604 * is called because we have an inbound packet 605 * (e.g. SYN) that has to have all others flushed 606 * in front of it. Note we have to do the remove 607 * because tcp_lro_flush() assumes that the entry 608 * is being freed. This is ok it will just get 609 * reallocated again like it was new. 610 */ 611 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 612 if (le->m_head != NULL) { 613 tcp_lro_active_remove(le); 614 tcp_lro_flush(lc, le); 615 } 616 } 617 } 618 619 void 620 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 621 { 622 struct lro_entry *le, *le_tmp; 623 uint64_t now, tov; 624 struct bintime bt; 625 626 NET_EPOCH_ASSERT(); 627 if (LIST_EMPTY(&lc->lro_active)) 628 return; 629 630 /* get timeout time and current time in ns */ 631 binuptime(&bt); 632 now = bintime2ns(&bt); 633 tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000)); 634 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 635 if (now >= (bintime2ns(&le->alloc_time) + tov)) { 636 tcp_lro_active_remove(le); 637 tcp_lro_flush(lc, le); 638 } 639 } 640 } 641 642 #ifdef INET 643 static int 644 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4) 645 { 646 uint16_t csum; 647 648 /* Legacy IP has a header checksum that needs to be correct. */ 649 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 650 if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) { 651 lc->lro_bad_csum++; 652 return (TCP_LRO_CANNOT); 653 } 654 } else { 655 csum = in_cksum_hdr(ip4); 656 if (__predict_false(csum != 0)) { 657 lc->lro_bad_csum++; 658 return (TCP_LRO_CANNOT); 659 } 660 } 661 return (0); 662 } 663 #endif 664 665 static inline void 666 tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum) 667 { 668 uint32_t csum; 669 670 csum = 0xffff - *ptr + value; 671 while (csum > 0xffff) 672 csum = (csum >> 16) + (csum & 0xffff); 673 *ptr = value; 674 *psum = csum; 675 } 676 677 static uint16_t 678 tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le, 679 uint16_t payload_len, uint16_t delta_sum) 680 { 681 uint32_t csum; 682 uint16_t tlen; 683 uint16_t temp[5] = {}; 684 685 switch (pa->data.lro_type) { 686 case LRO_TYPE_IPV4_TCP: 687 /* Compute new IPv4 length. */ 688 tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len; 689 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 690 691 /* Subtract delta from current IPv4 checksum. */ 692 csum = pa->ip4->ip_sum + 0xffff - temp[0]; 693 while (csum > 0xffff) 694 csum = (csum >> 16) + (csum & 0xffff); 695 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 696 goto update_tcp_header; 697 698 case LRO_TYPE_IPV6_TCP: 699 /* Compute new IPv6 length. */ 700 tlen = (pa->tcp->th_off << 2) + payload_len; 701 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 702 goto update_tcp_header; 703 704 case LRO_TYPE_IPV4_UDP: 705 /* Compute new IPv4 length. */ 706 tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len; 707 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 708 709 /* Subtract delta from current IPv4 checksum. */ 710 csum = pa->ip4->ip_sum + 0xffff - temp[0]; 711 while (csum > 0xffff) 712 csum = (csum >> 16) + (csum & 0xffff); 713 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 714 goto update_udp_header; 715 716 case LRO_TYPE_IPV6_UDP: 717 /* Compute new IPv6 length. */ 718 tlen = sizeof(*pa->udp) + payload_len; 719 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 720 goto update_udp_header; 721 722 default: 723 return (0); 724 } 725 726 update_tcp_header: 727 /* Compute current TCP header checksum. */ 728 temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp); 729 730 /* Incorporate the latest ACK into the TCP header. */ 731 pa->tcp->th_ack = le->ack_seq; 732 pa->tcp->th_win = le->window; 733 734 /* Incorporate latest timestamp into the TCP header. */ 735 if (le->timestamp != 0) { 736 uint32_t *ts_ptr; 737 738 ts_ptr = (uint32_t *)(pa->tcp + 1); 739 ts_ptr[1] = htonl(le->tsval); 740 ts_ptr[2] = le->tsecr; 741 } 742 743 /* Compute new TCP header checksum. */ 744 temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp); 745 746 /* Compute new TCP checksum. */ 747 csum = pa->tcp->th_sum + 0xffff - delta_sum + 748 0xffff - temp[0] + 0xffff - temp[3] + temp[2]; 749 while (csum > 0xffff) 750 csum = (csum >> 16) + (csum & 0xffff); 751 752 /* Assign new TCP checksum. */ 753 tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]); 754 755 /* Compute all modififications affecting next checksum. */ 756 csum = temp[0] + temp[1] + 0xffff - temp[2] + 757 temp[3] + temp[4] + delta_sum; 758 while (csum > 0xffff) 759 csum = (csum >> 16) + (csum & 0xffff); 760 761 /* Return delta checksum to next stage, if any. */ 762 return (csum); 763 764 update_udp_header: 765 tlen = sizeof(*pa->udp) + payload_len; 766 /* Assign new UDP length and compute checksum delta. */ 767 tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]); 768 769 /* Check if there is a UDP checksum. */ 770 if (__predict_false(pa->udp->uh_sum != 0)) { 771 /* Compute new UDP checksum. */ 772 csum = pa->udp->uh_sum + 0xffff - delta_sum + 773 0xffff - temp[0] + 0xffff - temp[2]; 774 while (csum > 0xffff) 775 csum = (csum >> 16) + (csum & 0xffff); 776 /* Assign new UDP checksum. */ 777 tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]); 778 } 779 780 /* Compute all modififications affecting next checksum. */ 781 csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum; 782 while (csum > 0xffff) 783 csum = (csum >> 16) + (csum & 0xffff); 784 785 /* Return delta checksum to next stage, if any. */ 786 return (csum); 787 } 788 789 static void 790 tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le) 791 { 792 /* Check if we need to recompute any checksums. */ 793 if (le->needs_merge) { 794 uint16_t csum; 795 796 switch (le->inner.data.lro_type) { 797 case LRO_TYPE_IPV4_TCP: 798 csum = tcp_lro_update_checksum(&le->inner, le, 799 le->m_head->m_pkthdr.lro_tcp_d_len, 800 le->m_head->m_pkthdr.lro_tcp_d_csum); 801 csum = tcp_lro_update_checksum(&le->outer, NULL, 802 le->m_head->m_pkthdr.lro_tcp_d_len + 803 le->inner.total_hdr_len, csum); 804 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 805 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 806 le->m_head->m_pkthdr.csum_data = 0xffff; 807 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 808 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 809 break; 810 case LRO_TYPE_IPV6_TCP: 811 csum = tcp_lro_update_checksum(&le->inner, le, 812 le->m_head->m_pkthdr.lro_tcp_d_len, 813 le->m_head->m_pkthdr.lro_tcp_d_csum); 814 csum = tcp_lro_update_checksum(&le->outer, NULL, 815 le->m_head->m_pkthdr.lro_tcp_d_len + 816 le->inner.total_hdr_len, csum); 817 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 818 CSUM_PSEUDO_HDR; 819 le->m_head->m_pkthdr.csum_data = 0xffff; 820 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 821 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 822 break; 823 case LRO_TYPE_NONE: 824 switch (le->outer.data.lro_type) { 825 case LRO_TYPE_IPV4_TCP: 826 csum = tcp_lro_update_checksum(&le->outer, le, 827 le->m_head->m_pkthdr.lro_tcp_d_len, 828 le->m_head->m_pkthdr.lro_tcp_d_csum); 829 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 830 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 831 le->m_head->m_pkthdr.csum_data = 0xffff; 832 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 833 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 834 break; 835 case LRO_TYPE_IPV6_TCP: 836 csum = tcp_lro_update_checksum(&le->outer, le, 837 le->m_head->m_pkthdr.lro_tcp_d_len, 838 le->m_head->m_pkthdr.lro_tcp_d_csum); 839 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 840 CSUM_PSEUDO_HDR; 841 le->m_head->m_pkthdr.csum_data = 0xffff; 842 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED)) 843 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED; 844 break; 845 default: 846 break; 847 } 848 break; 849 default: 850 break; 851 } 852 } 853 854 /* 855 * Break any chain, this is not set to NULL on the singleton 856 * case m_nextpkt points to m_head. Other case set them 857 * m_nextpkt to NULL in push_and_replace. 858 */ 859 le->m_head->m_nextpkt = NULL; 860 lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs; 861 (*lc->ifp->if_input)(lc->ifp, le->m_head); 862 } 863 864 static void 865 tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, 866 struct mbuf *m, struct tcphdr *th) 867 { 868 uint32_t *ts_ptr; 869 uint16_t tcp_data_len; 870 uint16_t tcp_opt_len; 871 872 ts_ptr = (uint32_t *)(th + 1); 873 tcp_opt_len = (th->th_off << 2); 874 tcp_opt_len -= sizeof(*th); 875 876 /* Check if there is a timestamp option. */ 877 if (tcp_opt_len == 0 || 878 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 879 *ts_ptr != TCP_LRO_TS_OPTION)) { 880 /* We failed to find the timestamp option. */ 881 le->timestamp = 0; 882 } else { 883 le->timestamp = 1; 884 le->tsval = ntohl(*(ts_ptr + 1)); 885 le->tsecr = *(ts_ptr + 2); 886 } 887 888 tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 889 890 /* Pull out TCP sequence numbers and window size. */ 891 le->next_seq = ntohl(th->th_seq) + tcp_data_len; 892 le->ack_seq = th->th_ack; 893 le->window = th->th_win; 894 le->flags = tcp_get_flags(th); 895 le->needs_merge = 0; 896 897 /* Setup new data pointers. */ 898 le->m_head = m; 899 le->m_tail = m_last(m); 900 } 901 902 static void 903 tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m) 904 { 905 struct lro_parser *pa; 906 907 /* 908 * Push up the stack of the current entry 909 * and replace it with "m". 910 */ 911 struct mbuf *msave; 912 913 /* Grab off the next and save it */ 914 msave = le->m_head->m_nextpkt; 915 le->m_head->m_nextpkt = NULL; 916 917 /* Now push out the old entry */ 918 tcp_flush_out_entry(lc, le); 919 920 /* Re-parse new header, should not fail. */ 921 pa = tcp_lro_parser(m, &le->outer, &le->inner, false); 922 KASSERT(pa != NULL, 923 ("tcp_push_and_replace: LRO parser failed on m=%p\n", m)); 924 925 /* 926 * Now to replace the data properly in the entry 927 * we have to reset the TCP header and 928 * other fields. 929 */ 930 tcp_set_entry_to_mbuf(lc, le, m, pa->tcp); 931 932 /* Restore the next list */ 933 m->m_nextpkt = msave; 934 } 935 936 static void 937 tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p) 938 { 939 struct mbuf *m; 940 uint32_t csum; 941 942 m = le->m_head; 943 if (m->m_pkthdr.lro_nsegs == 1) { 944 /* Compute relative checksum. */ 945 csum = p->m_pkthdr.lro_tcp_d_csum; 946 } else { 947 /* Merge TCP data checksums. */ 948 csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum + 949 (uint32_t)p->m_pkthdr.lro_tcp_d_csum; 950 while (csum > 0xffff) 951 csum = (csum >> 16) + (csum & 0xffff); 952 } 953 954 /* Update various counters. */ 955 m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len; 956 m->m_pkthdr.lro_tcp_d_csum = csum; 957 m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len; 958 m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs; 959 le->needs_merge = 1; 960 } 961 962 static void 963 tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le) 964 { 965 /* 966 * Walk through the mbuf chain we 967 * have on tap and compress/condense 968 * as required. 969 */ 970 uint32_t *ts_ptr; 971 struct mbuf *m; 972 struct tcphdr *th; 973 uint32_t tcp_data_len_total; 974 uint32_t tcp_data_seg_total; 975 uint16_t tcp_data_len; 976 uint16_t tcp_opt_len; 977 978 /* 979 * First we must check the lead (m_head) 980 * we must make sure that it is *not* 981 * something that should be sent up 982 * right away (sack etc). 983 */ 984 again: 985 m = le->m_head->m_nextpkt; 986 if (m == NULL) { 987 /* Just one left. */ 988 return; 989 } 990 991 th = tcp_lro_get_th(m); 992 tcp_opt_len = (th->th_off << 2); 993 tcp_opt_len -= sizeof(*th); 994 ts_ptr = (uint32_t *)(th + 1); 995 996 if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 997 *ts_ptr != TCP_LRO_TS_OPTION)) { 998 /* 999 * Its not the timestamp. We can't 1000 * use this guy as the head. 1001 */ 1002 le->m_head->m_nextpkt = m->m_nextpkt; 1003 tcp_push_and_replace(lc, le, m); 1004 goto again; 1005 } 1006 if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 1007 /* 1008 * Make sure that previously seen segments/ACKs are delivered 1009 * before this segment, e.g. FIN. 1010 */ 1011 le->m_head->m_nextpkt = m->m_nextpkt; 1012 tcp_push_and_replace(lc, le, m); 1013 goto again; 1014 } 1015 while((m = le->m_head->m_nextpkt) != NULL) { 1016 /* 1017 * condense m into le, first 1018 * pull m out of the list. 1019 */ 1020 le->m_head->m_nextpkt = m->m_nextpkt; 1021 m->m_nextpkt = NULL; 1022 /* Setup my data */ 1023 tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 1024 th = tcp_lro_get_th(m); 1025 ts_ptr = (uint32_t *)(th + 1); 1026 tcp_opt_len = (th->th_off << 2); 1027 tcp_opt_len -= sizeof(*th); 1028 tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len; 1029 tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs; 1030 1031 if (tcp_data_seg_total >= lc->lro_ackcnt_lim || 1032 tcp_data_len_total >= lc->lro_length_lim) { 1033 /* Flush now if appending will result in overflow. */ 1034 tcp_push_and_replace(lc, le, m); 1035 goto again; 1036 } 1037 if (tcp_opt_len != 0 && 1038 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 1039 *ts_ptr != TCP_LRO_TS_OPTION)) { 1040 /* 1041 * Maybe a sack in the new one? We need to 1042 * start all over after flushing the 1043 * current le. We will go up to the beginning 1044 * and flush it (calling the replace again possibly 1045 * or just returning). 1046 */ 1047 tcp_push_and_replace(lc, le, m); 1048 goto again; 1049 } 1050 if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) { 1051 tcp_push_and_replace(lc, le, m); 1052 goto again; 1053 } 1054 if (tcp_opt_len != 0) { 1055 uint32_t tsval = ntohl(*(ts_ptr + 1)); 1056 /* Make sure timestamp values are increasing. */ 1057 if (TSTMP_GT(le->tsval, tsval)) { 1058 tcp_push_and_replace(lc, le, m); 1059 goto again; 1060 } 1061 le->tsval = tsval; 1062 le->tsecr = *(ts_ptr + 2); 1063 } 1064 /* Try to append the new segment. */ 1065 if (__predict_false(ntohl(th->th_seq) != le->next_seq || 1066 ((tcp_get_flags(th) & TH_ACK) != 1067 (le->flags & TH_ACK)) || 1068 (tcp_data_len == 0 && 1069 le->ack_seq == th->th_ack && 1070 le->window == th->th_win))) { 1071 /* Out of order packet, non-ACK + ACK or dup ACK. */ 1072 tcp_push_and_replace(lc, le, m); 1073 goto again; 1074 } 1075 if (tcp_data_len != 0 || 1076 SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1077 le->next_seq += tcp_data_len; 1078 le->ack_seq = th->th_ack; 1079 le->window = th->th_win; 1080 le->needs_merge = 1; 1081 } else if (th->th_ack == le->ack_seq) { 1082 if (WIN_GT(th->th_win, le->window)) { 1083 le->window = th->th_win; 1084 le->needs_merge = 1; 1085 } 1086 } 1087 1088 if (tcp_data_len == 0) { 1089 m_freem(m); 1090 continue; 1091 } 1092 1093 /* Merge TCP data checksum and length to head mbuf. */ 1094 tcp_lro_mbuf_append_pkthdr(le, m); 1095 1096 /* 1097 * Adjust the mbuf so that m_data points to the first byte of 1098 * the ULP payload. Adjust the mbuf to avoid complications and 1099 * append new segment to existing mbuf chain. 1100 */ 1101 m_adj(m, m->m_pkthdr.len - tcp_data_len); 1102 m_demote_pkthdr(m); 1103 le->m_tail->m_next = m; 1104 le->m_tail = m_last(m); 1105 } 1106 } 1107 1108 static void 1109 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 1110 { 1111 1112 /* Only optimise if there are multiple packets waiting. */ 1113 NET_EPOCH_ASSERT(); 1114 if (tcp_lro_flush_tcphpts == NULL || 1115 tcp_lro_flush_tcphpts(lc, le) != 0) { 1116 tcp_lro_condense(lc, le); 1117 tcp_flush_out_entry(lc, le); 1118 } 1119 lc->lro_flushed++; 1120 bzero(le, sizeof(*le)); 1121 LIST_INSERT_HEAD(&lc->lro_free, le, next); 1122 } 1123 1124 #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1)) 1125 1126 /* 1127 * The tcp_lro_sort() routine is comparable to qsort(), except it has 1128 * a worst case complexity limit of O(MIN(N,64)*N), where N is the 1129 * number of elements to sort and 64 is the number of sequence bits 1130 * available. The algorithm is bit-slicing the 64-bit sequence number, 1131 * sorting one bit at a time from the most significant bit until the 1132 * least significant one, skipping the constant bits. This is 1133 * typically called a radix sort. 1134 */ 1135 static void 1136 tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size) 1137 { 1138 struct lro_mbuf_sort temp; 1139 uint64_t ones; 1140 uint64_t zeros; 1141 uint32_t x; 1142 uint32_t y; 1143 1144 repeat: 1145 /* for small arrays insertion sort is faster */ 1146 if (size <= 12) { 1147 for (x = 1; x < size; x++) { 1148 temp = parray[x]; 1149 for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--) 1150 parray[y] = parray[y - 1]; 1151 parray[y] = temp; 1152 } 1153 return; 1154 } 1155 1156 /* compute sequence bits which are constant */ 1157 ones = 0; 1158 zeros = 0; 1159 for (x = 0; x != size; x++) { 1160 ones |= parray[x].seq; 1161 zeros |= ~parray[x].seq; 1162 } 1163 1164 /* compute bits which are not constant into "ones" */ 1165 ones &= zeros; 1166 if (ones == 0) 1167 return; 1168 1169 /* pick the most significant bit which is not constant */ 1170 ones = tcp_lro_msb_64(ones); 1171 1172 /* 1173 * Move entries having cleared sequence bits to the beginning 1174 * of the array: 1175 */ 1176 for (x = y = 0; y != size; y++) { 1177 /* skip set bits */ 1178 if (parray[y].seq & ones) 1179 continue; 1180 /* swap entries */ 1181 temp = parray[x]; 1182 parray[x] = parray[y]; 1183 parray[y] = temp; 1184 x++; 1185 } 1186 1187 KASSERT(x != 0 && x != size, ("Memory is corrupted\n")); 1188 1189 /* sort zeros */ 1190 tcp_lro_sort(parray, x); 1191 1192 /* sort ones */ 1193 parray += x; 1194 size -= x; 1195 goto repeat; 1196 } 1197 1198 void 1199 tcp_lro_flush_all(struct lro_ctrl *lc) 1200 { 1201 uint64_t seq; 1202 uint64_t nseq; 1203 unsigned x; 1204 1205 NET_EPOCH_ASSERT(); 1206 /* check if no mbufs to flush */ 1207 if (lc->lro_mbuf_count == 0) 1208 goto done; 1209 if (lc->lro_cpu_is_set == 0) { 1210 if (lc->lro_last_cpu == curcpu) { 1211 lc->lro_cnt_of_same_cpu++; 1212 /* Have we reached the threshold to declare a cpu? */ 1213 if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh) 1214 lc->lro_cpu_is_set = 1; 1215 } else { 1216 lc->lro_last_cpu = curcpu; 1217 lc->lro_cnt_of_same_cpu = 0; 1218 } 1219 } 1220 CURVNET_SET(lc->ifp->if_vnet); 1221 1222 /* get current time */ 1223 binuptime(&lc->lro_last_queue_time); 1224 1225 /* sort all mbufs according to stream */ 1226 tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count); 1227 1228 /* input data into LRO engine, stream by stream */ 1229 seq = 0; 1230 for (x = 0; x != lc->lro_mbuf_count; x++) { 1231 struct mbuf *mb; 1232 1233 /* get mbuf */ 1234 mb = lc->lro_mbuf_data[x].mb; 1235 1236 /* get sequence number, masking away the packet index */ 1237 nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24); 1238 1239 /* check for new stream */ 1240 if (seq != nseq) { 1241 seq = nseq; 1242 1243 /* flush active streams */ 1244 tcp_lro_rx_done(lc); 1245 } 1246 1247 /* add packet to LRO engine */ 1248 if (tcp_lro_rx_common(lc, mb, 0, false) != 0) { 1249 /* Flush anything we have acummulated */ 1250 tcp_lro_flush_active(lc); 1251 /* input packet to network layer */ 1252 (*lc->ifp->if_input)(lc->ifp, mb); 1253 lc->lro_queued++; 1254 lc->lro_flushed++; 1255 } 1256 } 1257 CURVNET_RESTORE(); 1258 done: 1259 /* flush active streams */ 1260 tcp_lro_rx_done(lc); 1261 tcp_hpts_softclock(); 1262 lc->lro_mbuf_count = 0; 1263 } 1264 1265 static struct lro_head * 1266 tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser) 1267 { 1268 u_long hash; 1269 1270 if (M_HASHTYPE_ISHASH(m)) { 1271 hash = m->m_pkthdr.flowid; 1272 } else { 1273 for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++) 1274 hash += parser->data.raw[i]; 1275 } 1276 return (&lc->lro_hash[hash % lc->lro_hashsz]); 1277 } 1278 1279 static int 1280 tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash) 1281 { 1282 struct lro_parser pi; /* inner address data */ 1283 struct lro_parser po; /* outer address data */ 1284 struct lro_parser *pa; /* current parser for TCP stream */ 1285 struct lro_entry *le; 1286 struct lro_head *bucket; 1287 struct tcphdr *th; 1288 int tcp_data_len; 1289 int tcp_opt_len; 1290 int error; 1291 uint16_t tcp_data_sum; 1292 1293 #ifdef INET 1294 /* Quickly decide if packet cannot be LRO'ed */ 1295 if (__predict_false(V_ipforwarding != 0)) 1296 return (TCP_LRO_CANNOT); 1297 #endif 1298 #ifdef INET6 1299 /* Quickly decide if packet cannot be LRO'ed */ 1300 if (__predict_false(V_ip6_forwarding != 0)) 1301 return (TCP_LRO_CANNOT); 1302 #endif 1303 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1304 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1305 (m->m_pkthdr.csum_data != 0xffff)) { 1306 /* 1307 * The checksum either did not have hardware offload 1308 * or it was a bad checksum. We can't LRO such 1309 * a packet. 1310 */ 1311 counter_u64_add(tcp_bad_csums, 1); 1312 return (TCP_LRO_CANNOT); 1313 } 1314 /* We expect a contiguous header [eh, ip, tcp]. */ 1315 pa = tcp_lro_parser(m, &po, &pi, true); 1316 if (__predict_false(pa == NULL)) 1317 return (TCP_LRO_NOT_SUPPORTED); 1318 1319 /* We don't expect any padding. */ 1320 error = tcp_lro_trim_mbuf_chain(m, pa); 1321 if (__predict_false(error != 0)) 1322 return (error); 1323 1324 #ifdef INET 1325 switch (pa->data.lro_type) { 1326 case LRO_TYPE_IPV4_TCP: 1327 error = tcp_lro_rx_ipv4(lc, m, pa->ip4); 1328 if (__predict_false(error != 0)) 1329 return (error); 1330 break; 1331 default: 1332 break; 1333 } 1334 #endif 1335 /* If no hardware or arrival stamp on the packet add timestamp */ 1336 if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) { 1337 m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1338 m->m_flags |= M_TSTMP_LRO; 1339 } 1340 1341 /* Get pointer to TCP header. */ 1342 th = pa->tcp; 1343 1344 /* Don't process SYN packets. */ 1345 if (__predict_false(tcp_get_flags(th) & TH_SYN)) 1346 return (TCP_LRO_CANNOT); 1347 1348 /* Get total TCP header length and compute payload length. */ 1349 tcp_opt_len = (th->th_off << 2); 1350 tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th - 1351 (uint8_t *)m->m_data) - tcp_opt_len; 1352 tcp_opt_len -= sizeof(*th); 1353 1354 /* Don't process invalid TCP headers. */ 1355 if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0)) 1356 return (TCP_LRO_CANNOT); 1357 1358 /* Compute TCP data only checksum. */ 1359 if (tcp_data_len == 0) 1360 tcp_data_sum = 0; /* no data, no checksum */ 1361 else if (__predict_false(csum != 0)) 1362 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum); 1363 else 1364 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum); 1365 1366 /* Save TCP info in mbuf. */ 1367 m->m_nextpkt = NULL; 1368 m->m_pkthdr.rcvif = lc->ifp; 1369 m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum; 1370 m->m_pkthdr.lro_tcp_d_len = tcp_data_len; 1371 m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data); 1372 m->m_pkthdr.lro_nsegs = 1; 1373 1374 /* Get hash bucket. */ 1375 if (!use_hash) { 1376 bucket = &lc->lro_hash[0]; 1377 } else { 1378 bucket = tcp_lro_rx_get_bucket(lc, m, pa); 1379 } 1380 1381 /* Try to find a matching previous segment. */ 1382 LIST_FOREACH(le, bucket, hash_next) { 1383 /* Compare addresses and ports. */ 1384 if (lro_address_compare(&po.data, &le->outer.data) == false || 1385 lro_address_compare(&pi.data, &le->inner.data) == false) 1386 continue; 1387 1388 /* Check if no data and old ACK. */ 1389 if (tcp_data_len == 0 && 1390 SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1391 m_freem(m); 1392 return (0); 1393 } 1394 1395 /* Mark "m" in the last spot. */ 1396 le->m_last_mbuf->m_nextpkt = m; 1397 /* Now set the tail to "m". */ 1398 le->m_last_mbuf = m; 1399 return (0); 1400 } 1401 1402 /* Try to find an empty slot. */ 1403 if (LIST_EMPTY(&lc->lro_free)) 1404 return (TCP_LRO_NO_ENTRIES); 1405 1406 /* Start a new segment chain. */ 1407 le = LIST_FIRST(&lc->lro_free); 1408 LIST_REMOVE(le, next); 1409 tcp_lro_active_insert(lc, bucket, le); 1410 1411 /* Make sure the headers are set. */ 1412 le->inner = pi; 1413 le->outer = po; 1414 1415 /* Store time this entry was allocated. */ 1416 le->alloc_time = lc->lro_last_queue_time; 1417 1418 tcp_set_entry_to_mbuf(lc, le, m, th); 1419 1420 /* Now set the tail to "m". */ 1421 le->m_last_mbuf = m; 1422 1423 return (0); 1424 } 1425 1426 int 1427 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 1428 { 1429 int error; 1430 1431 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1432 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1433 (m->m_pkthdr.csum_data != 0xffff)) { 1434 /* 1435 * The checksum either did not have hardware offload 1436 * or it was a bad checksum. We can't LRO such 1437 * a packet. 1438 */ 1439 counter_u64_add(tcp_bad_csums, 1); 1440 return (TCP_LRO_CANNOT); 1441 } 1442 /* get current time */ 1443 binuptime(&lc->lro_last_queue_time); 1444 CURVNET_SET(lc->ifp->if_vnet); 1445 error = tcp_lro_rx_common(lc, m, csum, true); 1446 if (__predict_false(error != 0)) { 1447 /* 1448 * Flush anything we have acummulated 1449 * ahead of this packet that can't 1450 * be LRO'd. This preserves order. 1451 */ 1452 tcp_lro_flush_active(lc); 1453 } 1454 CURVNET_RESTORE(); 1455 1456 return (error); 1457 } 1458 1459 void 1460 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 1461 { 1462 NET_EPOCH_ASSERT(); 1463 /* sanity checks */ 1464 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 1465 lc->lro_mbuf_max == 0)) { 1466 /* packet drop */ 1467 m_freem(mb); 1468 return; 1469 } 1470 1471 /* check if packet is not LRO capable */ 1472 if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 1473 /* input packet to network layer */ 1474 (*lc->ifp->if_input) (lc->ifp, mb); 1475 return; 1476 } 1477 1478 /* If no hardware or arrival stamp on the packet add timestamp */ 1479 if ((tcplro_stacks_wanting_mbufq > 0) && 1480 (tcp_less_accurate_lro_ts == 0) && 1481 ((mb->m_flags & M_TSTMP) == 0)) { 1482 /* Add in an LRO time since no hardware */ 1483 binuptime(&lc->lro_last_queue_time); 1484 mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1485 mb->m_flags |= M_TSTMP_LRO; 1486 } 1487 1488 /* create sequence number */ 1489 lc->lro_mbuf_data[lc->lro_mbuf_count].seq = 1490 (((uint64_t)M_HASHTYPE_GET(mb)) << 56) | 1491 (((uint64_t)mb->m_pkthdr.flowid) << 24) | 1492 ((uint64_t)lc->lro_mbuf_count); 1493 1494 /* enter mbuf */ 1495 lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb; 1496 1497 /* flush if array is full */ 1498 if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max)) 1499 tcp_lro_flush_all(lc); 1500 } 1501 1502 /* end */ 1503