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, 0, "TCP SACK"); 132 VNET_DEFINE(int, tcp_do_sack) = 1; 133 #define V_tcp_do_sack VNET(tcp_do_sack) 134 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 135 &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support"); 136 137 VNET_DEFINE(int, tcp_sack_maxholes) = 128; 138 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW, 139 &VNET_NAME(tcp_sack_maxholes), 0, 140 "Maximum number of TCP SACK holes allowed per connection"); 141 142 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536; 143 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW, 144 &VNET_NAME(tcp_sack_globalmaxholes), 0, 145 "Global maximum number of TCP SACK holes"); 146 147 VNET_DEFINE(int, tcp_sack_globalholes) = 0; 148 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD, 149 &VNET_NAME(tcp_sack_globalholes), 0, 150 "Global number of TCP SACK holes currently allocated"); 151 152 153 /* 154 * This function will find overlaps with the currently stored sackblocks 155 * and add any overlap as a dsack block upfront 156 */ 157 void 158 tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 159 { 160 struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS]; 161 int i, j, n, identical; 162 tcp_seq start, end; 163 164 INP_WLOCK_ASSERT(tp->t_inpcb); 165 166 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end")); 167 168 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) { 169 log(LOG_DEBUG, "\nDSACK update: %d..%d, rcv_nxt: %u\n", 170 rcv_start, rcv_end, tp->rcv_nxt); 171 } 172 173 if (SEQ_LT(rcv_end, tp->rcv_nxt) || 174 ((rcv_end == tp->rcv_nxt) && 175 (tp->rcv_numsacks > 0 ) && 176 (tp->sackblks[0].end == tp->rcv_nxt))) { 177 saved_blks[0].start = rcv_start; 178 saved_blks[0].end = rcv_end; 179 } else { 180 saved_blks[0].start = saved_blks[0].end = 0; 181 } 182 183 head_blk.start = head_blk.end = 0; 184 mid_blk.start = rcv_start; 185 mid_blk.end = rcv_end; 186 identical = 0; 187 188 for (i = 0; i < tp->rcv_numsacks; i++) { 189 start = tp->sackblks[i].start; 190 end = tp->sackblks[i].end; 191 if (SEQ_LT(rcv_end, start)) { 192 /* pkt left to sack blk */ 193 continue; 194 } 195 if (SEQ_GT(rcv_start, end)) { 196 /* pkt right to sack blk */ 197 continue; 198 } 199 if (SEQ_GT(tp->rcv_nxt, end)) { 200 if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) && 201 (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) || 202 (head_blk.start == head_blk.end))) { 203 head_blk.start = SEQ_MAX(rcv_start, start); 204 head_blk.end = SEQ_MIN(rcv_end, end); 205 } 206 continue; 207 } 208 if (((head_blk.start == head_blk.end) || 209 SEQ_LT(start, head_blk.start)) && 210 (SEQ_GT(end, rcv_start) && 211 SEQ_LEQ(start, rcv_end))) { 212 head_blk.start = start; 213 head_blk.end = end; 214 } 215 mid_blk.start = SEQ_MIN(mid_blk.start, start); 216 mid_blk.end = SEQ_MAX(mid_blk.end, end); 217 if ((mid_blk.start == start) && 218 (mid_blk.end == end)) 219 identical = 1; 220 } 221 if (SEQ_LT(head_blk.start, head_blk.end)) { 222 /* store overlapping range */ 223 saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start); 224 saved_blks[0].end = SEQ_MIN(rcv_end, head_blk.end); 225 } 226 n = 1; 227 /* 228 * Second, if not ACKed, store the SACK block that 229 * overlaps with the DSACK block unless it is identical 230 */ 231 if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) && 232 !((mid_blk.start == saved_blks[0].start) && 233 (mid_blk.end == saved_blks[0].end))) || 234 identical == 1) { 235 saved_blks[n].start = mid_blk.start; 236 saved_blks[n++].end = mid_blk.end; 237 } 238 for (j = 0; (j < tp->rcv_numsacks) && (j < MAX_SACK_BLKS-1); j++) { 239 if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) || 240 SEQ_GT(tp->sackblks[j].start, mid_blk.end)) && 241 (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt)))) 242 saved_blks[n++] = tp->sackblks[j]; 243 } 244 j = 0; 245 for (i = 0; i < n; i++) { 246 /* we can end up with a stale inital entry */ 247 if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) { 248 tp->sackblks[j++] = saved_blks[i]; 249 } 250 } 251 tp->rcv_numsacks = j; 252 } 253 254 /* 255 * This function is called upon receipt of new valid data (while not in 256 * header prediction mode), and it updates the ordered list of sacks. 257 */ 258 void 259 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 260 { 261 /* 262 * First reported block MUST be the most recent one. Subsequent 263 * blocks SHOULD be in the order in which they arrived at the 264 * receiver. These two conditions make the implementation fully 265 * compliant with RFC 2018. 266 */ 267 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; 268 int num_head, num_saved, i; 269 270 INP_WLOCK_ASSERT(tp->t_inpcb); 271 272 /* Check arguments. */ 273 KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end")); 274 275 if ((rcv_start == rcv_end) && 276 (tp->rcv_numsacks >= 1) && 277 (rcv_end == tp->sackblks[0].end)) { 278 /* retaining DSACK block below rcv_nxt (todrop) */ 279 head_blk = tp->sackblks[0]; 280 } else { 281 /* SACK block for the received segment. */ 282 head_blk.start = rcv_start; 283 head_blk.end = rcv_end; 284 } 285 286 /* 287 * Merge updated SACK blocks into head_blk, and save unchanged SACK 288 * blocks into saved_blks[]. num_saved will have the number of the 289 * saved SACK blocks. 290 */ 291 num_saved = 0; 292 for (i = 0; i < tp->rcv_numsacks; i++) { 293 tcp_seq start = tp->sackblks[i].start; 294 tcp_seq end = tp->sackblks[i].end; 295 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 296 /* 297 * Discard this SACK block. 298 */ 299 } else if (SEQ_LEQ(head_blk.start, end) && 300 SEQ_GEQ(head_blk.end, start)) { 301 /* 302 * Merge this SACK block into head_blk. This SACK 303 * block itself will be discarded. 304 */ 305 /* 306 * |-| 307 * |---| merge 308 * 309 * |-| 310 * |---| merge 311 * 312 * |-----| 313 * |-| DSACK smaller 314 * 315 * |-| 316 * |-----| DSACK smaller 317 */ 318 if (head_blk.start == end) 319 head_blk.start = start; 320 else if (head_blk.end == start) 321 head_blk.end = end; 322 else { 323 if (SEQ_LT(head_blk.start, start)) { 324 tcp_seq temp = start; 325 start = head_blk.start; 326 head_blk.start = temp; 327 } 328 if (SEQ_GT(head_blk.end, end)) { 329 tcp_seq temp = end; 330 end = head_blk.end; 331 head_blk.end = temp; 332 } 333 if ((head_blk.start != start) || 334 (head_blk.end != end)) { 335 if ((num_saved >= 1) && 336 SEQ_GEQ(saved_blks[num_saved-1].start, start) && 337 SEQ_LEQ(saved_blks[num_saved-1].end, end)) 338 num_saved--; 339 saved_blks[num_saved].start = start; 340 saved_blks[num_saved].end = end; 341 num_saved++; 342 } 343 } 344 } else { 345 /* 346 * This block supercedes the prior block 347 */ 348 if ((num_saved >= 1) && 349 SEQ_GEQ(saved_blks[num_saved-1].start, start) && 350 SEQ_LEQ(saved_blks[num_saved-1].end, end)) 351 num_saved--; 352 /* 353 * Save this SACK block. 354 */ 355 saved_blks[num_saved].start = start; 356 saved_blks[num_saved].end = end; 357 num_saved++; 358 } 359 } 360 361 /* 362 * Update SACK list in tp->sackblks[]. 363 */ 364 num_head = 0; 365 if (SEQ_LT(rcv_start, rcv_end)) { 366 /* 367 * The received data segment is an out-of-order segment. Put 368 * head_blk at the top of SACK list. 369 */ 370 tp->sackblks[0] = head_blk; 371 num_head = 1; 372 /* 373 * If the number of saved SACK blocks exceeds its limit, 374 * discard the last SACK block. 375 */ 376 if (num_saved >= MAX_SACK_BLKS) 377 num_saved--; 378 } 379 if ((rcv_start == rcv_end) && 380 (rcv_start == tp->sackblks[0].end)) { 381 num_head = 1; 382 } 383 if (num_saved > 0) { 384 /* 385 * Copy the saved SACK blocks back. 386 */ 387 bcopy(saved_blks, &tp->sackblks[num_head], 388 sizeof(struct sackblk) * num_saved); 389 } 390 391 /* Save the number of SACK blocks. */ 392 tp->rcv_numsacks = num_head + num_saved; 393 } 394 395 void 396 tcp_clean_dsack_blocks(struct tcpcb *tp) 397 { 398 struct sackblk saved_blks[MAX_SACK_BLKS]; 399 int num_saved, i; 400 401 INP_WLOCK_ASSERT(tp->t_inpcb); 402 /* 403 * Clean up any DSACK blocks that 404 * are in our queue of sack blocks. 405 * 406 */ 407 num_saved = 0; 408 for (i = 0; i < tp->rcv_numsacks; i++) { 409 tcp_seq start = tp->sackblks[i].start; 410 tcp_seq end = tp->sackblks[i].end; 411 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 412 /* 413 * Discard this D-SACK block. 414 */ 415 continue; 416 } 417 /* 418 * Save this SACK block. 419 */ 420 saved_blks[num_saved].start = start; 421 saved_blks[num_saved].end = end; 422 num_saved++; 423 } 424 if (num_saved > 0) { 425 /* 426 * Copy the saved SACK blocks back. 427 */ 428 bcopy(saved_blks, &tp->sackblks[0], 429 sizeof(struct sackblk) * num_saved); 430 } 431 tp->rcv_numsacks = num_saved; 432 } 433 434 /* 435 * Delete all receiver-side SACK information. 436 */ 437 void 438 tcp_clean_sackreport(struct tcpcb *tp) 439 { 440 int i; 441 442 INP_WLOCK_ASSERT(tp->t_inpcb); 443 tp->rcv_numsacks = 0; 444 for (i = 0; i < MAX_SACK_BLKS; i++) 445 tp->sackblks[i].start = tp->sackblks[i].end=0; 446 } 447 448 /* 449 * Allocate struct sackhole. 450 */ 451 static struct sackhole * 452 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) 453 { 454 struct sackhole *hole; 455 456 if (tp->snd_numholes >= V_tcp_sack_maxholes || 457 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) { 458 TCPSTAT_INC(tcps_sack_sboverflow); 459 return NULL; 460 } 461 462 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT); 463 if (hole == NULL) 464 return NULL; 465 466 hole->start = start; 467 hole->end = end; 468 hole->rxmit = start; 469 470 tp->snd_numholes++; 471 atomic_add_int(&V_tcp_sack_globalholes, 1); 472 473 return hole; 474 } 475 476 /* 477 * Free struct sackhole. 478 */ 479 static void 480 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) 481 { 482 483 uma_zfree(V_sack_hole_zone, hole); 484 485 tp->snd_numholes--; 486 atomic_subtract_int(&V_tcp_sack_globalholes, 1); 487 488 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0")); 489 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0")); 490 } 491 492 /* 493 * Insert new SACK hole into scoreboard. 494 */ 495 static struct sackhole * 496 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, 497 struct sackhole *after) 498 { 499 struct sackhole *hole; 500 501 /* Allocate a new SACK hole. */ 502 hole = tcp_sackhole_alloc(tp, start, end); 503 if (hole == NULL) 504 return NULL; 505 506 /* Insert the new SACK hole into scoreboard. */ 507 if (after != NULL) 508 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); 509 else 510 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); 511 512 /* Update SACK hint. */ 513 if (tp->sackhint.nexthole == NULL) 514 tp->sackhint.nexthole = hole; 515 516 return hole; 517 } 518 519 /* 520 * Remove SACK hole from scoreboard. 521 */ 522 static void 523 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole) 524 { 525 526 /* Update SACK hint. */ 527 if (tp->sackhint.nexthole == hole) 528 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink); 529 530 /* Remove this SACK hole. */ 531 TAILQ_REMOVE(&tp->snd_holes, hole, scblink); 532 533 /* Free this SACK hole. */ 534 tcp_sackhole_free(tp, hole); 535 } 536 537 /* 538 * Process cumulative ACK and the TCP SACK option to update the scoreboard. 539 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of 540 * the sequence space). 541 * Returns 1 if incoming ACK has previously unknown SACK information, 542 * 0 otherwise. Note: We treat (snd_una, th_ack) as a sack block so any changes 543 * to that (i.e. left edge moving) would also be considered a change in SACK 544 * information which is slightly different than rfc6675. 545 */ 546 int 547 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack) 548 { 549 struct sackhole *cur, *temp; 550 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp; 551 int i, j, num_sack_blks, sack_changed; 552 553 INP_WLOCK_ASSERT(tp->t_inpcb); 554 555 num_sack_blks = 0; 556 sack_changed = 0; 557 /* 558 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist, 559 * treat [SND.UNA, SEG.ACK) as if it is a SACK block. 560 */ 561 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) { 562 sack_blocks[num_sack_blks].start = tp->snd_una; 563 sack_blocks[num_sack_blks++].end = th_ack; 564 } 565 /* 566 * Append received valid SACK blocks to sack_blocks[], but only if we 567 * received new blocks from the other side. 568 */ 569 if (to->to_flags & TOF_SACK) { 570 tp->sackhint.sacked_bytes = 0; /* reset */ 571 for (i = 0; i < to->to_nsacks; i++) { 572 bcopy((to->to_sacks + i * TCPOLEN_SACK), 573 &sack, sizeof(sack)); 574 sack.start = ntohl(sack.start); 575 sack.end = ntohl(sack.end); 576 if (SEQ_GT(sack.end, sack.start) && 577 SEQ_GT(sack.start, tp->snd_una) && 578 SEQ_GT(sack.start, th_ack) && 579 SEQ_LT(sack.start, tp->snd_max) && 580 SEQ_GT(sack.end, tp->snd_una) && 581 SEQ_LEQ(sack.end, tp->snd_max)) { 582 sack_blocks[num_sack_blks++] = sack; 583 tp->sackhint.sacked_bytes += 584 (sack.end-sack.start); 585 } 586 } 587 } 588 /* 589 * Return if SND.UNA is not advanced and no valid SACK block is 590 * received. 591 */ 592 if (num_sack_blks == 0) 593 return (sack_changed); 594 595 /* 596 * Sort the SACK blocks so we can update the scoreboard with just one 597 * pass. The overhead of sorting up to 4+1 elements is less than 598 * making up to 4+1 passes over the scoreboard. 599 */ 600 for (i = 0; i < num_sack_blks; i++) { 601 for (j = i + 1; j < num_sack_blks; j++) { 602 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 603 sack = sack_blocks[i]; 604 sack_blocks[i] = sack_blocks[j]; 605 sack_blocks[j] = sack; 606 } 607 } 608 } 609 if (TAILQ_EMPTY(&tp->snd_holes)) 610 /* 611 * Empty scoreboard. Need to initialize snd_fack (it may be 612 * uninitialized or have a bogus value). Scoreboard holes 613 * (from the sack blocks received) are created later below 614 * (in the logic that adds holes to the tail of the 615 * scoreboard). 616 */ 617 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack); 618 /* 619 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and 620 * SACK holes (snd_holes) are traversed from their tails with just 621 * one pass in order to reduce the number of compares especially when 622 * the bandwidth-delay product is large. 623 * 624 * Note: Typically, in the first RTT of SACK recovery, the highest 625 * three or four SACK blocks with the same ack number are received. 626 * In the second RTT, if retransmitted data segments are not lost, 627 * the highest three or four SACK blocks with ack number advancing 628 * are received. 629 */ 630 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */ 631 tp->sackhint.last_sack_ack = sblkp->end; 632 if (SEQ_LT(tp->snd_fack, sblkp->start)) { 633 /* 634 * The highest SACK block is beyond fack. Append new SACK 635 * hole at the tail. If the second or later highest SACK 636 * blocks are also beyond the current fack, they will be 637 * inserted by way of hole splitting in the while-loop below. 638 */ 639 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); 640 if (temp != NULL) { 641 tp->snd_fack = sblkp->end; 642 /* Go to the previous sack block. */ 643 sblkp--; 644 sack_changed = 1; 645 } else { 646 /* 647 * We failed to add a new hole based on the current 648 * sack block. Skip over all the sack blocks that 649 * fall completely to the right of snd_fack and 650 * proceed to trim the scoreboard based on the 651 * remaining sack blocks. This also trims the 652 * scoreboard for th_ack (which is sack_blocks[0]). 653 */ 654 while (sblkp >= sack_blocks && 655 SEQ_LT(tp->snd_fack, sblkp->start)) 656 sblkp--; 657 if (sblkp >= sack_blocks && 658 SEQ_LT(tp->snd_fack, sblkp->end)) 659 tp->snd_fack = sblkp->end; 660 } 661 } else if (SEQ_LT(tp->snd_fack, sblkp->end)) { 662 /* fack is advanced. */ 663 tp->snd_fack = sblkp->end; 664 sack_changed = 1; 665 } 666 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */ 667 /* 668 * Since the incoming sack blocks are sorted, we can process them 669 * making one sweep of the scoreboard. 670 */ 671 while (sblkp >= sack_blocks && cur != NULL) { 672 if (SEQ_GEQ(sblkp->start, cur->end)) { 673 /* 674 * SACKs data beyond the current hole. Go to the 675 * previous sack block. 676 */ 677 sblkp--; 678 continue; 679 } 680 if (SEQ_LEQ(sblkp->end, cur->start)) { 681 /* 682 * SACKs data before the current hole. Go to the 683 * previous hole. 684 */ 685 cur = TAILQ_PREV(cur, sackhole_head, scblink); 686 continue; 687 } 688 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); 689 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 690 ("sackhint bytes rtx >= 0")); 691 sack_changed = 1; 692 if (SEQ_LEQ(sblkp->start, cur->start)) { 693 /* Data acks at least the beginning of hole. */ 694 if (SEQ_GEQ(sblkp->end, cur->end)) { 695 /* Acks entire hole, so delete hole. */ 696 temp = cur; 697 cur = TAILQ_PREV(cur, sackhole_head, scblink); 698 tcp_sackhole_remove(tp, temp); 699 /* 700 * The sack block may ack all or part of the 701 * next hole too, so continue onto the next 702 * hole. 703 */ 704 continue; 705 } else { 706 /* Move start of hole forward. */ 707 cur->start = sblkp->end; 708 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 709 } 710 } else { 711 /* Data acks at least the end of hole. */ 712 if (SEQ_GEQ(sblkp->end, cur->end)) { 713 /* Move end of hole backward. */ 714 cur->end = sblkp->start; 715 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 716 } else { 717 /* 718 * ACKs some data in middle of a hole; need 719 * to split current hole 720 */ 721 temp = tcp_sackhole_insert(tp, sblkp->end, 722 cur->end, cur); 723 if (temp != NULL) { 724 if (SEQ_GT(cur->rxmit, temp->rxmit)) { 725 temp->rxmit = cur->rxmit; 726 tp->sackhint.sack_bytes_rexmit 727 += (temp->rxmit 728 - temp->start); 729 } 730 cur->end = sblkp->start; 731 cur->rxmit = SEQ_MIN(cur->rxmit, 732 cur->end); 733 } 734 } 735 } 736 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); 737 /* 738 * Testing sblkp->start against cur->start tells us whether 739 * we're done with the sack block or the sack hole. 740 * Accordingly, we advance one or the other. 741 */ 742 if (SEQ_LEQ(sblkp->start, cur->start)) 743 cur = TAILQ_PREV(cur, sackhole_head, scblink); 744 else 745 sblkp--; 746 } 747 return (sack_changed); 748 } 749 750 /* 751 * Free all SACK holes to clear the scoreboard. 752 */ 753 void 754 tcp_free_sackholes(struct tcpcb *tp) 755 { 756 struct sackhole *q; 757 758 INP_WLOCK_ASSERT(tp->t_inpcb); 759 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) 760 tcp_sackhole_remove(tp, q); 761 tp->sackhint.sack_bytes_rexmit = 0; 762 763 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 764 KASSERT(tp->sackhint.nexthole == NULL, 765 ("tp->sackhint.nexthole == NULL")); 766 } 767 768 /* 769 * Partial ack handling within a sack recovery episode. Keeping this very 770 * simple for now. When a partial ack is received, force snd_cwnd to a value 771 * that will allow the sender to transmit no more than 2 segments. If 772 * necessary, a better scheme can be adopted at a later point, but for now, 773 * the goal is to prevent the sender from bursting a large amount of data in 774 * the midst of sack recovery. 775 */ 776 void 777 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) 778 { 779 int num_segs = 1; 780 781 INP_WLOCK_ASSERT(tp->t_inpcb); 782 tcp_timer_activate(tp, TT_REXMT, 0); 783 tp->t_rtttime = 0; 784 /* Send one or 2 segments based on how much new data was acked. */ 785 if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2) 786 num_segs = 2; 787 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 788 (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg); 789 if (tp->snd_cwnd > tp->snd_ssthresh) 790 tp->snd_cwnd = tp->snd_ssthresh; 791 tp->t_flags |= TF_ACKNOW; 792 (void) tp->t_fb->tfb_tcp_output(tp); 793 } 794 795 #if 0 796 /* 797 * Debug version of tcp_sack_output() that walks the scoreboard. Used for 798 * now to sanity check the hint. 799 */ 800 static struct sackhole * 801 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 802 { 803 struct sackhole *p; 804 805 INP_WLOCK_ASSERT(tp->t_inpcb); 806 *sack_bytes_rexmt = 0; 807 TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 808 if (SEQ_LT(p->rxmit, p->end)) { 809 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 810 continue; 811 } 812 *sack_bytes_rexmt += (p->rxmit - p->start); 813 break; 814 } 815 *sack_bytes_rexmt += (p->rxmit - p->start); 816 } 817 return (p); 818 } 819 #endif 820 821 /* 822 * Returns the next hole to retransmit and the number of retransmitted bytes 823 * from the scoreboard. We store both the next hole and the number of 824 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 825 * reception). This avoids scoreboard traversals completely. 826 * 827 * The loop here will traverse *at most* one link. Here's the argument. For 828 * the loop to traverse more than 1 link before finding the next hole to 829 * retransmit, we would need to have at least 1 node following the current 830 * hint with (rxmit == end). But, for all holes following the current hint, 831 * (start == rxmit), since we have not yet retransmitted from them. 832 * Therefore, in order to traverse more 1 link in the loop below, we need to 833 * have at least one node following the current hint with (start == rxmit == 834 * end). But that can't happen, (start == end) means that all the data in 835 * that hole has been sacked, in which case, the hole would have been removed 836 * from the scoreboard. 837 */ 838 struct sackhole * 839 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 840 { 841 struct sackhole *hole = NULL; 842 843 INP_WLOCK_ASSERT(tp->t_inpcb); 844 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 845 hole = tp->sackhint.nexthole; 846 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 847 goto out; 848 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 849 if (SEQ_LT(hole->rxmit, hole->end)) { 850 tp->sackhint.nexthole = hole; 851 break; 852 } 853 } 854 out: 855 return (hole); 856 } 857 858 /* 859 * After a timeout, the SACK list may be rebuilt. This SACK information 860 * should be used to avoid retransmitting SACKed data. This function 861 * traverses the SACK list to see if snd_nxt should be moved forward. 862 */ 863 void 864 tcp_sack_adjust(struct tcpcb *tp) 865 { 866 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 867 868 INP_WLOCK_ASSERT(tp->t_inpcb); 869 if (cur == NULL) 870 return; /* No holes */ 871 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) 872 return; /* We're already beyond any SACKed blocks */ 873 /*- 874 * Two cases for which we want to advance snd_nxt: 875 * i) snd_nxt lies between end of one hole and beginning of another 876 * ii) snd_nxt lies between end of last hole and snd_fack 877 */ 878 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 879 if (SEQ_LT(tp->snd_nxt, cur->end)) 880 return; 881 if (SEQ_GEQ(tp->snd_nxt, p->start)) 882 cur = p; 883 else { 884 tp->snd_nxt = p->start; 885 return; 886 } 887 } 888 if (SEQ_LT(tp->snd_nxt, cur->end)) 889 return; 890 tp->snd_nxt = tp->snd_fack; 891 } 892