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