1 /*- 2 * Copyright (c) 2007, Myricom Inc. 3 * Copyright (c) 2008, Intel Corporation. 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * Copyright (c) 2016 Mellanox Technologies. 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Bjoern Zeeb 9 * under sponsorship from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/ethernet.h> 49 #include <net/vnet.h> 50 51 #include <netinet/in_systm.h> 52 #include <netinet/in.h> 53 #include <netinet/ip6.h> 54 #include <netinet/ip.h> 55 #include <netinet/ip_var.h> 56 #include <netinet/tcp.h> 57 #include <netinet/tcp_lro.h> 58 59 #include <netinet6/ip6_var.h> 60 61 #include <machine/in_cksum.h> 62 63 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures"); 64 65 #define TCP_LRO_UPDATE_CSUM 1 66 #ifndef TCP_LRO_UPDATE_CSUM 67 #define TCP_LRO_INVALID_CSUM 0x0000 68 #endif 69 70 static void tcp_lro_rx_done(struct lro_ctrl *lc); 71 72 static __inline void 73 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_entry *le) 74 { 75 76 LIST_INSERT_HEAD(&lc->lro_active, le, next); 77 } 78 79 static __inline void 80 tcp_lro_active_remove(struct lro_entry *le) 81 { 82 83 LIST_REMOVE(le, next); 84 } 85 86 int 87 tcp_lro_init(struct lro_ctrl *lc) 88 { 89 return (tcp_lro_init_args(lc, NULL, TCP_LRO_ENTRIES, 0)); 90 } 91 92 int 93 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, 94 unsigned lro_entries, unsigned lro_mbufs) 95 { 96 struct lro_entry *le; 97 size_t size; 98 unsigned i; 99 100 lc->lro_bad_csum = 0; 101 lc->lro_queued = 0; 102 lc->lro_flushed = 0; 103 lc->lro_cnt = 0; 104 lc->lro_mbuf_count = 0; 105 lc->lro_mbuf_max = lro_mbufs; 106 lc->lro_cnt = lro_entries; 107 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX; 108 lc->lro_length_lim = TCP_LRO_LENGTH_MAX; 109 lc->ifp = ifp; 110 LIST_INIT(&lc->lro_free); 111 LIST_INIT(&lc->lro_active); 112 113 /* compute size to allocate */ 114 size = (lro_mbufs * sizeof(struct mbuf *)) + 115 (lro_entries * sizeof(*le)); 116 lc->lro_mbuf_data = (struct mbuf **) 117 malloc(size, M_LRO, M_NOWAIT | M_ZERO); 118 119 /* check for out of memory */ 120 if (lc->lro_mbuf_data == NULL) { 121 memset(lc, 0, sizeof(*lc)); 122 return (ENOMEM); 123 } 124 /* compute offset for LRO entries */ 125 le = (struct lro_entry *) 126 (lc->lro_mbuf_data + lro_mbufs); 127 128 /* setup linked list */ 129 for (i = 0; i != lro_entries; i++) 130 LIST_INSERT_HEAD(&lc->lro_free, le + i, next); 131 132 return (0); 133 } 134 135 void 136 tcp_lro_free(struct lro_ctrl *lc) 137 { 138 struct lro_entry *le; 139 unsigned x; 140 141 /* reset LRO free list */ 142 LIST_INIT(&lc->lro_free); 143 144 /* free active mbufs, if any */ 145 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 146 tcp_lro_active_remove(le); 147 m_freem(le->m_head); 148 } 149 150 /* free mbuf array, if any */ 151 for (x = 0; x != lc->lro_mbuf_count; x++) 152 m_freem(lc->lro_mbuf_data[x]); 153 lc->lro_mbuf_count = 0; 154 155 /* free allocated memory, if any */ 156 free(lc->lro_mbuf_data, M_LRO); 157 lc->lro_mbuf_data = NULL; 158 } 159 160 #ifdef TCP_LRO_UPDATE_CSUM 161 static uint16_t 162 tcp_lro_csum_th(struct tcphdr *th) 163 { 164 uint32_t ch; 165 uint16_t *p, l; 166 167 ch = th->th_sum = 0x0000; 168 l = th->th_off; 169 p = (uint16_t *)th; 170 while (l > 0) { 171 ch += *p; 172 p++; 173 ch += *p; 174 p++; 175 l--; 176 } 177 while (ch > 0xffff) 178 ch = (ch >> 16) + (ch & 0xffff); 179 180 return (ch & 0xffff); 181 } 182 183 static uint16_t 184 tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th, 185 uint16_t tcp_data_len, uint16_t csum) 186 { 187 uint32_t c; 188 uint16_t cs; 189 190 c = csum; 191 192 /* Remove length from checksum. */ 193 switch (le->eh_type) { 194 #ifdef INET6 195 case ETHERTYPE_IPV6: 196 { 197 struct ip6_hdr *ip6; 198 199 ip6 = (struct ip6_hdr *)l3hdr; 200 if (le->append_cnt == 0) 201 cs = ip6->ip6_plen; 202 else { 203 uint32_t cx; 204 205 cx = ntohs(ip6->ip6_plen); 206 cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0); 207 } 208 break; 209 } 210 #endif 211 #ifdef INET 212 case ETHERTYPE_IP: 213 { 214 struct ip *ip4; 215 216 ip4 = (struct ip *)l3hdr; 217 if (le->append_cnt == 0) 218 cs = ip4->ip_len; 219 else { 220 cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4), 221 IPPROTO_TCP); 222 cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr, 223 htons(cs)); 224 } 225 break; 226 } 227 #endif 228 default: 229 cs = 0; /* Keep compiler happy. */ 230 } 231 232 cs = ~cs; 233 c += cs; 234 235 /* Remove TCP header csum. */ 236 cs = ~tcp_lro_csum_th(th); 237 c += cs; 238 while (c > 0xffff) 239 c = (c >> 16) + (c & 0xffff); 240 241 return (c & 0xffff); 242 } 243 #endif 244 245 static void 246 tcp_lro_rx_done(struct lro_ctrl *lc) 247 { 248 struct lro_entry *le; 249 250 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 251 tcp_lro_active_remove(le); 252 tcp_lro_flush(lc, le); 253 } 254 } 255 256 void 257 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 258 { 259 struct lro_entry *le, *le_tmp; 260 struct timeval tv; 261 262 if (LIST_EMPTY(&lc->lro_active)) 263 return; 264 265 getmicrotime(&tv); 266 timevalsub(&tv, timeout); 267 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 268 if (timevalcmp(&tv, &le->mtime, >=)) { 269 tcp_lro_active_remove(le); 270 tcp_lro_flush(lc, le); 271 } 272 } 273 } 274 275 void 276 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 277 { 278 279 if (le->append_cnt > 0) { 280 struct tcphdr *th; 281 uint16_t p_len; 282 283 p_len = htons(le->p_len); 284 switch (le->eh_type) { 285 #ifdef INET6 286 case ETHERTYPE_IPV6: 287 { 288 struct ip6_hdr *ip6; 289 290 ip6 = le->le_ip6; 291 ip6->ip6_plen = p_len; 292 th = (struct tcphdr *)(ip6 + 1); 293 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 294 CSUM_PSEUDO_HDR; 295 le->p_len += ETHER_HDR_LEN + sizeof(*ip6); 296 break; 297 } 298 #endif 299 #ifdef INET 300 case ETHERTYPE_IP: 301 { 302 struct ip *ip4; 303 #ifdef TCP_LRO_UPDATE_CSUM 304 uint32_t cl; 305 uint16_t c; 306 #endif 307 308 ip4 = le->le_ip4; 309 #ifdef TCP_LRO_UPDATE_CSUM 310 /* Fix IP header checksum for new length. */ 311 c = ~ip4->ip_sum; 312 cl = c; 313 c = ~ip4->ip_len; 314 cl += c + p_len; 315 while (cl > 0xffff) 316 cl = (cl >> 16) + (cl & 0xffff); 317 c = cl; 318 ip4->ip_sum = ~c; 319 #else 320 ip4->ip_sum = TCP_LRO_INVALID_CSUM; 321 #endif 322 ip4->ip_len = p_len; 323 th = (struct tcphdr *)(ip4 + 1); 324 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 325 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 326 le->p_len += ETHER_HDR_LEN; 327 break; 328 } 329 #endif 330 default: 331 th = NULL; /* Keep compiler happy. */ 332 } 333 le->m_head->m_pkthdr.csum_data = 0xffff; 334 le->m_head->m_pkthdr.len = le->p_len; 335 336 /* Incorporate the latest ACK into the TCP header. */ 337 th->th_ack = le->ack_seq; 338 th->th_win = le->window; 339 /* Incorporate latest timestamp into the TCP header. */ 340 if (le->timestamp != 0) { 341 uint32_t *ts_ptr; 342 343 ts_ptr = (uint32_t *)(th + 1); 344 ts_ptr[1] = htonl(le->tsval); 345 ts_ptr[2] = le->tsecr; 346 } 347 #ifdef TCP_LRO_UPDATE_CSUM 348 /* Update the TCP header checksum. */ 349 le->ulp_csum += p_len; 350 le->ulp_csum += tcp_lro_csum_th(th); 351 while (le->ulp_csum > 0xffff) 352 le->ulp_csum = (le->ulp_csum >> 16) + 353 (le->ulp_csum & 0xffff); 354 th->th_sum = (le->ulp_csum & 0xffff); 355 th->th_sum = ~th->th_sum; 356 #else 357 th->th_sum = TCP_LRO_INVALID_CSUM; 358 #endif 359 } 360 361 (*lc->ifp->if_input)(lc->ifp, le->m_head); 362 lc->lro_queued += le->append_cnt + 1; 363 lc->lro_flushed++; 364 bzero(le, sizeof(*le)); 365 LIST_INSERT_HEAD(&lc->lro_free, le, next); 366 } 367 368 static int 369 tcp_lro_mbuf_compare_header(const void *ppa, const void *ppb) 370 { 371 const struct mbuf *ma = *((const struct mbuf * const *)ppa); 372 const struct mbuf *mb = *((const struct mbuf * const *)ppb); 373 int ret; 374 375 ret = M_HASHTYPE_GET(ma) - M_HASHTYPE_GET(mb); 376 if (ret != 0) 377 goto done; 378 379 if (ma->m_pkthdr.flowid > mb->m_pkthdr.flowid) 380 return (1); 381 else if (ma->m_pkthdr.flowid < mb->m_pkthdr.flowid) 382 return (-1); 383 384 ret = TCP_LRO_SEQUENCE(ma) - TCP_LRO_SEQUENCE(mb); 385 done: 386 return (ret); 387 } 388 389 void 390 tcp_lro_flush_all(struct lro_ctrl *lc) 391 { 392 uint32_t hashtype; 393 uint32_t flowid; 394 unsigned x; 395 396 /* check if no mbufs to flush */ 397 if (lc->lro_mbuf_count == 0) 398 goto done; 399 400 /* sort all mbufs according to stream */ 401 qsort(lc->lro_mbuf_data, lc->lro_mbuf_count, sizeof(struct mbuf *), 402 &tcp_lro_mbuf_compare_header); 403 404 /* input data into LRO engine, stream by stream */ 405 flowid = 0; 406 hashtype = M_HASHTYPE_NONE; 407 for (x = 0; x != lc->lro_mbuf_count; x++) { 408 struct mbuf *mb; 409 410 mb = lc->lro_mbuf_data[x]; 411 412 /* check for new stream */ 413 if (mb->m_pkthdr.flowid != flowid || 414 M_HASHTYPE_GET(mb) != hashtype) { 415 flowid = mb->m_pkthdr.flowid; 416 hashtype = M_HASHTYPE_GET(mb); 417 418 /* flush active streams */ 419 tcp_lro_rx_done(lc); 420 } 421 #ifdef TCP_LRO_RESET_SEQUENCE 422 /* reset sequence number */ 423 TCP_LRO_SEQUENCE(mb) = 0; 424 #endif 425 /* add packet to LRO engine */ 426 if (tcp_lro_rx(lc, mb, 0) != 0) { 427 /* input packet to network layer */ 428 (*lc->ifp->if_input)(lc->ifp, mb); 429 lc->lro_queued++; 430 lc->lro_flushed++; 431 } 432 } 433 done: 434 /* flush active streams */ 435 tcp_lro_rx_done(lc); 436 437 lc->lro_mbuf_count = 0; 438 } 439 440 #ifdef INET6 441 static int 442 tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6, 443 struct tcphdr **th) 444 { 445 446 /* XXX-BZ we should check the flow-label. */ 447 448 /* XXX-BZ We do not yet support ext. hdrs. */ 449 if (ip6->ip6_nxt != IPPROTO_TCP) 450 return (TCP_LRO_NOT_SUPPORTED); 451 452 /* Find the TCP header. */ 453 *th = (struct tcphdr *)(ip6 + 1); 454 455 return (0); 456 } 457 #endif 458 459 #ifdef INET 460 static int 461 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4, 462 struct tcphdr **th) 463 { 464 int csum_flags; 465 uint16_t csum; 466 467 if (ip4->ip_p != IPPROTO_TCP) 468 return (TCP_LRO_NOT_SUPPORTED); 469 470 /* Ensure there are no options. */ 471 if ((ip4->ip_hl << 2) != sizeof (*ip4)) 472 return (TCP_LRO_CANNOT); 473 474 /* .. and the packet is not fragmented. */ 475 if (ip4->ip_off & htons(IP_MF|IP_OFFMASK)) 476 return (TCP_LRO_CANNOT); 477 478 /* Legacy IP has a header checksum that needs to be correct. */ 479 csum_flags = m->m_pkthdr.csum_flags; 480 if (csum_flags & CSUM_IP_CHECKED) { 481 if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) { 482 lc->lro_bad_csum++; 483 return (TCP_LRO_CANNOT); 484 } 485 } else { 486 csum = in_cksum_hdr(ip4); 487 if (__predict_false((csum) != 0)) { 488 lc->lro_bad_csum++; 489 return (TCP_LRO_CANNOT); 490 } 491 } 492 493 /* Find the TCP header (we assured there are no IP options). */ 494 *th = (struct tcphdr *)(ip4 + 1); 495 496 return (0); 497 } 498 #endif 499 500 int 501 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 502 { 503 struct lro_entry *le; 504 struct ether_header *eh; 505 #ifdef INET6 506 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */ 507 #endif 508 #ifdef INET 509 struct ip *ip4 = NULL; /* Keep compiler happy. */ 510 #endif 511 struct tcphdr *th; 512 void *l3hdr = NULL; /* Keep compiler happy. */ 513 uint32_t *ts_ptr; 514 tcp_seq seq; 515 int error, ip_len, l; 516 uint16_t eh_type, tcp_data_len; 517 518 /* We expect a contiguous header [eh, ip, tcp]. */ 519 520 eh = mtod(m, struct ether_header *); 521 eh_type = ntohs(eh->ether_type); 522 switch (eh_type) { 523 #ifdef INET6 524 case ETHERTYPE_IPV6: 525 { 526 CURVNET_SET(lc->ifp->if_vnet); 527 if (V_ip6_forwarding != 0) { 528 /* XXX-BZ stats but changing lro_ctrl is a problem. */ 529 CURVNET_RESTORE(); 530 return (TCP_LRO_CANNOT); 531 } 532 CURVNET_RESTORE(); 533 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1); 534 error = tcp_lro_rx_ipv6(lc, m, ip6, &th); 535 if (error != 0) 536 return (error); 537 tcp_data_len = ntohs(ip6->ip6_plen); 538 ip_len = sizeof(*ip6) + tcp_data_len; 539 break; 540 } 541 #endif 542 #ifdef INET 543 case ETHERTYPE_IP: 544 { 545 CURVNET_SET(lc->ifp->if_vnet); 546 if (V_ipforwarding != 0) { 547 /* XXX-BZ stats but changing lro_ctrl is a problem. */ 548 CURVNET_RESTORE(); 549 return (TCP_LRO_CANNOT); 550 } 551 CURVNET_RESTORE(); 552 l3hdr = ip4 = (struct ip *)(eh + 1); 553 error = tcp_lro_rx_ipv4(lc, m, ip4, &th); 554 if (error != 0) 555 return (error); 556 ip_len = ntohs(ip4->ip_len); 557 tcp_data_len = ip_len - sizeof(*ip4); 558 break; 559 } 560 #endif 561 /* XXX-BZ what happens in case of VLAN(s)? */ 562 default: 563 return (TCP_LRO_NOT_SUPPORTED); 564 } 565 566 /* 567 * If the frame is padded beyond the end of the IP packet, then we must 568 * trim the extra bytes off. 569 */ 570 l = m->m_pkthdr.len - (ETHER_HDR_LEN + ip_len); 571 if (l != 0) { 572 if (l < 0) 573 /* Truncated packet. */ 574 return (TCP_LRO_CANNOT); 575 576 m_adj(m, -l); 577 } 578 579 /* 580 * Check TCP header constraints. 581 */ 582 /* Ensure no bits set besides ACK or PSH. */ 583 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) 584 return (TCP_LRO_CANNOT); 585 586 /* XXX-BZ We lose a ACK|PUSH flag concatenating multiple segments. */ 587 /* XXX-BZ Ideally we'd flush on PUSH? */ 588 589 /* 590 * Check for timestamps. 591 * Since the only option we handle are timestamps, we only have to 592 * handle the simple case of aligned timestamps. 593 */ 594 l = (th->th_off << 2); 595 tcp_data_len -= l; 596 l -= sizeof(*th); 597 ts_ptr = (uint32_t *)(th + 1); 598 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) || 599 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16| 600 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) 601 return (TCP_LRO_CANNOT); 602 603 /* If the driver did not pass in the checksum, set it now. */ 604 if (csum == 0x0000) 605 csum = th->th_sum; 606 607 seq = ntohl(th->th_seq); 608 609 /* Try to find a matching previous segment. */ 610 LIST_FOREACH(le, &lc->lro_active, next) { 611 if (le->eh_type != eh_type) 612 continue; 613 if (le->source_port != th->th_sport || 614 le->dest_port != th->th_dport) 615 continue; 616 switch (eh_type) { 617 #ifdef INET6 618 case ETHERTYPE_IPV6: 619 if (bcmp(&le->source_ip6, &ip6->ip6_src, 620 sizeof(struct in6_addr)) != 0 || 621 bcmp(&le->dest_ip6, &ip6->ip6_dst, 622 sizeof(struct in6_addr)) != 0) 623 continue; 624 break; 625 #endif 626 #ifdef INET 627 case ETHERTYPE_IP: 628 if (le->source_ip4 != ip4->ip_src.s_addr || 629 le->dest_ip4 != ip4->ip_dst.s_addr) 630 continue; 631 break; 632 #endif 633 } 634 635 /* Flush now if appending will result in overflow. */ 636 if (le->p_len > (lc->lro_length_lim - tcp_data_len)) { 637 tcp_lro_active_remove(le); 638 tcp_lro_flush(lc, le); 639 break; 640 } 641 642 /* Try to append the new segment. */ 643 if (__predict_false(seq != le->next_seq || 644 (tcp_data_len == 0 && le->ack_seq == th->th_ack))) { 645 /* Out of order packet or duplicate ACK. */ 646 tcp_lro_active_remove(le); 647 tcp_lro_flush(lc, le); 648 return (TCP_LRO_CANNOT); 649 } 650 651 if (l != 0) { 652 uint32_t tsval = ntohl(*(ts_ptr + 1)); 653 /* Make sure timestamp values are increasing. */ 654 /* XXX-BZ flip and use TSTMP_GEQ macro for this? */ 655 if (__predict_false(le->tsval > tsval || 656 *(ts_ptr + 2) == 0)) 657 return (TCP_LRO_CANNOT); 658 le->tsval = tsval; 659 le->tsecr = *(ts_ptr + 2); 660 } 661 662 le->next_seq += tcp_data_len; 663 le->ack_seq = th->th_ack; 664 le->window = th->th_win; 665 le->append_cnt++; 666 667 #ifdef TCP_LRO_UPDATE_CSUM 668 le->ulp_csum += tcp_lro_rx_csum_fixup(le, l3hdr, th, 669 tcp_data_len, ~csum); 670 #endif 671 672 if (tcp_data_len == 0) { 673 m_freem(m); 674 /* 675 * Flush this LRO entry, if this ACK should not 676 * be further delayed. 677 */ 678 if (le->append_cnt >= lc->lro_ackcnt_lim) { 679 tcp_lro_active_remove(le); 680 tcp_lro_flush(lc, le); 681 } 682 return (0); 683 } 684 685 le->p_len += tcp_data_len; 686 687 /* 688 * Adjust the mbuf so that m_data points to the first byte of 689 * the ULP payload. Adjust the mbuf to avoid complications and 690 * append new segment to existing mbuf chain. 691 */ 692 m_adj(m, m->m_pkthdr.len - tcp_data_len); 693 m_demote_pkthdr(m); 694 695 le->m_tail->m_next = m; 696 le->m_tail = m_last(m); 697 698 /* 699 * If a possible next full length packet would cause an 700 * overflow, pro-actively flush now. 701 */ 702 if (le->p_len > (lc->lro_length_lim - lc->ifp->if_mtu)) { 703 tcp_lro_active_remove(le); 704 tcp_lro_flush(lc, le); 705 } else 706 getmicrotime(&le->mtime); 707 708 return (0); 709 } 710 711 /* Try to find an empty slot. */ 712 if (LIST_EMPTY(&lc->lro_free)) 713 return (TCP_LRO_NO_ENTRIES); 714 715 /* Start a new segment chain. */ 716 le = LIST_FIRST(&lc->lro_free); 717 LIST_REMOVE(le, next); 718 tcp_lro_active_insert(lc, le); 719 getmicrotime(&le->mtime); 720 721 /* Start filling in details. */ 722 switch (eh_type) { 723 #ifdef INET6 724 case ETHERTYPE_IPV6: 725 le->le_ip6 = ip6; 726 le->source_ip6 = ip6->ip6_src; 727 le->dest_ip6 = ip6->ip6_dst; 728 le->eh_type = eh_type; 729 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6); 730 break; 731 #endif 732 #ifdef INET 733 case ETHERTYPE_IP: 734 le->le_ip4 = ip4; 735 le->source_ip4 = ip4->ip_src.s_addr; 736 le->dest_ip4 = ip4->ip_dst.s_addr; 737 le->eh_type = eh_type; 738 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN; 739 break; 740 #endif 741 } 742 le->source_port = th->th_sport; 743 le->dest_port = th->th_dport; 744 745 le->next_seq = seq + tcp_data_len; 746 le->ack_seq = th->th_ack; 747 le->window = th->th_win; 748 if (l != 0) { 749 le->timestamp = 1; 750 le->tsval = ntohl(*(ts_ptr + 1)); 751 le->tsecr = *(ts_ptr + 2); 752 } 753 754 #ifdef TCP_LRO_UPDATE_CSUM 755 /* 756 * Do not touch the csum of the first packet. However save the 757 * "adjusted" checksum of just the source and destination addresses, 758 * the next header and the TCP payload. The length and TCP header 759 * parts may change, so we remove those from the saved checksum and 760 * re-add with final values on tcp_lro_flush() if needed. 761 */ 762 KASSERT(le->ulp_csum == 0, ("%s: le=%p le->ulp_csum=0x%04x\n", 763 __func__, le, le->ulp_csum)); 764 765 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len, 766 ~csum); 767 th->th_sum = csum; /* Restore checksum on first packet. */ 768 #endif 769 770 le->m_head = m; 771 le->m_tail = m_last(m); 772 773 return (0); 774 } 775 776 void 777 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 778 { 779 /* sanity checks */ 780 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 781 lc->lro_mbuf_max == 0)) { 782 /* packet drop */ 783 m_freem(mb); 784 return; 785 } 786 787 /* check if packet is not LRO capable */ 788 if (__predict_false(mb->m_pkthdr.csum_flags == 0 || 789 (lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 790 lc->lro_flushed++; 791 lc->lro_queued++; 792 793 /* input packet to network layer */ 794 (*lc->ifp->if_input) (lc->ifp, mb); 795 return; 796 } 797 798 /* check if array is full */ 799 if (__predict_false(lc->lro_mbuf_count == lc->lro_mbuf_max)) 800 tcp_lro_flush_all(lc); 801 802 /* store sequence number */ 803 TCP_LRO_SEQUENCE(mb) = lc->lro_mbuf_count; 804 805 /* enter mbuf */ 806 lc->lro_mbuf_data[lc->lro_mbuf_count++] = mb; 807 } 808 809 /* end */ 810