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