1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 37 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/sockbuf.h> 49 #include <sys/sysctl.h> 50 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/ethernet.h> 54 #include <net/bpf.h> 55 #include <net/vnet.h> 56 57 #include <netinet/in_systm.h> 58 #include <netinet/in.h> 59 #include <netinet/ip6.h> 60 #include <netinet/ip.h> 61 #include <netinet/ip_var.h> 62 #include <netinet/in_pcb.h> 63 #include <netinet6/in6_pcb.h> 64 #include <netinet/tcp.h> 65 #include <netinet/tcp_seq.h> 66 #include <netinet/tcp_lro.h> 67 #include <netinet/tcp_var.h> 68 #include <netinet/tcpip.h> 69 #include <netinet/tcp_hpts.h> 70 #include <netinet/tcp_log_buf.h> 71 #include <netinet/udp.h> 72 #include <netinet6/ip6_var.h> 73 74 #include <machine/in_cksum.h> 75 76 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures"); 77 78 #define TCP_LRO_TS_OPTION \ 79 ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 80 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP) 81 82 static void tcp_lro_rx_done(struct lro_ctrl *lc); 83 static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, 84 uint32_t csum, bool use_hash); 85 86 #ifdef TCPHPTS 87 static bool do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *, 88 struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **, bool *, bool); 89 90 #endif 91 92 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 93 "TCP LRO"); 94 95 static long tcplro_stacks_wanting_mbufq; 96 counter_u64_t tcp_inp_lro_direct_queue; 97 counter_u64_t tcp_inp_lro_wokeup_queue; 98 counter_u64_t tcp_inp_lro_compressed; 99 counter_u64_t tcp_inp_lro_locks_taken; 100 counter_u64_t tcp_extra_mbuf; 101 counter_u64_t tcp_would_have_but; 102 counter_u64_t tcp_comp_total; 103 counter_u64_t tcp_uncomp_total; 104 counter_u64_t tcp_bad_csums; 105 106 static unsigned tcp_lro_entries = TCP_LRO_ENTRIES; 107 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, 108 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0, 109 "default number of LRO entries"); 110 111 static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH; 112 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold, 113 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0, 114 "Number of interrups in a row on the same CPU that will make us declare an 'affinity' cpu?"); 115 116 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD, 117 &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport"); 118 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD, 119 &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts"); 120 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD, 121 &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport"); 122 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD, 123 &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken"); 124 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD, 125 &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp"); 126 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD, 127 &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed"); 128 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD, 129 &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set"); 130 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD, 131 &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP"); 132 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD, 133 &tcp_bad_csums, "Number of packets that the common code saw with bad csums"); 134 135 void 136 tcp_lro_reg_mbufq(void) 137 { 138 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1); 139 } 140 141 void 142 tcp_lro_dereg_mbufq(void) 143 { 144 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1); 145 } 146 147 static __inline void 148 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket, 149 struct lro_entry *le) 150 { 151 152 LIST_INSERT_HEAD(&lc->lro_active, le, next); 153 LIST_INSERT_HEAD(bucket, le, hash_next); 154 } 155 156 static __inline void 157 tcp_lro_active_remove(struct lro_entry *le) 158 { 159 160 LIST_REMOVE(le, next); /* active list */ 161 LIST_REMOVE(le, hash_next); /* hash bucket */ 162 } 163 164 int 165 tcp_lro_init(struct lro_ctrl *lc) 166 { 167 return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0)); 168 } 169 170 int 171 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, 172 unsigned lro_entries, unsigned lro_mbufs) 173 { 174 struct lro_entry *le; 175 size_t size; 176 unsigned i, elements; 177 178 lc->lro_bad_csum = 0; 179 lc->lro_queued = 0; 180 lc->lro_flushed = 0; 181 lc->lro_mbuf_count = 0; 182 lc->lro_mbuf_max = lro_mbufs; 183 lc->lro_cnt = lro_entries; 184 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX; 185 lc->lro_length_lim = TCP_LRO_LENGTH_MAX; 186 lc->ifp = ifp; 187 LIST_INIT(&lc->lro_free); 188 LIST_INIT(&lc->lro_active); 189 190 /* create hash table to accelerate entry lookup */ 191 if (lro_entries > lro_mbufs) 192 elements = lro_entries; 193 else 194 elements = lro_mbufs; 195 lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz, 196 HASH_NOWAIT); 197 if (lc->lro_hash == NULL) { 198 memset(lc, 0, sizeof(*lc)); 199 return (ENOMEM); 200 } 201 202 /* compute size to allocate */ 203 size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) + 204 (lro_entries * sizeof(*le)); 205 lc->lro_mbuf_data = (struct lro_mbuf_sort *) 206 malloc(size, M_LRO, M_NOWAIT | M_ZERO); 207 208 /* check for out of memory */ 209 if (lc->lro_mbuf_data == NULL) { 210 free(lc->lro_hash, M_LRO); 211 memset(lc, 0, sizeof(*lc)); 212 return (ENOMEM); 213 } 214 /* compute offset for LRO entries */ 215 le = (struct lro_entry *) 216 (lc->lro_mbuf_data + lro_mbufs); 217 218 /* setup linked list */ 219 for (i = 0; i != lro_entries; i++) 220 LIST_INSERT_HEAD(&lc->lro_free, le + i, next); 221 222 return (0); 223 } 224 225 struct vxlan_header { 226 uint32_t vxlh_flags; 227 uint32_t vxlh_vni; 228 }; 229 230 static inline void * 231 tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen) 232 { 233 const struct ether_vlan_header *eh; 234 void *old; 235 uint16_t eth_type; 236 237 if (update_data) 238 memset(parser, 0, sizeof(*parser)); 239 240 old = ptr; 241 242 if (is_vxlan) { 243 const struct vxlan_header *vxh; 244 vxh = ptr; 245 ptr = (uint8_t *)ptr + sizeof(*vxh); 246 if (update_data) { 247 parser->data.vxlan_vni = 248 vxh->vxlh_vni & htonl(0xffffff00); 249 } 250 } 251 252 eh = ptr; 253 if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) { 254 eth_type = eh->evl_proto; 255 if (update_data) { 256 /* strip priority and keep VLAN ID only */ 257 parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK); 258 } 259 /* advance to next header */ 260 ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 261 mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 262 } else { 263 eth_type = eh->evl_encap_proto; 264 /* advance to next header */ 265 mlen -= ETHER_HDR_LEN; 266 ptr = (uint8_t *)ptr + ETHER_HDR_LEN; 267 } 268 if (__predict_false(mlen <= 0)) 269 return (NULL); 270 switch (eth_type) { 271 #ifdef INET 272 case htons(ETHERTYPE_IP): 273 parser->ip4 = ptr; 274 if (__predict_false(mlen < sizeof(struct ip))) 275 return (NULL); 276 /* Ensure there are no IPv4 options. */ 277 if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4)) 278 break; 279 /* .. and the packet is not fragmented. */ 280 if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK)) 281 break; 282 ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2); 283 mlen -= sizeof(struct ip); 284 if (update_data) { 285 parser->data.s_addr.v4 = parser->ip4->ip_src; 286 parser->data.d_addr.v4 = parser->ip4->ip_dst; 287 } 288 switch (parser->ip4->ip_p) { 289 case IPPROTO_UDP: 290 if (__predict_false(mlen < sizeof(struct udphdr))) 291 return (NULL); 292 parser->udp = ptr; 293 if (update_data) { 294 parser->data.lro_type = LRO_TYPE_IPV4_UDP; 295 parser->data.s_port = parser->udp->uh_sport; 296 parser->data.d_port = parser->udp->uh_dport; 297 } else { 298 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP); 299 } 300 ptr = ((uint8_t *)ptr + sizeof(*parser->udp)); 301 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 302 return (ptr); 303 case IPPROTO_TCP: 304 parser->tcp = ptr; 305 if (__predict_false(mlen < sizeof(struct tcphdr))) 306 return (NULL); 307 if (update_data) { 308 parser->data.lro_type = LRO_TYPE_IPV4_TCP; 309 parser->data.s_port = parser->tcp->th_sport; 310 parser->data.d_port = parser->tcp->th_dport; 311 } else { 312 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP); 313 } 314 if (__predict_false(mlen < (parser->tcp->th_off << 2))) 315 return (NULL); 316 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 317 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 318 return (ptr); 319 default: 320 break; 321 } 322 break; 323 #endif 324 #ifdef INET6 325 case htons(ETHERTYPE_IPV6): 326 parser->ip6 = ptr; 327 if (__predict_false(mlen < sizeof(struct ip6_hdr))) 328 return (NULL); 329 ptr = (uint8_t *)ptr + sizeof(*parser->ip6); 330 if (update_data) { 331 parser->data.s_addr.v6 = parser->ip6->ip6_src; 332 parser->data.d_addr.v6 = parser->ip6->ip6_dst; 333 } 334 mlen -= sizeof(struct ip6_hdr); 335 switch (parser->ip6->ip6_nxt) { 336 case IPPROTO_UDP: 337 if (__predict_false(mlen < sizeof(struct udphdr))) 338 return (NULL); 339 parser->udp = ptr; 340 if (update_data) { 341 parser->data.lro_type = LRO_TYPE_IPV6_UDP; 342 parser->data.s_port = parser->udp->uh_sport; 343 parser->data.d_port = parser->udp->uh_dport; 344 } else { 345 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP); 346 } 347 ptr = (uint8_t *)ptr + sizeof(*parser->udp); 348 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 349 return (ptr); 350 case IPPROTO_TCP: 351 if (__predict_false(mlen < sizeof(struct tcphdr))) 352 return (NULL); 353 parser->tcp = ptr; 354 if (update_data) { 355 parser->data.lro_type = LRO_TYPE_IPV6_TCP; 356 parser->data.s_port = parser->tcp->th_sport; 357 parser->data.d_port = parser->tcp->th_dport; 358 } else { 359 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP); 360 } 361 if (__predict_false(mlen < (parser->tcp->th_off << 2))) 362 return (NULL); 363 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 364 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 365 return (ptr); 366 default: 367 break; 368 } 369 break; 370 #endif 371 default: 372 break; 373 } 374 /* Invalid packet - cannot parse */ 375 return (NULL); 376 } 377 378 static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID | 379 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID; 380 381 static inline struct lro_parser * 382 tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data) 383 { 384 void *data_ptr; 385 386 /* Try to parse outer headers first. */ 387 data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len); 388 if (data_ptr == NULL || po->total_hdr_len > m->m_len) 389 return (NULL); 390 391 if (update_data) { 392 /* Store VLAN ID, if any. */ 393 if (__predict_false(m->m_flags & M_VLANTAG)) { 394 po->data.vlan_id = 395 htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK); 396 } 397 /* Store decrypted flag, if any. */ 398 if (__predict_false(m->m_flags & M_DECRYPTED)) 399 po->data.lro_flags |= LRO_FLAG_DECRYPTED; 400 } 401 402 switch (po->data.lro_type) { 403 case LRO_TYPE_IPV4_UDP: 404 case LRO_TYPE_IPV6_UDP: 405 /* Check for VXLAN headers. */ 406 if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum) 407 break; 408 409 /* Try to parse inner headers. */ 410 data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true, 411 (m->m_len - ((caddr_t)data_ptr - m->m_data))); 412 if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len) 413 break; 414 415 /* Verify supported header types. */ 416 switch (pi->data.lro_type) { 417 case LRO_TYPE_IPV4_TCP: 418 case LRO_TYPE_IPV6_TCP: 419 return (pi); 420 default: 421 break; 422 } 423 break; 424 case LRO_TYPE_IPV4_TCP: 425 case LRO_TYPE_IPV6_TCP: 426 if (update_data) 427 memset(pi, 0, sizeof(*pi)); 428 return (po); 429 default: 430 break; 431 } 432 return (NULL); 433 } 434 435 static inline int 436 tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po) 437 { 438 int len; 439 440 switch (po->data.lro_type) { 441 #ifdef INET 442 case LRO_TYPE_IPV4_TCP: 443 len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) + 444 ntohs(po->ip4->ip_len); 445 break; 446 #endif 447 #ifdef INET6 448 case LRO_TYPE_IPV6_TCP: 449 len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) + 450 ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6); 451 break; 452 #endif 453 default: 454 return (TCP_LRO_CANNOT); 455 } 456 457 /* 458 * If the frame is padded beyond the end of the IP packet, 459 * then trim the extra bytes off: 460 */ 461 if (__predict_true(m->m_pkthdr.len == len)) { 462 return (0); 463 } else if (m->m_pkthdr.len > len) { 464 m_adj(m, len - m->m_pkthdr.len); 465 return (0); 466 } 467 return (TCP_LRO_CANNOT); 468 } 469 470 static struct tcphdr * 471 tcp_lro_get_th(struct mbuf *m) 472 { 473 return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off)); 474 } 475 476 static void 477 lro_free_mbuf_chain(struct mbuf *m) 478 { 479 struct mbuf *save; 480 481 while (m) { 482 save = m->m_nextpkt; 483 m->m_nextpkt = NULL; 484 m_freem(m); 485 m = save; 486 } 487 } 488 489 void 490 tcp_lro_free(struct lro_ctrl *lc) 491 { 492 struct lro_entry *le; 493 unsigned x; 494 495 /* reset LRO free list */ 496 LIST_INIT(&lc->lro_free); 497 498 /* free active mbufs, if any */ 499 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 500 tcp_lro_active_remove(le); 501 lro_free_mbuf_chain(le->m_head); 502 } 503 504 /* free hash table */ 505 free(lc->lro_hash, M_LRO); 506 lc->lro_hash = NULL; 507 lc->lro_hashsz = 0; 508 509 /* free mbuf array, if any */ 510 for (x = 0; x != lc->lro_mbuf_count; x++) 511 m_freem(lc->lro_mbuf_data[x].mb); 512 lc->lro_mbuf_count = 0; 513 514 /* free allocated memory, if any */ 515 free(lc->lro_mbuf_data, M_LRO); 516 lc->lro_mbuf_data = NULL; 517 } 518 519 static uint16_t 520 tcp_lro_rx_csum_tcphdr(const struct tcphdr *th) 521 { 522 const uint16_t *ptr; 523 uint32_t csum; 524 uint16_t len; 525 526 csum = -th->th_sum; /* exclude checksum field */ 527 len = th->th_off; 528 ptr = (const uint16_t *)th; 529 while (len--) { 530 csum += *ptr; 531 ptr++; 532 csum += *ptr; 533 ptr++; 534 } 535 while (csum > 0xffff) 536 csum = (csum >> 16) + (csum & 0xffff); 537 538 return (csum); 539 } 540 541 static uint16_t 542 tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum) 543 { 544 uint32_t c; 545 uint16_t cs; 546 547 c = tcp_csum; 548 549 switch (pa->data.lro_type) { 550 #ifdef INET6 551 case LRO_TYPE_IPV6_TCP: 552 /* Compute full pseudo IPv6 header checksum. */ 553 cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0); 554 break; 555 #endif 556 #ifdef INET 557 case LRO_TYPE_IPV4_TCP: 558 /* Compute full pseudo IPv4 header checsum. */ 559 cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP); 560 cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs)); 561 break; 562 #endif 563 default: 564 cs = 0; /* Keep compiler happy. */ 565 break; 566 } 567 568 /* Complement checksum. */ 569 cs = ~cs; 570 c += cs; 571 572 /* Remove TCP header checksum. */ 573 cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp); 574 c += cs; 575 576 /* Compute checksum remainder. */ 577 while (c > 0xffff) 578 c = (c >> 16) + (c & 0xffff); 579 580 return (c); 581 } 582 583 static void 584 tcp_lro_rx_done(struct lro_ctrl *lc) 585 { 586 struct lro_entry *le; 587 588 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 589 tcp_lro_active_remove(le); 590 tcp_lro_flush(lc, le); 591 } 592 } 593 594 void 595 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 596 { 597 struct lro_entry *le, *le_tmp; 598 uint64_t now, tov; 599 struct bintime bt; 600 601 if (LIST_EMPTY(&lc->lro_active)) 602 return; 603 604 /* get timeout time and current time in ns */ 605 binuptime(&bt); 606 now = bintime2ns(&bt); 607 tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000)); 608 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 609 if (now >= (bintime2ns(&le->alloc_time) + tov)) { 610 tcp_lro_active_remove(le); 611 tcp_lro_flush(lc, le); 612 } 613 } 614 } 615 616 #ifdef INET 617 static int 618 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4) 619 { 620 uint16_t csum; 621 622 /* Legacy IP has a header checksum that needs to be correct. */ 623 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 624 if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) { 625 lc->lro_bad_csum++; 626 return (TCP_LRO_CANNOT); 627 } 628 } else { 629 csum = in_cksum_hdr(ip4); 630 if (__predict_false(csum != 0)) { 631 lc->lro_bad_csum++; 632 return (TCP_LRO_CANNOT); 633 } 634 } 635 return (0); 636 } 637 #endif 638 639 #ifdef TCPHPTS 640 static void 641 tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc, 642 const struct lro_entry *le, const struct mbuf *m, 643 int frm, int32_t tcp_data_len, uint32_t th_seq, 644 uint32_t th_ack, uint16_t th_win) 645 { 646 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 647 union tcp_log_stackspecific log; 648 struct timeval tv, btv; 649 uint32_t cts; 650 651 cts = tcp_get_usecs(&tv); 652 memset(&log, 0, sizeof(union tcp_log_stackspecific)); 653 log.u_bbr.flex8 = frm; 654 log.u_bbr.flex1 = tcp_data_len; 655 if (m) 656 log.u_bbr.flex2 = m->m_pkthdr.len; 657 else 658 log.u_bbr.flex2 = 0; 659 log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs; 660 log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len; 661 if (le->m_head) { 662 log.u_bbr.flex5 = le->m_head->m_pkthdr.len; 663 log.u_bbr.delRate = le->m_head->m_flags; 664 log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp; 665 } 666 log.u_bbr.inflight = th_seq; 667 log.u_bbr.delivered = th_ack; 668 log.u_bbr.timeStamp = cts; 669 log.u_bbr.epoch = le->next_seq; 670 log.u_bbr.lt_epoch = le->ack_seq; 671 log.u_bbr.pacing_gain = th_win; 672 log.u_bbr.cwnd_gain = le->window; 673 log.u_bbr.lost = curcpu; 674 log.u_bbr.cur_del_rate = (uintptr_t)m; 675 log.u_bbr.bw_inuse = (uintptr_t)le->m_head; 676 bintime2timeval(&lc->lro_last_queue_time, &btv); 677 log.u_bbr.flex6 = tcp_tv_to_usectick(&btv); 678 log.u_bbr.flex7 = le->compressed; 679 log.u_bbr.pacing_gain = le->uncompressed; 680 if (in_epoch(net_epoch_preempt)) 681 log.u_bbr.inhpts = 1; 682 else 683 log.u_bbr.inhpts = 0; 684 TCP_LOG_EVENTP(tp, NULL, 685 &tp->t_inpcb->inp_socket->so_rcv, 686 &tp->t_inpcb->inp_socket->so_snd, 687 TCP_LOG_LRO, 0, 688 0, &log, false, &tv); 689 } 690 } 691 #endif 692 693 static inline void 694 tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum) 695 { 696 uint32_t csum; 697 698 csum = 0xffff - *ptr + value; 699 while (csum > 0xffff) 700 csum = (csum >> 16) + (csum & 0xffff); 701 *ptr = value; 702 *psum = csum; 703 } 704 705 static uint16_t 706 tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le, 707 uint16_t payload_len, uint16_t delta_sum) 708 { 709 uint32_t csum; 710 uint16_t tlen; 711 uint16_t temp[5] = {}; 712 713 switch (pa->data.lro_type) { 714 case LRO_TYPE_IPV4_TCP: 715 /* Compute new IPv4 length. */ 716 tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len; 717 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 718 719 /* Subtract delta from current IPv4 checksum. */ 720 csum = pa->ip4->ip_sum + 0xffff - temp[0]; 721 while (csum > 0xffff) 722 csum = (csum >> 16) + (csum & 0xffff); 723 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 724 goto update_tcp_header; 725 726 case LRO_TYPE_IPV6_TCP: 727 /* Compute new IPv6 length. */ 728 tlen = (pa->tcp->th_off << 2) + payload_len; 729 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 730 goto update_tcp_header; 731 732 case LRO_TYPE_IPV4_UDP: 733 /* Compute new IPv4 length. */ 734 tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len; 735 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 736 737 /* Subtract delta from current IPv4 checksum. */ 738 csum = pa->ip4->ip_sum + 0xffff - temp[0]; 739 while (csum > 0xffff) 740 csum = (csum >> 16) + (csum & 0xffff); 741 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 742 goto update_udp_header; 743 744 case LRO_TYPE_IPV6_UDP: 745 /* Compute new IPv6 length. */ 746 tlen = sizeof(*pa->udp) + payload_len; 747 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 748 goto update_udp_header; 749 750 default: 751 return (0); 752 } 753 754 update_tcp_header: 755 /* Compute current TCP header checksum. */ 756 temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp); 757 758 /* Incorporate the latest ACK into the TCP header. */ 759 pa->tcp->th_ack = le->ack_seq; 760 pa->tcp->th_win = le->window; 761 762 /* Incorporate latest timestamp into the TCP header. */ 763 if (le->timestamp != 0) { 764 uint32_t *ts_ptr; 765 766 ts_ptr = (uint32_t *)(pa->tcp + 1); 767 ts_ptr[1] = htonl(le->tsval); 768 ts_ptr[2] = le->tsecr; 769 } 770 771 /* Compute new TCP header checksum. */ 772 temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp); 773 774 /* Compute new TCP checksum. */ 775 csum = pa->tcp->th_sum + 0xffff - delta_sum + 776 0xffff - temp[0] + 0xffff - temp[3] + temp[2]; 777 while (csum > 0xffff) 778 csum = (csum >> 16) + (csum & 0xffff); 779 780 /* Assign new TCP checksum. */ 781 tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]); 782 783 /* Compute all modififications affecting next checksum. */ 784 csum = temp[0] + temp[1] + 0xffff - temp[2] + 785 temp[3] + temp[4] + delta_sum; 786 while (csum > 0xffff) 787 csum = (csum >> 16) + (csum & 0xffff); 788 789 /* Return delta checksum to next stage, if any. */ 790 return (csum); 791 792 update_udp_header: 793 tlen = sizeof(*pa->udp) + payload_len; 794 /* Assign new UDP length and compute checksum delta. */ 795 tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]); 796 797 /* Check if there is a UDP checksum. */ 798 if (__predict_false(pa->udp->uh_sum != 0)) { 799 /* Compute new UDP checksum. */ 800 csum = pa->udp->uh_sum + 0xffff - delta_sum + 801 0xffff - temp[0] + 0xffff - temp[2]; 802 while (csum > 0xffff) 803 csum = (csum >> 16) + (csum & 0xffff); 804 /* Assign new UDP checksum. */ 805 tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]); 806 } 807 808 /* Compute all modififications affecting next checksum. */ 809 csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum; 810 while (csum > 0xffff) 811 csum = (csum >> 16) + (csum & 0xffff); 812 813 /* Return delta checksum to next stage, if any. */ 814 return (csum); 815 } 816 817 static void 818 tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le) 819 { 820 /* Check if we need to recompute any checksums. */ 821 if (le->m_head->m_pkthdr.lro_nsegs > 1) { 822 uint16_t csum; 823 824 switch (le->inner.data.lro_type) { 825 case LRO_TYPE_IPV4_TCP: 826 csum = tcp_lro_update_checksum(&le->inner, le, 827 le->m_head->m_pkthdr.lro_tcp_d_len, 828 le->m_head->m_pkthdr.lro_tcp_d_csum); 829 csum = tcp_lro_update_checksum(&le->outer, NULL, 830 le->m_head->m_pkthdr.lro_tcp_d_len + 831 le->inner.total_hdr_len, csum); 832 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 833 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 834 le->m_head->m_pkthdr.csum_data = 0xffff; 835 break; 836 case LRO_TYPE_IPV6_TCP: 837 csum = tcp_lro_update_checksum(&le->inner, le, 838 le->m_head->m_pkthdr.lro_tcp_d_len, 839 le->m_head->m_pkthdr.lro_tcp_d_csum); 840 csum = tcp_lro_update_checksum(&le->outer, NULL, 841 le->m_head->m_pkthdr.lro_tcp_d_len + 842 le->inner.total_hdr_len, csum); 843 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 844 CSUM_PSEUDO_HDR; 845 le->m_head->m_pkthdr.csum_data = 0xffff; 846 break; 847 case LRO_TYPE_NONE: 848 switch (le->outer.data.lro_type) { 849 case LRO_TYPE_IPV4_TCP: 850 csum = tcp_lro_update_checksum(&le->outer, le, 851 le->m_head->m_pkthdr.lro_tcp_d_len, 852 le->m_head->m_pkthdr.lro_tcp_d_csum); 853 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 854 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 855 le->m_head->m_pkthdr.csum_data = 0xffff; 856 break; 857 case LRO_TYPE_IPV6_TCP: 858 csum = tcp_lro_update_checksum(&le->outer, le, 859 le->m_head->m_pkthdr.lro_tcp_d_len, 860 le->m_head->m_pkthdr.lro_tcp_d_csum); 861 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 862 CSUM_PSEUDO_HDR; 863 le->m_head->m_pkthdr.csum_data = 0xffff; 864 break; 865 default: 866 break; 867 } 868 break; 869 default: 870 break; 871 } 872 } 873 874 /* 875 * Break any chain, this is not set to NULL on the singleton 876 * case m_nextpkt points to m_head. Other case set them 877 * m_nextpkt to NULL in push_and_replace. 878 */ 879 le->m_head->m_nextpkt = NULL; 880 lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs; 881 (*lc->ifp->if_input)(lc->ifp, le->m_head); 882 } 883 884 static void 885 tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, 886 struct mbuf *m, struct tcphdr *th) 887 { 888 uint32_t *ts_ptr; 889 uint16_t tcp_data_len; 890 uint16_t tcp_opt_len; 891 892 ts_ptr = (uint32_t *)(th + 1); 893 tcp_opt_len = (th->th_off << 2); 894 tcp_opt_len -= sizeof(*th); 895 896 /* Check if there is a timestamp option. */ 897 if (tcp_opt_len == 0 || 898 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 899 *ts_ptr != TCP_LRO_TS_OPTION)) { 900 /* We failed to find the timestamp option. */ 901 le->timestamp = 0; 902 } else { 903 le->timestamp = 1; 904 le->tsval = ntohl(*(ts_ptr + 1)); 905 le->tsecr = *(ts_ptr + 2); 906 } 907 908 tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 909 910 /* Pull out TCP sequence numbers and window size. */ 911 le->next_seq = ntohl(th->th_seq) + tcp_data_len; 912 le->ack_seq = th->th_ack; 913 le->window = th->th_win; 914 915 /* Setup new data pointers. */ 916 le->m_head = m; 917 le->m_tail = m_last(m); 918 } 919 920 static void 921 tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m) 922 { 923 struct lro_parser *pa; 924 925 /* 926 * Push up the stack of the current entry 927 * and replace it with "m". 928 */ 929 struct mbuf *msave; 930 931 /* Grab off the next and save it */ 932 msave = le->m_head->m_nextpkt; 933 le->m_head->m_nextpkt = NULL; 934 935 /* Now push out the old entry */ 936 tcp_flush_out_entry(lc, le); 937 938 /* Re-parse new header, should not fail. */ 939 pa = tcp_lro_parser(m, &le->outer, &le->inner, false); 940 KASSERT(pa != NULL, 941 ("tcp_push_and_replace: LRO parser failed on m=%p\n", m)); 942 943 /* 944 * Now to replace the data properly in the entry 945 * we have to reset the TCP header and 946 * other fields. 947 */ 948 tcp_set_entry_to_mbuf(lc, le, m, pa->tcp); 949 950 /* Restore the next list */ 951 m->m_nextpkt = msave; 952 } 953 954 static void 955 tcp_lro_mbuf_append_pkthdr(struct mbuf *m, const struct mbuf *p) 956 { 957 uint32_t csum; 958 959 if (m->m_pkthdr.lro_nsegs == 1) { 960 /* Compute relative checksum. */ 961 csum = p->m_pkthdr.lro_tcp_d_csum; 962 } else { 963 /* Merge TCP data checksums. */ 964 csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum + 965 (uint32_t)p->m_pkthdr.lro_tcp_d_csum; 966 while (csum > 0xffff) 967 csum = (csum >> 16) + (csum & 0xffff); 968 } 969 970 /* Update various counters. */ 971 m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len; 972 m->m_pkthdr.lro_tcp_d_csum = csum; 973 m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len; 974 m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs; 975 } 976 977 static void 978 tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le) 979 { 980 /* 981 * Walk through the mbuf chain we 982 * have on tap and compress/condense 983 * as required. 984 */ 985 uint32_t *ts_ptr; 986 struct mbuf *m; 987 struct tcphdr *th; 988 uint32_t tcp_data_len_total; 989 uint32_t tcp_data_seg_total; 990 uint16_t tcp_data_len; 991 uint16_t tcp_opt_len; 992 993 /* 994 * First we must check the lead (m_head) 995 * we must make sure that it is *not* 996 * something that should be sent up 997 * right away (sack etc). 998 */ 999 again: 1000 m = le->m_head->m_nextpkt; 1001 if (m == NULL) { 1002 /* Just one left. */ 1003 return; 1004 } 1005 1006 th = tcp_lro_get_th(m); 1007 tcp_opt_len = (th->th_off << 2); 1008 tcp_opt_len -= sizeof(*th); 1009 ts_ptr = (uint32_t *)(th + 1); 1010 1011 if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 1012 *ts_ptr != TCP_LRO_TS_OPTION)) { 1013 /* 1014 * Its not the timestamp. We can't 1015 * use this guy as the head. 1016 */ 1017 le->m_head->m_nextpkt = m->m_nextpkt; 1018 tcp_push_and_replace(lc, le, m); 1019 goto again; 1020 } 1021 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) { 1022 /* 1023 * Make sure that previously seen segements/ACKs are delivered 1024 * before this segment, e.g. FIN. 1025 */ 1026 le->m_head->m_nextpkt = m->m_nextpkt; 1027 tcp_push_and_replace(lc, le, m); 1028 goto again; 1029 } 1030 while((m = le->m_head->m_nextpkt) != NULL) { 1031 /* 1032 * condense m into le, first 1033 * pull m out of the list. 1034 */ 1035 le->m_head->m_nextpkt = m->m_nextpkt; 1036 m->m_nextpkt = NULL; 1037 /* Setup my data */ 1038 tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 1039 th = tcp_lro_get_th(m); 1040 ts_ptr = (uint32_t *)(th + 1); 1041 tcp_opt_len = (th->th_off << 2); 1042 tcp_opt_len -= sizeof(*th); 1043 tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len; 1044 tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs; 1045 1046 if (tcp_data_seg_total >= lc->lro_ackcnt_lim || 1047 tcp_data_len_total >= lc->lro_length_lim) { 1048 /* Flush now if appending will result in overflow. */ 1049 tcp_push_and_replace(lc, le, m); 1050 goto again; 1051 } 1052 if (tcp_opt_len != 0 && 1053 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 1054 *ts_ptr != TCP_LRO_TS_OPTION)) { 1055 /* 1056 * Maybe a sack in the new one? We need to 1057 * start all over after flushing the 1058 * current le. We will go up to the beginning 1059 * and flush it (calling the replace again possibly 1060 * or just returning). 1061 */ 1062 tcp_push_and_replace(lc, le, m); 1063 goto again; 1064 } 1065 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) { 1066 tcp_push_and_replace(lc, le, m); 1067 goto again; 1068 } 1069 if (tcp_opt_len != 0) { 1070 uint32_t tsval = ntohl(*(ts_ptr + 1)); 1071 /* Make sure timestamp values are increasing. */ 1072 if (TSTMP_GT(le->tsval, tsval)) { 1073 tcp_push_and_replace(lc, le, m); 1074 goto again; 1075 } 1076 le->tsval = tsval; 1077 le->tsecr = *(ts_ptr + 2); 1078 } 1079 /* Try to append the new segment. */ 1080 if (__predict_false(ntohl(th->th_seq) != le->next_seq || 1081 (tcp_data_len == 0 && 1082 le->ack_seq == th->th_ack && 1083 le->window == th->th_win))) { 1084 /* Out of order packet or duplicate ACK. */ 1085 tcp_push_and_replace(lc, le, m); 1086 goto again; 1087 } 1088 if (tcp_data_len != 0 || 1089 SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1090 le->next_seq += tcp_data_len; 1091 le->ack_seq = th->th_ack; 1092 le->window = th->th_win; 1093 } else if (th->th_ack == le->ack_seq) { 1094 le->window = WIN_MAX(le->window, th->th_win); 1095 } 1096 1097 if (tcp_data_len == 0) { 1098 m_freem(m); 1099 continue; 1100 } 1101 1102 /* Merge TCP data checksum and length to head mbuf. */ 1103 tcp_lro_mbuf_append_pkthdr(le->m_head, m); 1104 1105 /* 1106 * Adjust the mbuf so that m_data points to the first byte of 1107 * the ULP payload. Adjust the mbuf to avoid complications and 1108 * append new segment to existing mbuf chain. 1109 */ 1110 m_adj(m, m->m_pkthdr.len - tcp_data_len); 1111 m_demote_pkthdr(m); 1112 le->m_tail->m_next = m; 1113 le->m_tail = m_last(m); 1114 } 1115 } 1116 1117 #ifdef TCPHPTS 1118 static void 1119 tcp_queue_pkts(struct inpcb *inp, struct tcpcb *tp, struct lro_entry *le) 1120 { 1121 INP_WLOCK_ASSERT(inp); 1122 if (tp->t_in_pkt == NULL) { 1123 /* Nothing yet there */ 1124 tp->t_in_pkt = le->m_head; 1125 tp->t_tail_pkt = le->m_last_mbuf; 1126 } else { 1127 /* Already some there */ 1128 tp->t_tail_pkt->m_nextpkt = le->m_head; 1129 tp->t_tail_pkt = le->m_last_mbuf; 1130 } 1131 le->m_head = NULL; 1132 le->m_last_mbuf = NULL; 1133 } 1134 1135 static struct mbuf * 1136 tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le, 1137 struct inpcb *inp, int32_t *new_m) 1138 { 1139 struct tcpcb *tp; 1140 struct mbuf *m; 1141 1142 tp = intotcpcb(inp); 1143 if (__predict_false(tp == NULL)) 1144 return (NULL); 1145 1146 /* Look at the last mbuf if any in queue */ 1147 m = tp->t_tail_pkt; 1148 if (m != NULL && (m->m_flags & M_ACKCMP) != 0) { 1149 if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) { 1150 tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0); 1151 *new_m = 0; 1152 counter_u64_add(tcp_extra_mbuf, 1); 1153 return (m); 1154 } else { 1155 /* Mark we ran out of space */ 1156 inp->inp_flags2 |= INP_MBUF_L_ACKS; 1157 } 1158 } 1159 /* Decide mbuf size. */ 1160 if (inp->inp_flags2 & INP_MBUF_L_ACKS) 1161 m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR); 1162 else 1163 m = m_gethdr(M_NOWAIT, MT_DATA); 1164 1165 if (__predict_false(m == NULL)) { 1166 counter_u64_add(tcp_would_have_but, 1); 1167 return (NULL); 1168 } 1169 counter_u64_add(tcp_comp_total, 1); 1170 m->m_flags |= M_ACKCMP; 1171 *new_m = 1; 1172 return (m); 1173 } 1174 1175 static struct inpcb * 1176 tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa) 1177 { 1178 struct inpcb *inp; 1179 1180 NET_EPOCH_ASSERT(); 1181 1182 switch (pa->data.lro_type) { 1183 #ifdef INET6 1184 case LRO_TYPE_IPV6_TCP: 1185 inp = in6_pcblookup(&V_tcbinfo, 1186 &pa->data.s_addr.v6, 1187 pa->data.s_port, 1188 &pa->data.d_addr.v6, 1189 pa->data.d_port, 1190 INPLOOKUP_WLOCKPCB, 1191 ifp); 1192 break; 1193 #endif 1194 #ifdef INET 1195 case LRO_TYPE_IPV4_TCP: 1196 inp = in_pcblookup(&V_tcbinfo, 1197 pa->data.s_addr.v4, 1198 pa->data.s_port, 1199 pa->data.d_addr.v4, 1200 pa->data.d_port, 1201 INPLOOKUP_WLOCKPCB, 1202 ifp); 1203 break; 1204 #endif 1205 default: 1206 inp = NULL; 1207 break; 1208 } 1209 return (inp); 1210 } 1211 1212 static inline bool 1213 tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts) 1214 { 1215 /* 1216 * This function returns two bits of valuable information. 1217 * a) Is what is present capable of being ack-compressed, 1218 * we can ack-compress if there is no options or just 1219 * a timestamp option, and of course the th_flags must 1220 * be correct as well. 1221 * b) Our other options present such as SACK. This is 1222 * used to determine if we want to wakeup or not. 1223 */ 1224 bool ret = true; 1225 1226 switch (th->th_off << 2) { 1227 case (sizeof(*th) + TCPOLEN_TSTAMP_APPA): 1228 *ppts = (uint32_t *)(th + 1); 1229 /* Check if we have only one timestamp option. */ 1230 if (**ppts == TCP_LRO_TS_OPTION) 1231 *other_opts = false; 1232 else { 1233 *other_opts = true; 1234 ret = false; 1235 } 1236 break; 1237 case (sizeof(*th)): 1238 /* No options. */ 1239 *ppts = NULL; 1240 *other_opts = false; 1241 break; 1242 default: 1243 *ppts = NULL; 1244 *other_opts = true; 1245 ret = false; 1246 break; 1247 } 1248 /* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */ 1249 if ((th->th_flags & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0) 1250 ret = false; 1251 /* If it has data on it we cannot compress it */ 1252 if (m->m_pkthdr.lro_tcp_d_len) 1253 ret = false; 1254 1255 /* ACK flag must be set. */ 1256 if (!(th->th_flags & TH_ACK)) 1257 ret = false; 1258 return (ret); 1259 } 1260 1261 static int 1262 tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le) 1263 { 1264 struct inpcb *inp; 1265 struct tcpcb *tp; 1266 struct mbuf **pp, *cmp, *mv_to; 1267 bool bpf_req, should_wake; 1268 1269 /* Check if packet doesn't belongs to our network interface. */ 1270 if ((tcplro_stacks_wanting_mbufq == 0) || 1271 (le->outer.data.vlan_id != 0) || 1272 (le->inner.data.lro_type != LRO_TYPE_NONE)) 1273 return (TCP_LRO_CANNOT); 1274 1275 #ifdef INET6 1276 /* 1277 * Be proactive about unspecified IPv6 address in source. As 1278 * we use all-zero to indicate unbounded/unconnected pcb, 1279 * unspecified IPv6 address can be used to confuse us. 1280 * 1281 * Note that packets with unspecified IPv6 destination is 1282 * already dropped in ip6_input. 1283 */ 1284 if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP && 1285 IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6))) 1286 return (TCP_LRO_CANNOT); 1287 1288 if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP && 1289 IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6))) 1290 return (TCP_LRO_CANNOT); 1291 #endif 1292 /* Lookup inp, if any. */ 1293 inp = tcp_lro_lookup(lc->ifp, 1294 (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner); 1295 if (inp == NULL) 1296 return (TCP_LRO_CANNOT); 1297 1298 counter_u64_add(tcp_inp_lro_locks_taken, 1); 1299 1300 /* Get TCP control structure. */ 1301 tp = intotcpcb(inp); 1302 1303 /* Check if the inp is dead, Jim. */ 1304 if (tp == NULL || 1305 (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) || 1306 (inp->inp_flags2 & INP_FREED)) { 1307 INP_WUNLOCK(inp); 1308 return (TCP_LRO_CANNOT); 1309 } 1310 if ((inp->inp_irq_cpu_set == 0) && (lc->lro_cpu_is_set == 1)) { 1311 inp->inp_irq_cpu = lc->lro_last_cpu; 1312 inp->inp_irq_cpu_set = 1; 1313 } 1314 /* Check if the transport doesn't support the needed optimizations. */ 1315 if ((inp->inp_flags2 & (INP_SUPPORTS_MBUFQ | INP_MBUF_ACKCMP)) == 0) { 1316 INP_WUNLOCK(inp); 1317 return (TCP_LRO_CANNOT); 1318 } 1319 1320 if (inp->inp_flags2 & INP_MBUF_QUEUE_READY) 1321 should_wake = false; 1322 else 1323 should_wake = true; 1324 /* Check if packets should be tapped to BPF. */ 1325 bpf_req = bpf_peers_present(lc->ifp->if_bpf); 1326 1327 /* Strip and compress all the incoming packets. */ 1328 cmp = NULL; 1329 for (pp = &le->m_head; *pp != NULL; ) { 1330 mv_to = NULL; 1331 if (do_bpf_strip_and_compress(inp, lc, le, pp, 1332 &cmp, &mv_to, &should_wake, bpf_req ) == false) { 1333 /* Advance to next mbuf. */ 1334 pp = &(*pp)->m_nextpkt; 1335 } else if (mv_to != NULL) { 1336 /* We are asked to move pp up */ 1337 pp = &mv_to->m_nextpkt; 1338 } 1339 } 1340 /* Update "m_last_mbuf", if any. */ 1341 if (pp == &le->m_head) 1342 le->m_last_mbuf = *pp; 1343 else 1344 le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt); 1345 1346 /* Check if any data mbufs left. */ 1347 if (le->m_head != NULL) { 1348 counter_u64_add(tcp_inp_lro_direct_queue, 1); 1349 tcp_lro_log(tp, lc, le, NULL, 22, 1, 1350 inp->inp_flags2, inp->inp_in_input, 1); 1351 tcp_queue_pkts(inp, tp, le); 1352 } 1353 if (should_wake) { 1354 /* Wakeup */ 1355 counter_u64_add(tcp_inp_lro_wokeup_queue, 1); 1356 if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0)) 1357 inp = NULL; 1358 } 1359 if (inp != NULL) 1360 INP_WUNLOCK(inp); 1361 return (0); /* Success. */ 1362 } 1363 #endif 1364 1365 void 1366 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 1367 { 1368 /* Only optimise if there are multiple packets waiting. */ 1369 #ifdef TCPHPTS 1370 int error; 1371 1372 CURVNET_SET(lc->ifp->if_vnet); 1373 error = tcp_lro_flush_tcphpts(lc, le); 1374 CURVNET_RESTORE(); 1375 if (error != 0) { 1376 #endif 1377 tcp_lro_condense(lc, le); 1378 tcp_flush_out_entry(lc, le); 1379 #ifdef TCPHPTS 1380 } 1381 #endif 1382 lc->lro_flushed++; 1383 bzero(le, sizeof(*le)); 1384 LIST_INSERT_HEAD(&lc->lro_free, le, next); 1385 } 1386 1387 #ifdef HAVE_INLINE_FLSLL 1388 #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1)) 1389 #else 1390 static inline uint64_t 1391 tcp_lro_msb_64(uint64_t x) 1392 { 1393 x |= (x >> 1); 1394 x |= (x >> 2); 1395 x |= (x >> 4); 1396 x |= (x >> 8); 1397 x |= (x >> 16); 1398 x |= (x >> 32); 1399 return (x & ~(x >> 1)); 1400 } 1401 #endif 1402 1403 /* 1404 * The tcp_lro_sort() routine is comparable to qsort(), except it has 1405 * a worst case complexity limit of O(MIN(N,64)*N), where N is the 1406 * number of elements to sort and 64 is the number of sequence bits 1407 * available. The algorithm is bit-slicing the 64-bit sequence number, 1408 * sorting one bit at a time from the most significant bit until the 1409 * least significant one, skipping the constant bits. This is 1410 * typically called a radix sort. 1411 */ 1412 static void 1413 tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size) 1414 { 1415 struct lro_mbuf_sort temp; 1416 uint64_t ones; 1417 uint64_t zeros; 1418 uint32_t x; 1419 uint32_t y; 1420 1421 repeat: 1422 /* for small arrays insertion sort is faster */ 1423 if (size <= 12) { 1424 for (x = 1; x < size; x++) { 1425 temp = parray[x]; 1426 for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--) 1427 parray[y] = parray[y - 1]; 1428 parray[y] = temp; 1429 } 1430 return; 1431 } 1432 1433 /* compute sequence bits which are constant */ 1434 ones = 0; 1435 zeros = 0; 1436 for (x = 0; x != size; x++) { 1437 ones |= parray[x].seq; 1438 zeros |= ~parray[x].seq; 1439 } 1440 1441 /* compute bits which are not constant into "ones" */ 1442 ones &= zeros; 1443 if (ones == 0) 1444 return; 1445 1446 /* pick the most significant bit which is not constant */ 1447 ones = tcp_lro_msb_64(ones); 1448 1449 /* 1450 * Move entries having cleared sequence bits to the beginning 1451 * of the array: 1452 */ 1453 for (x = y = 0; y != size; y++) { 1454 /* skip set bits */ 1455 if (parray[y].seq & ones) 1456 continue; 1457 /* swap entries */ 1458 temp = parray[x]; 1459 parray[x] = parray[y]; 1460 parray[y] = temp; 1461 x++; 1462 } 1463 1464 KASSERT(x != 0 && x != size, ("Memory is corrupted\n")); 1465 1466 /* sort zeros */ 1467 tcp_lro_sort(parray, x); 1468 1469 /* sort ones */ 1470 parray += x; 1471 size -= x; 1472 goto repeat; 1473 } 1474 1475 void 1476 tcp_lro_flush_all(struct lro_ctrl *lc) 1477 { 1478 uint64_t seq; 1479 uint64_t nseq; 1480 unsigned x; 1481 1482 /* check if no mbufs to flush */ 1483 if (lc->lro_mbuf_count == 0) 1484 goto done; 1485 if (lc->lro_cpu_is_set == 0) { 1486 if (lc->lro_last_cpu == curcpu) { 1487 lc->lro_cnt_of_same_cpu++; 1488 /* Have we reached the threshold to declare a cpu? */ 1489 if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh) 1490 lc->lro_cpu_is_set = 1; 1491 } else { 1492 lc->lro_last_cpu = curcpu; 1493 lc->lro_cnt_of_same_cpu = 0; 1494 } 1495 } 1496 CURVNET_SET(lc->ifp->if_vnet); 1497 1498 /* get current time */ 1499 binuptime(&lc->lro_last_queue_time); 1500 1501 /* sort all mbufs according to stream */ 1502 tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count); 1503 1504 /* input data into LRO engine, stream by stream */ 1505 seq = 0; 1506 for (x = 0; x != lc->lro_mbuf_count; x++) { 1507 struct mbuf *mb; 1508 1509 /* get mbuf */ 1510 mb = lc->lro_mbuf_data[x].mb; 1511 1512 /* get sequence number, masking away the packet index */ 1513 nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24); 1514 1515 /* check for new stream */ 1516 if (seq != nseq) { 1517 seq = nseq; 1518 1519 /* flush active streams */ 1520 tcp_lro_rx_done(lc); 1521 } 1522 1523 /* add packet to LRO engine */ 1524 if (tcp_lro_rx_common(lc, mb, 0, false) != 0) { 1525 /* input packet to network layer */ 1526 (*lc->ifp->if_input)(lc->ifp, mb); 1527 lc->lro_queued++; 1528 lc->lro_flushed++; 1529 } 1530 } 1531 CURVNET_RESTORE(); 1532 done: 1533 /* flush active streams */ 1534 tcp_lro_rx_done(lc); 1535 1536 #ifdef TCPHPTS 1537 tcp_run_hpts(); 1538 #endif 1539 lc->lro_mbuf_count = 0; 1540 } 1541 1542 #ifdef TCPHPTS 1543 static void 1544 build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m, 1545 uint32_t *ts_ptr, uint16_t iptos) 1546 { 1547 /* 1548 * Given a TCP ACK, summarize it down into the small TCP ACK 1549 * entry. 1550 */ 1551 ae->timestamp = m->m_pkthdr.rcv_tstmp; 1552 if (m->m_flags & M_TSTMP_LRO) 1553 ae->flags = TSTMP_LRO; 1554 else if (m->m_flags & M_TSTMP) 1555 ae->flags = TSTMP_HDWR; 1556 ae->seq = ntohl(th->th_seq); 1557 ae->ack = ntohl(th->th_ack); 1558 ae->flags |= th->th_flags; 1559 if (ts_ptr != NULL) { 1560 ae->ts_value = ntohl(ts_ptr[1]); 1561 ae->ts_echo = ntohl(ts_ptr[2]); 1562 ae->flags |= HAS_TSTMP; 1563 } 1564 ae->win = ntohs(th->th_win); 1565 ae->codepoint = iptos; 1566 } 1567 1568 /* 1569 * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets 1570 * and strip all, but the IPv4/IPv6 header. 1571 */ 1572 static bool 1573 do_bpf_strip_and_compress(struct inpcb *inp, struct lro_ctrl *lc, 1574 struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to, 1575 bool *should_wake, bool bpf_req) 1576 { 1577 union { 1578 void *ptr; 1579 struct ip *ip4; 1580 struct ip6_hdr *ip6; 1581 } l3; 1582 struct mbuf *m; 1583 struct mbuf *nm; 1584 struct tcphdr *th; 1585 struct tcp_ackent *ack_ent; 1586 uint32_t *ts_ptr; 1587 int32_t n_mbuf; 1588 bool other_opts, can_compress; 1589 uint8_t lro_type; 1590 uint16_t iptos; 1591 int tcp_hdr_offset; 1592 int idx; 1593 1594 /* Get current mbuf. */ 1595 m = *pp; 1596 1597 /* Let the BPF see the packet */ 1598 if (__predict_false(bpf_req)) 1599 ETHER_BPF_MTAP(lc->ifp, m); 1600 1601 tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off; 1602 lro_type = le->inner.data.lro_type; 1603 switch (lro_type) { 1604 case LRO_TYPE_NONE: 1605 lro_type = le->outer.data.lro_type; 1606 switch (lro_type) { 1607 case LRO_TYPE_IPV4_TCP: 1608 tcp_hdr_offset -= sizeof(*le->outer.ip4); 1609 m->m_pkthdr.lro_etype = ETHERTYPE_IP; 1610 break; 1611 case LRO_TYPE_IPV6_TCP: 1612 tcp_hdr_offset -= sizeof(*le->outer.ip6); 1613 m->m_pkthdr.lro_etype = ETHERTYPE_IPV6; 1614 break; 1615 default: 1616 goto compressed; 1617 } 1618 break; 1619 case LRO_TYPE_IPV4_TCP: 1620 tcp_hdr_offset -= sizeof(*le->outer.ip4); 1621 m->m_pkthdr.lro_etype = ETHERTYPE_IP; 1622 break; 1623 case LRO_TYPE_IPV6_TCP: 1624 tcp_hdr_offset -= sizeof(*le->outer.ip6); 1625 m->m_pkthdr.lro_etype = ETHERTYPE_IPV6; 1626 break; 1627 default: 1628 goto compressed; 1629 } 1630 1631 MPASS(tcp_hdr_offset >= 0); 1632 1633 m_adj(m, tcp_hdr_offset); 1634 m->m_flags |= M_LRO_EHDRSTRP; 1635 m->m_flags &= ~M_ACKCMP; 1636 m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset; 1637 1638 th = tcp_lro_get_th(m); 1639 1640 th->th_sum = 0; /* TCP checksum is valid. */ 1641 1642 /* Check if ACK can be compressed */ 1643 can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts); 1644 1645 /* Now lets look at the should wake states */ 1646 if ((other_opts == true) && 1647 ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) == 0)) { 1648 /* 1649 * If there are other options (SACK?) and the 1650 * tcp endpoint has not expressly told us it does 1651 * not care about SACKS, then we should wake up. 1652 */ 1653 *should_wake = true; 1654 } 1655 /* Is the ack compressable? */ 1656 if (can_compress == false) 1657 goto done; 1658 /* Does the TCP endpoint support ACK compression? */ 1659 if ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0) 1660 goto done; 1661 1662 /* Lets get the TOS/traffic class field */ 1663 l3.ptr = mtod(m, void *); 1664 switch (lro_type) { 1665 case LRO_TYPE_IPV4_TCP: 1666 iptos = l3.ip4->ip_tos; 1667 break; 1668 case LRO_TYPE_IPV6_TCP: 1669 iptos = IPV6_TRAFFIC_CLASS(l3.ip6); 1670 break; 1671 default: 1672 iptos = 0; /* Keep compiler happy. */ 1673 break; 1674 } 1675 /* Now lets get space if we don't have some already */ 1676 if (*cmp == NULL) { 1677 new_one: 1678 nm = tcp_lro_get_last_if_ackcmp(lc, le, inp, &n_mbuf); 1679 if (__predict_false(nm == NULL)) 1680 goto done; 1681 *cmp = nm; 1682 if (n_mbuf) { 1683 /* 1684 * Link in the new cmp ack to our in-order place, 1685 * first set our cmp ack's next to where we are. 1686 */ 1687 nm->m_nextpkt = m; 1688 (*pp) = nm; 1689 /* 1690 * Set it up so mv_to is advanced to our 1691 * compressed ack. This way the caller can 1692 * advance pp to the right place. 1693 */ 1694 *mv_to = nm; 1695 /* 1696 * Advance it here locally as well. 1697 */ 1698 pp = &nm->m_nextpkt; 1699 } 1700 } else { 1701 /* We have one already we are working on */ 1702 nm = *cmp; 1703 if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) { 1704 /* We ran out of space */ 1705 inp->inp_flags2 |= INP_MBUF_L_ACKS; 1706 goto new_one; 1707 } 1708 } 1709 MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent)); 1710 counter_u64_add(tcp_inp_lro_compressed, 1); 1711 le->compressed++; 1712 /* We can add in to the one on the tail */ 1713 ack_ent = mtod(nm, struct tcp_ackent *); 1714 idx = (nm->m_len / sizeof(struct tcp_ackent)); 1715 build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos); 1716 1717 /* Bump the size of both pkt-hdr and len */ 1718 nm->m_len += sizeof(struct tcp_ackent); 1719 nm->m_pkthdr.len += sizeof(struct tcp_ackent); 1720 compressed: 1721 /* Advance to next mbuf before freeing. */ 1722 *pp = m->m_nextpkt; 1723 m->m_nextpkt = NULL; 1724 m_freem(m); 1725 return (true); 1726 done: 1727 counter_u64_add(tcp_uncomp_total, 1); 1728 le->uncompressed++; 1729 return (false); 1730 } 1731 #endif 1732 1733 static struct lro_head * 1734 tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser) 1735 { 1736 u_long hash; 1737 1738 if (M_HASHTYPE_ISHASH(m)) { 1739 hash = m->m_pkthdr.flowid; 1740 } else { 1741 for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++) 1742 hash += parser->data.raw[i]; 1743 } 1744 return (&lc->lro_hash[hash % lc->lro_hashsz]); 1745 } 1746 1747 static int 1748 tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash) 1749 { 1750 struct lro_parser pi; /* inner address data */ 1751 struct lro_parser po; /* outer address data */ 1752 struct lro_parser *pa; /* current parser for TCP stream */ 1753 struct lro_entry *le; 1754 struct lro_head *bucket; 1755 struct tcphdr *th; 1756 int tcp_data_len; 1757 int tcp_opt_len; 1758 int error; 1759 uint16_t tcp_data_sum; 1760 1761 #ifdef INET 1762 /* Quickly decide if packet cannot be LRO'ed */ 1763 if (__predict_false(V_ipforwarding != 0)) 1764 return (TCP_LRO_CANNOT); 1765 #endif 1766 #ifdef INET6 1767 /* Quickly decide if packet cannot be LRO'ed */ 1768 if (__predict_false(V_ip6_forwarding != 0)) 1769 return (TCP_LRO_CANNOT); 1770 #endif 1771 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1772 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1773 (m->m_pkthdr.csum_data != 0xffff)) { 1774 /* 1775 * The checksum either did not have hardware offload 1776 * or it was a bad checksum. We can't LRO such 1777 * a packet. 1778 */ 1779 counter_u64_add(tcp_bad_csums, 1); 1780 return (TCP_LRO_CANNOT); 1781 } 1782 /* We expect a contiguous header [eh, ip, tcp]. */ 1783 pa = tcp_lro_parser(m, &po, &pi, true); 1784 if (__predict_false(pa == NULL)) 1785 return (TCP_LRO_NOT_SUPPORTED); 1786 1787 /* We don't expect any padding. */ 1788 error = tcp_lro_trim_mbuf_chain(m, pa); 1789 if (__predict_false(error != 0)) 1790 return (error); 1791 1792 #ifdef INET 1793 switch (pa->data.lro_type) { 1794 case LRO_TYPE_IPV4_TCP: 1795 error = tcp_lro_rx_ipv4(lc, m, pa->ip4); 1796 if (__predict_false(error != 0)) 1797 return (error); 1798 break; 1799 default: 1800 break; 1801 } 1802 #endif 1803 /* If no hardware or arrival stamp on the packet add timestamp */ 1804 if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) { 1805 m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1806 m->m_flags |= M_TSTMP_LRO; 1807 } 1808 1809 /* Get pointer to TCP header. */ 1810 th = pa->tcp; 1811 1812 /* Don't process SYN packets. */ 1813 if (__predict_false(th->th_flags & TH_SYN)) 1814 return (TCP_LRO_CANNOT); 1815 1816 /* Get total TCP header length and compute payload length. */ 1817 tcp_opt_len = (th->th_off << 2); 1818 tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th - 1819 (uint8_t *)m->m_data) - tcp_opt_len; 1820 tcp_opt_len -= sizeof(*th); 1821 1822 /* Don't process invalid TCP headers. */ 1823 if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0)) 1824 return (TCP_LRO_CANNOT); 1825 1826 /* Compute TCP data only checksum. */ 1827 if (tcp_data_len == 0) 1828 tcp_data_sum = 0; /* no data, no checksum */ 1829 else if (__predict_false(csum != 0)) 1830 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum); 1831 else 1832 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum); 1833 1834 /* Save TCP info in mbuf. */ 1835 m->m_nextpkt = NULL; 1836 m->m_pkthdr.rcvif = lc->ifp; 1837 m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum; 1838 m->m_pkthdr.lro_tcp_d_len = tcp_data_len; 1839 m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data); 1840 m->m_pkthdr.lro_nsegs = 1; 1841 1842 /* Get hash bucket. */ 1843 if (!use_hash) { 1844 bucket = &lc->lro_hash[0]; 1845 } else { 1846 bucket = tcp_lro_rx_get_bucket(lc, m, pa); 1847 } 1848 1849 /* Try to find a matching previous segment. */ 1850 LIST_FOREACH(le, bucket, hash_next) { 1851 /* Compare addresses and ports. */ 1852 if (lro_address_compare(&po.data, &le->outer.data) == false || 1853 lro_address_compare(&pi.data, &le->inner.data) == false) 1854 continue; 1855 1856 /* Check if no data and old ACK. */ 1857 if (tcp_data_len == 0 && 1858 SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1859 m_freem(m); 1860 return (0); 1861 } 1862 1863 /* Mark "m" in the last spot. */ 1864 le->m_last_mbuf->m_nextpkt = m; 1865 /* Now set the tail to "m". */ 1866 le->m_last_mbuf = m; 1867 return (0); 1868 } 1869 1870 /* Try to find an empty slot. */ 1871 if (LIST_EMPTY(&lc->lro_free)) 1872 return (TCP_LRO_NO_ENTRIES); 1873 1874 /* Start a new segment chain. */ 1875 le = LIST_FIRST(&lc->lro_free); 1876 LIST_REMOVE(le, next); 1877 tcp_lro_active_insert(lc, bucket, le); 1878 1879 /* Make sure the headers are set. */ 1880 le->inner = pi; 1881 le->outer = po; 1882 1883 /* Store time this entry was allocated. */ 1884 le->alloc_time = lc->lro_last_queue_time; 1885 1886 tcp_set_entry_to_mbuf(lc, le, m, th); 1887 1888 /* Now set the tail to "m". */ 1889 le->m_last_mbuf = m; 1890 1891 return (0); 1892 } 1893 1894 int 1895 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 1896 { 1897 int error; 1898 1899 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1900 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1901 (m->m_pkthdr.csum_data != 0xffff)) { 1902 /* 1903 * The checksum either did not have hardware offload 1904 * or it was a bad checksum. We can't LRO such 1905 * a packet. 1906 */ 1907 counter_u64_add(tcp_bad_csums, 1); 1908 return (TCP_LRO_CANNOT); 1909 } 1910 /* get current time */ 1911 binuptime(&lc->lro_last_queue_time); 1912 CURVNET_SET(lc->ifp->if_vnet); 1913 error = tcp_lro_rx_common(lc, m, csum, true); 1914 CURVNET_RESTORE(); 1915 1916 return (error); 1917 } 1918 1919 void 1920 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 1921 { 1922 /* sanity checks */ 1923 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 1924 lc->lro_mbuf_max == 0)) { 1925 /* packet drop */ 1926 m_freem(mb); 1927 return; 1928 } 1929 1930 /* check if packet is not LRO capable */ 1931 if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 1932 /* input packet to network layer */ 1933 (*lc->ifp->if_input) (lc->ifp, mb); 1934 return; 1935 } 1936 1937 /* create sequence number */ 1938 lc->lro_mbuf_data[lc->lro_mbuf_count].seq = 1939 (((uint64_t)M_HASHTYPE_GET(mb)) << 56) | 1940 (((uint64_t)mb->m_pkthdr.flowid) << 24) | 1941 ((uint64_t)lc->lro_mbuf_count); 1942 1943 /* enter mbuf */ 1944 lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb; 1945 1946 /* flush if array is full */ 1947 if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max)) 1948 tcp_lro_flush_all(lc); 1949 } 1950 1951 /* end */ 1952