1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5 * The Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95 33 */ 34 35 /*- 36 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995 37 * 38 * NRL grants permission for redistribution and use in source and binary 39 * forms, with or without modification, of the software and documentation 40 * created at NRL provided that the following conditions are met: 41 * 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgements: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * This product includes software developed at the Information 52 * Technology Division, US Naval Research Laboratory. 53 * 4. Neither the name of the NRL nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 58 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 60 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 61 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 62 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 63 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 64 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 65 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 66 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 67 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 * 69 * The views and conclusions contained in the software and documentation 70 * are those of the authors and should not be interpreted as representing 71 * official policies, either expressed or implied, of the US Naval 72 * Research Laboratory (NRL). 73 */ 74 75 #include <sys/cdefs.h> 76 __FBSDID("$FreeBSD$"); 77 78 #include "opt_inet.h" 79 #include "opt_inet6.h" 80 #include "opt_tcpdebug.h" 81 82 #include <sys/param.h> 83 #include <sys/systm.h> 84 #include <sys/kernel.h> 85 #include <sys/sysctl.h> 86 #include <sys/malloc.h> 87 #include <sys/mbuf.h> 88 #include <sys/proc.h> /* for proc0 declaration */ 89 #include <sys/protosw.h> 90 #include <sys/socket.h> 91 #include <sys/socketvar.h> 92 #include <sys/syslog.h> 93 #include <sys/systm.h> 94 95 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 96 97 #include <vm/uma.h> 98 99 #include <net/if.h> 100 #include <net/if_var.h> 101 #include <net/route.h> 102 #include <net/vnet.h> 103 104 #include <netinet/in.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/ip.h> 107 #include <netinet/in_var.h> 108 #include <netinet/in_pcb.h> 109 #include <netinet/ip_var.h> 110 #include <netinet/ip6.h> 111 #include <netinet/icmp6.h> 112 #include <netinet6/nd6.h> 113 #include <netinet6/ip6_var.h> 114 #include <netinet6/in6_pcb.h> 115 #include <netinet/tcp.h> 116 #include <netinet/tcp_fsm.h> 117 #include <netinet/tcp_seq.h> 118 #include <netinet/tcp_timer.h> 119 #include <netinet/tcp_var.h> 120 #include <netinet6/tcp6_var.h> 121 #include <netinet/tcpip.h> 122 #ifdef TCPDEBUG 123 #include <netinet/tcp_debug.h> 124 #endif /* TCPDEBUG */ 125 126 #include <machine/in_cksum.h> 127 128 VNET_DECLARE(struct uma_zone *, sack_hole_zone); 129 #define V_sack_hole_zone VNET(sack_hole_zone) 130 131 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 132 "TCP SACK"); 133 VNET_DEFINE(int, tcp_do_sack) = 1; 134 #define V_tcp_do_sack VNET(tcp_do_sack) 135 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 136 &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support"); 137 138 VNET_DEFINE(int, tcp_sack_maxholes) = 128; 139 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW, 140 &VNET_NAME(tcp_sack_maxholes), 0, 141 "Maximum number of TCP SACK holes allowed per connection"); 142 143 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536; 144 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW, 145 &VNET_NAME(tcp_sack_globalmaxholes), 0, 146 "Global maximum number of TCP SACK holes"); 147 148 VNET_DEFINE(int, tcp_sack_globalholes) = 0; 149 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD, 150 &VNET_NAME(tcp_sack_globalholes), 0, 151 "Global number of TCP SACK holes currently allocated"); 152 153 154 /* 155 * This function will find overlaps with the currently stored sackblocks 156 * and add any overlap as a dsack block upfront 157 */ 158 void 159 tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 160 { 161 struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS]; 162 int i, j, n, identical; 163 tcp_seq start, end; 164 165 INP_WLOCK_ASSERT(tp->t_inpcb); 166 167 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end")); 168 169 if (SEQ_LT(rcv_end, tp->rcv_nxt) || 170 ((rcv_end == tp->rcv_nxt) && 171 (tp->rcv_numsacks > 0 ) && 172 (tp->sackblks[0].end == tp->rcv_nxt))) { 173 saved_blks[0].start = rcv_start; 174 saved_blks[0].end = rcv_end; 175 } else { 176 saved_blks[0].start = saved_blks[0].end = 0; 177 } 178 179 head_blk.start = head_blk.end = 0; 180 mid_blk.start = rcv_start; 181 mid_blk.end = rcv_end; 182 identical = 0; 183 184 for (i = 0; i < tp->rcv_numsacks; i++) { 185 start = tp->sackblks[i].start; 186 end = tp->sackblks[i].end; 187 if (SEQ_LT(rcv_end, start)) { 188 /* pkt left to sack blk */ 189 continue; 190 } 191 if (SEQ_GT(rcv_start, end)) { 192 /* pkt right to sack blk */ 193 continue; 194 } 195 if (SEQ_GT(tp->rcv_nxt, end)) { 196 if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) && 197 (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) || 198 (head_blk.start == head_blk.end))) { 199 head_blk.start = SEQ_MAX(rcv_start, start); 200 head_blk.end = SEQ_MIN(rcv_end, end); 201 } 202 continue; 203 } 204 if (((head_blk.start == head_blk.end) || 205 SEQ_LT(start, head_blk.start)) && 206 (SEQ_GT(end, rcv_start) && 207 SEQ_LEQ(start, rcv_end))) { 208 head_blk.start = start; 209 head_blk.end = end; 210 } 211 mid_blk.start = SEQ_MIN(mid_blk.start, start); 212 mid_blk.end = SEQ_MAX(mid_blk.end, end); 213 if ((mid_blk.start == start) && 214 (mid_blk.end == end)) 215 identical = 1; 216 } 217 if (SEQ_LT(head_blk.start, head_blk.end)) { 218 /* store overlapping range */ 219 saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start); 220 saved_blks[0].end = SEQ_MIN(rcv_end, head_blk.end); 221 } 222 n = 1; 223 /* 224 * Second, if not ACKed, store the SACK block that 225 * overlaps with the DSACK block unless it is identical 226 */ 227 if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) && 228 !((mid_blk.start == saved_blks[0].start) && 229 (mid_blk.end == saved_blks[0].end))) || 230 identical == 1) { 231 saved_blks[n].start = mid_blk.start; 232 saved_blks[n++].end = mid_blk.end; 233 } 234 for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) { 235 if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) || 236 SEQ_GT(tp->sackblks[j].start, mid_blk.end)) && 237 (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt)))) 238 saved_blks[n++] = tp->sackblks[j]; 239 } 240 j = 0; 241 for (i = 0; i < n; i++) { 242 /* we can end up with a stale initial entry */ 243 if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) { 244 tp->sackblks[j++] = saved_blks[i]; 245 } 246 } 247 tp->rcv_numsacks = j; 248 } 249 250 /* 251 * This function is called upon receipt of new valid data (while not in 252 * header prediction mode), and it updates the ordered list of sacks. 253 */ 254 void 255 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 256 { 257 /* 258 * First reported block MUST be the most recent one. Subsequent 259 * blocks SHOULD be in the order in which they arrived at the 260 * receiver. These two conditions make the implementation fully 261 * compliant with RFC 2018. 262 */ 263 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; 264 int num_head, num_saved, i; 265 266 INP_WLOCK_ASSERT(tp->t_inpcb); 267 268 /* Check arguments. */ 269 KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end")); 270 271 if ((rcv_start == rcv_end) && 272 (tp->rcv_numsacks >= 1) && 273 (rcv_end == tp->sackblks[0].end)) { 274 /* retaining DSACK block below rcv_nxt (todrop) */ 275 head_blk = tp->sackblks[0]; 276 } else { 277 /* SACK block for the received segment. */ 278 head_blk.start = rcv_start; 279 head_blk.end = rcv_end; 280 } 281 282 /* 283 * Merge updated SACK blocks into head_blk, and save unchanged SACK 284 * blocks into saved_blks[]. num_saved will have the number of the 285 * saved SACK blocks. 286 */ 287 num_saved = 0; 288 for (i = 0; i < tp->rcv_numsacks; i++) { 289 tcp_seq start = tp->sackblks[i].start; 290 tcp_seq end = tp->sackblks[i].end; 291 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 292 /* 293 * Discard this SACK block. 294 */ 295 } else if (SEQ_LEQ(head_blk.start, end) && 296 SEQ_GEQ(head_blk.end, start)) { 297 /* 298 * Merge this SACK block into head_blk. This SACK 299 * block itself will be discarded. 300 */ 301 /* 302 * |-| 303 * |---| merge 304 * 305 * |-| 306 * |---| merge 307 * 308 * |-----| 309 * |-| DSACK smaller 310 * 311 * |-| 312 * |-----| DSACK smaller 313 */ 314 if (head_blk.start == end) 315 head_blk.start = start; 316 else if (head_blk.end == start) 317 head_blk.end = end; 318 else { 319 if (SEQ_LT(head_blk.start, start)) { 320 tcp_seq temp = start; 321 start = head_blk.start; 322 head_blk.start = temp; 323 } 324 if (SEQ_GT(head_blk.end, end)) { 325 tcp_seq temp = end; 326 end = head_blk.end; 327 head_blk.end = temp; 328 } 329 if ((head_blk.start != start) || 330 (head_blk.end != end)) { 331 if ((num_saved >= 1) && 332 SEQ_GEQ(saved_blks[num_saved-1].start, start) && 333 SEQ_LEQ(saved_blks[num_saved-1].end, end)) 334 num_saved--; 335 saved_blks[num_saved].start = start; 336 saved_blks[num_saved].end = end; 337 num_saved++; 338 } 339 } 340 } else { 341 /* 342 * This block supercedes the prior block 343 */ 344 if ((num_saved >= 1) && 345 SEQ_GEQ(saved_blks[num_saved-1].start, start) && 346 SEQ_LEQ(saved_blks[num_saved-1].end, end)) 347 num_saved--; 348 /* 349 * Save this SACK block. 350 */ 351 saved_blks[num_saved].start = start; 352 saved_blks[num_saved].end = end; 353 num_saved++; 354 } 355 } 356 357 /* 358 * Update SACK list in tp->sackblks[]. 359 */ 360 num_head = 0; 361 if (SEQ_LT(rcv_start, rcv_end)) { 362 /* 363 * The received data segment is an out-of-order segment. Put 364 * head_blk at the top of SACK list. 365 */ 366 tp->sackblks[0] = head_blk; 367 num_head = 1; 368 /* 369 * If the number of saved SACK blocks exceeds its limit, 370 * discard the last SACK block. 371 */ 372 if (num_saved >= MAX_SACK_BLKS) 373 num_saved--; 374 } 375 if ((rcv_start == rcv_end) && 376 (rcv_start == tp->sackblks[0].end)) { 377 num_head = 1; 378 } 379 if (num_saved > 0) { 380 /* 381 * Copy the saved SACK blocks back. 382 */ 383 bcopy(saved_blks, &tp->sackblks[num_head], 384 sizeof(struct sackblk) * num_saved); 385 } 386 387 /* Save the number of SACK blocks. */ 388 tp->rcv_numsacks = num_head + num_saved; 389 } 390 391 void 392 tcp_clean_dsack_blocks(struct tcpcb *tp) 393 { 394 struct sackblk saved_blks[MAX_SACK_BLKS]; 395 int num_saved, i; 396 397 INP_WLOCK_ASSERT(tp->t_inpcb); 398 /* 399 * Clean up any DSACK blocks that 400 * are in our queue of sack blocks. 401 * 402 */ 403 num_saved = 0; 404 for (i = 0; i < tp->rcv_numsacks; i++) { 405 tcp_seq start = tp->sackblks[i].start; 406 tcp_seq end = tp->sackblks[i].end; 407 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 408 /* 409 * Discard this D-SACK block. 410 */ 411 continue; 412 } 413 /* 414 * Save this SACK block. 415 */ 416 saved_blks[num_saved].start = start; 417 saved_blks[num_saved].end = end; 418 num_saved++; 419 } 420 if (num_saved > 0) { 421 /* 422 * Copy the saved SACK blocks back. 423 */ 424 bcopy(saved_blks, &tp->sackblks[0], 425 sizeof(struct sackblk) * num_saved); 426 } 427 tp->rcv_numsacks = num_saved; 428 } 429 430 /* 431 * Delete all receiver-side SACK information. 432 */ 433 void 434 tcp_clean_sackreport(struct tcpcb *tp) 435 { 436 int i; 437 438 INP_WLOCK_ASSERT(tp->t_inpcb); 439 tp->rcv_numsacks = 0; 440 for (i = 0; i < MAX_SACK_BLKS; i++) 441 tp->sackblks[i].start = tp->sackblks[i].end=0; 442 } 443 444 /* 445 * Allocate struct sackhole. 446 */ 447 static struct sackhole * 448 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) 449 { 450 struct sackhole *hole; 451 452 if (tp->snd_numholes >= V_tcp_sack_maxholes || 453 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) { 454 TCPSTAT_INC(tcps_sack_sboverflow); 455 return NULL; 456 } 457 458 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT); 459 if (hole == NULL) 460 return NULL; 461 462 hole->start = start; 463 hole->end = end; 464 hole->rxmit = start; 465 466 tp->snd_numholes++; 467 atomic_add_int(&V_tcp_sack_globalholes, 1); 468 469 return hole; 470 } 471 472 /* 473 * Free struct sackhole. 474 */ 475 static void 476 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) 477 { 478 479 uma_zfree(V_sack_hole_zone, hole); 480 481 tp->snd_numholes--; 482 atomic_subtract_int(&V_tcp_sack_globalholes, 1); 483 484 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0")); 485 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0")); 486 } 487 488 /* 489 * Insert new SACK hole into scoreboard. 490 */ 491 static struct sackhole * 492 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, 493 struct sackhole *after) 494 { 495 struct sackhole *hole; 496 497 /* Allocate a new SACK hole. */ 498 hole = tcp_sackhole_alloc(tp, start, end); 499 if (hole == NULL) 500 return NULL; 501 502 /* Insert the new SACK hole into scoreboard. */ 503 if (after != NULL) 504 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); 505 else 506 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); 507 508 /* Update SACK hint. */ 509 if (tp->sackhint.nexthole == NULL) 510 tp->sackhint.nexthole = hole; 511 512 return hole; 513 } 514 515 /* 516 * Remove SACK hole from scoreboard. 517 */ 518 static void 519 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole) 520 { 521 522 /* Update SACK hint. */ 523 if (tp->sackhint.nexthole == hole) 524 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink); 525 526 /* Remove this SACK hole. */ 527 TAILQ_REMOVE(&tp->snd_holes, hole, scblink); 528 529 /* Free this SACK hole. */ 530 tcp_sackhole_free(tp, hole); 531 } 532 533 /* 534 * Process cumulative ACK and the TCP SACK option to update the scoreboard. 535 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of 536 * the sequence space). 537 * Returns 1 if incoming ACK has previously unknown SACK information, 538 * 0 otherwise. Note: We treat (snd_una, th_ack) as a sack block so any changes 539 * to that (i.e. left edge moving) would also be considered a change in SACK 540 * information which is slightly different than rfc6675. 541 */ 542 int 543 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack) 544 { 545 struct sackhole *cur, *temp; 546 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp; 547 int i, j, num_sack_blks, sack_changed; 548 549 INP_WLOCK_ASSERT(tp->t_inpcb); 550 551 num_sack_blks = 0; 552 sack_changed = 0; 553 /* 554 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist, 555 * treat [SND.UNA, SEG.ACK) as if it is a SACK block. 556 */ 557 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) { 558 sack_blocks[num_sack_blks].start = tp->snd_una; 559 sack_blocks[num_sack_blks++].end = th_ack; 560 } 561 /* 562 * Append received valid SACK blocks to sack_blocks[], but only if we 563 * received new blocks from the other side. 564 */ 565 if (to->to_flags & TOF_SACK) { 566 tp->sackhint.sacked_bytes = 0; /* reset */ 567 for (i = 0; i < to->to_nsacks; i++) { 568 bcopy((to->to_sacks + i * TCPOLEN_SACK), 569 &sack, sizeof(sack)); 570 sack.start = ntohl(sack.start); 571 sack.end = ntohl(sack.end); 572 if (SEQ_GT(sack.end, sack.start) && 573 SEQ_GT(sack.start, tp->snd_una) && 574 SEQ_GT(sack.start, th_ack) && 575 SEQ_LT(sack.start, tp->snd_max) && 576 SEQ_GT(sack.end, tp->snd_una) && 577 SEQ_LEQ(sack.end, tp->snd_max)) { 578 sack_blocks[num_sack_blks++] = sack; 579 tp->sackhint.sacked_bytes += 580 (sack.end-sack.start); 581 } 582 } 583 } 584 /* 585 * Return if SND.UNA is not advanced and no valid SACK block is 586 * received. 587 */ 588 if (num_sack_blks == 0) 589 return (sack_changed); 590 591 /* 592 * Sort the SACK blocks so we can update the scoreboard with just one 593 * pass. The overhead of sorting up to 4+1 elements is less than 594 * making up to 4+1 passes over the scoreboard. 595 */ 596 for (i = 0; i < num_sack_blks; i++) { 597 for (j = i + 1; j < num_sack_blks; j++) { 598 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 599 sack = sack_blocks[i]; 600 sack_blocks[i] = sack_blocks[j]; 601 sack_blocks[j] = sack; 602 } 603 } 604 } 605 if (TAILQ_EMPTY(&tp->snd_holes)) 606 /* 607 * Empty scoreboard. Need to initialize snd_fack (it may be 608 * uninitialized or have a bogus value). Scoreboard holes 609 * (from the sack blocks received) are created later below 610 * (in the logic that adds holes to the tail of the 611 * scoreboard). 612 */ 613 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack); 614 /* 615 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and 616 * SACK holes (snd_holes) are traversed from their tails with just 617 * one pass in order to reduce the number of compares especially when 618 * the bandwidth-delay product is large. 619 * 620 * Note: Typically, in the first RTT of SACK recovery, the highest 621 * three or four SACK blocks with the same ack number are received. 622 * In the second RTT, if retransmitted data segments are not lost, 623 * the highest three or four SACK blocks with ack number advancing 624 * are received. 625 */ 626 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */ 627 tp->sackhint.last_sack_ack = sblkp->end; 628 if (SEQ_LT(tp->snd_fack, sblkp->start)) { 629 /* 630 * The highest SACK block is beyond fack. Append new SACK 631 * hole at the tail. If the second or later highest SACK 632 * blocks are also beyond the current fack, they will be 633 * inserted by way of hole splitting in the while-loop below. 634 */ 635 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); 636 if (temp != NULL) { 637 tp->snd_fack = sblkp->end; 638 /* Go to the previous sack block. */ 639 sblkp--; 640 sack_changed = 1; 641 } else { 642 /* 643 * We failed to add a new hole based on the current 644 * sack block. Skip over all the sack blocks that 645 * fall completely to the right of snd_fack and 646 * proceed to trim the scoreboard based on the 647 * remaining sack blocks. This also trims the 648 * scoreboard for th_ack (which is sack_blocks[0]). 649 */ 650 while (sblkp >= sack_blocks && 651 SEQ_LT(tp->snd_fack, sblkp->start)) 652 sblkp--; 653 if (sblkp >= sack_blocks && 654 SEQ_LT(tp->snd_fack, sblkp->end)) 655 tp->snd_fack = sblkp->end; 656 } 657 } else if (SEQ_LT(tp->snd_fack, sblkp->end)) { 658 /* fack is advanced. */ 659 tp->snd_fack = sblkp->end; 660 sack_changed = 1; 661 } 662 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */ 663 /* 664 * Since the incoming sack blocks are sorted, we can process them 665 * making one sweep of the scoreboard. 666 */ 667 while (sblkp >= sack_blocks && cur != NULL) { 668 if (SEQ_GEQ(sblkp->start, cur->end)) { 669 /* 670 * SACKs data beyond the current hole. Go to the 671 * previous sack block. 672 */ 673 sblkp--; 674 continue; 675 } 676 if (SEQ_LEQ(sblkp->end, cur->start)) { 677 /* 678 * SACKs data before the current hole. Go to the 679 * previous hole. 680 */ 681 cur = TAILQ_PREV(cur, sackhole_head, scblink); 682 continue; 683 } 684 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); 685 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 686 ("sackhint bytes rtx >= 0")); 687 sack_changed = 1; 688 if (SEQ_LEQ(sblkp->start, cur->start)) { 689 /* Data acks at least the beginning of hole. */ 690 if (SEQ_GEQ(sblkp->end, cur->end)) { 691 /* Acks entire hole, so delete hole. */ 692 temp = cur; 693 cur = TAILQ_PREV(cur, sackhole_head, scblink); 694 tcp_sackhole_remove(tp, temp); 695 /* 696 * The sack block may ack all or part of the 697 * next hole too, so continue onto the next 698 * hole. 699 */ 700 continue; 701 } else { 702 /* Move start of hole forward. */ 703 cur->start = sblkp->end; 704 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 705 } 706 } else { 707 /* Data acks at least the end of hole. */ 708 if (SEQ_GEQ(sblkp->end, cur->end)) { 709 /* Move end of hole backward. */ 710 cur->end = sblkp->start; 711 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 712 } else { 713 /* 714 * ACKs some data in middle of a hole; need 715 * to split current hole 716 */ 717 temp = tcp_sackhole_insert(tp, sblkp->end, 718 cur->end, cur); 719 if (temp != NULL) { 720 if (SEQ_GT(cur->rxmit, temp->rxmit)) { 721 temp->rxmit = cur->rxmit; 722 tp->sackhint.sack_bytes_rexmit 723 += (temp->rxmit 724 - temp->start); 725 } 726 cur->end = sblkp->start; 727 cur->rxmit = SEQ_MIN(cur->rxmit, 728 cur->end); 729 } 730 } 731 } 732 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); 733 /* 734 * Testing sblkp->start against cur->start tells us whether 735 * we're done with the sack block or the sack hole. 736 * Accordingly, we advance one or the other. 737 */ 738 if (SEQ_LEQ(sblkp->start, cur->start)) 739 cur = TAILQ_PREV(cur, sackhole_head, scblink); 740 else 741 sblkp--; 742 } 743 return (sack_changed); 744 } 745 746 /* 747 * Free all SACK holes to clear the scoreboard. 748 */ 749 void 750 tcp_free_sackholes(struct tcpcb *tp) 751 { 752 struct sackhole *q; 753 754 INP_WLOCK_ASSERT(tp->t_inpcb); 755 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) 756 tcp_sackhole_remove(tp, q); 757 tp->sackhint.sack_bytes_rexmit = 0; 758 759 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 760 KASSERT(tp->sackhint.nexthole == NULL, 761 ("tp->sackhint.nexthole == NULL")); 762 } 763 764 /* 765 * Partial ack handling within a sack recovery episode. Keeping this very 766 * simple for now. When a partial ack is received, force snd_cwnd to a value 767 * that will allow the sender to transmit no more than 2 segments. If 768 * necessary, a better scheme can be adopted at a later point, but for now, 769 * the goal is to prevent the sender from bursting a large amount of data in 770 * the midst of sack recovery. 771 */ 772 void 773 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) 774 { 775 int num_segs = 1; 776 777 INP_WLOCK_ASSERT(tp->t_inpcb); 778 tcp_timer_activate(tp, TT_REXMT, 0); 779 tp->t_rtttime = 0; 780 /* Send one or 2 segments based on how much new data was acked. */ 781 if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2) 782 num_segs = 2; 783 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 784 (tp->snd_nxt - tp->snd_recover) + num_segs * tp->t_maxseg); 785 if (tp->snd_cwnd > tp->snd_ssthresh) 786 tp->snd_cwnd = tp->snd_ssthresh; 787 tp->t_flags |= TF_ACKNOW; 788 (void) tp->t_fb->tfb_tcp_output(tp); 789 } 790 791 #if 0 792 /* 793 * Debug version of tcp_sack_output() that walks the scoreboard. Used for 794 * now to sanity check the hint. 795 */ 796 static struct sackhole * 797 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 798 { 799 struct sackhole *p; 800 801 INP_WLOCK_ASSERT(tp->t_inpcb); 802 *sack_bytes_rexmt = 0; 803 TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 804 if (SEQ_LT(p->rxmit, p->end)) { 805 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 806 continue; 807 } 808 *sack_bytes_rexmt += (p->rxmit - p->start); 809 break; 810 } 811 *sack_bytes_rexmt += (p->rxmit - p->start); 812 } 813 return (p); 814 } 815 #endif 816 817 /* 818 * Returns the next hole to retransmit and the number of retransmitted bytes 819 * from the scoreboard. We store both the next hole and the number of 820 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 821 * reception). This avoids scoreboard traversals completely. 822 * 823 * The loop here will traverse *at most* one link. Here's the argument. For 824 * the loop to traverse more than 1 link before finding the next hole to 825 * retransmit, we would need to have at least 1 node following the current 826 * hint with (rxmit == end). But, for all holes following the current hint, 827 * (start == rxmit), since we have not yet retransmitted from them. 828 * Therefore, in order to traverse more 1 link in the loop below, we need to 829 * have at least one node following the current hint with (start == rxmit == 830 * end). But that can't happen, (start == end) means that all the data in 831 * that hole has been sacked, in which case, the hole would have been removed 832 * from the scoreboard. 833 */ 834 struct sackhole * 835 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 836 { 837 struct sackhole *hole = NULL; 838 839 INP_WLOCK_ASSERT(tp->t_inpcb); 840 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 841 hole = tp->sackhint.nexthole; 842 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 843 goto out; 844 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 845 if (SEQ_LT(hole->rxmit, hole->end)) { 846 tp->sackhint.nexthole = hole; 847 break; 848 } 849 } 850 out: 851 return (hole); 852 } 853 854 /* 855 * After a timeout, the SACK list may be rebuilt. This SACK information 856 * should be used to avoid retransmitting SACKed data. This function 857 * traverses the SACK list to see if snd_nxt should be moved forward. 858 */ 859 void 860 tcp_sack_adjust(struct tcpcb *tp) 861 { 862 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 863 864 INP_WLOCK_ASSERT(tp->t_inpcb); 865 if (cur == NULL) 866 return; /* No holes */ 867 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) 868 return; /* We're already beyond any SACKed blocks */ 869 /*- 870 * Two cases for which we want to advance snd_nxt: 871 * i) snd_nxt lies between end of one hole and beginning of another 872 * ii) snd_nxt lies between end of last hole and snd_fack 873 */ 874 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 875 if (SEQ_LT(tp->snd_nxt, cur->end)) 876 return; 877 if (SEQ_GEQ(tp->snd_nxt, p->start)) 878 cur = p; 879 else { 880 tp->snd_nxt = p->start; 881 return; 882 } 883 } 884 if (SEQ_LT(tp->snd_nxt, cur->end)) 885 return; 886 tp->snd_nxt = tp->snd_fack; 887 } 888