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