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