1 /* $NetBSD: tcp_sack.c,v 1.31 2015/03/31 08:47:01 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Kentaro A. Kurahone. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95 61 * $FreeBSD: src/sys/netinet/tcp_sack.c,v 1.3.2.2 2004/12/25 23:02:57 rwatson Exp $ 62 */ 63 64 /* 65 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995 66 * 67 * NRL grants permission for redistribution and use in source and binary 68 * forms, with or without modification, of the software and documentation 69 * created at NRL provided that the following conditions are met: 70 * 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. All advertising materials mentioning features or use of this software 77 * must display the following acknowledgements: 78 * This product includes software developed by the University of 79 * California, Berkeley and its contributors. 80 * This product includes software developed at the Information 81 * Technology Division, US Naval Research Laboratory. 82 * 4. Neither the name of the NRL nor the names of its contributors 83 * may be used to endorse or promote products derived from this software 84 * without specific prior written permission. 85 * 86 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 87 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 88 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 89 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 90 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 91 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 92 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 93 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 94 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 95 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 96 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 97 * 98 * The views and conclusions contained in the software and documentation 99 * are those of the authors and should not be interpreted as representing 100 * official policies, either expressed or implied, of the US Naval 101 * Research Laboratory (NRL). 102 */ 103 104 #include <sys/cdefs.h> 105 __KERNEL_RCSID(0, "$NetBSD: tcp_sack.c,v 1.31 2015/03/31 08:47:01 ozaki-r Exp $"); 106 107 #include "opt_inet.h" 108 #include "opt_inet_csum.h" 109 #include "opt_tcp_debug.h" 110 #include "opt_ddb.h" 111 112 #include <sys/param.h> 113 #include <sys/systm.h> 114 #include <sys/mbuf.h> 115 #include <sys/protosw.h> 116 #include <sys/socket.h> 117 #include <sys/socketvar.h> 118 #include <sys/errno.h> 119 #include <sys/syslog.h> 120 #include <sys/pool.h> 121 #include <sys/domain.h> 122 #include <sys/kernel.h> 123 124 #include <net/if.h> 125 #include <net/route.h> 126 #include <net/if_types.h> 127 128 #include <netinet/in.h> 129 #include <netinet/in_systm.h> 130 #include <netinet/ip.h> 131 #include <netinet/in_pcb.h> 132 #include <netinet/in_var.h> 133 #include <netinet/ip_var.h> 134 135 #ifdef INET6 136 #ifndef INET 137 #include <netinet/in.h> 138 #endif 139 #include <netinet/ip6.h> 140 #include <netinet6/ip6_var.h> 141 #include <netinet6/in6_pcb.h> 142 #include <netinet6/ip6_var.h> 143 #include <netinet6/in6_var.h> 144 #include <netinet/icmp6.h> 145 #include <netinet6/nd6.h> 146 #endif 147 148 #ifndef INET6 149 /* always need ip6.h for IP6_EXTHDR_GET */ 150 #include <netinet/ip6.h> 151 #endif 152 153 #include <netinet/tcp.h> 154 #include <netinet/tcp_fsm.h> 155 #include <netinet/tcp_seq.h> 156 #include <netinet/tcp_timer.h> 157 #include <netinet/tcp_var.h> 158 #include <netinet/tcpip.h> 159 #include <netinet/tcp_debug.h> 160 161 /* SACK block pool. */ 162 static struct pool sackhole_pool; 163 164 void 165 tcp_sack_init(void) 166 { 167 168 pool_init(&sackhole_pool, sizeof(struct sackhole), 0, 0, 0, 169 "sackholepl", NULL, IPL_SOFTNET); 170 } 171 172 static struct sackhole * 173 sack_allochole(struct tcpcb *tp) 174 { 175 struct sackhole *hole; 176 177 if (tp->snd_numholes >= tcp_sack_tp_maxholes || 178 tcp_sack_globalholes >= tcp_sack_globalmaxholes) { 179 return NULL; 180 } 181 hole = pool_get(&sackhole_pool, PR_NOWAIT); 182 if (hole == NULL) { 183 return NULL; 184 } 185 tp->snd_numholes++; 186 tcp_sack_globalholes++; 187 188 return hole; 189 } 190 191 static struct sackhole * 192 sack_inserthole(struct tcpcb *tp, tcp_seq start, tcp_seq end, 193 struct sackhole *prev) 194 { 195 struct sackhole *hole; 196 197 hole = sack_allochole(tp); 198 if (hole == NULL) { 199 return NULL; 200 } 201 hole->start = hole->rxmit = start; 202 hole->end = end; 203 if (prev != NULL) { 204 TAILQ_INSERT_AFTER(&tp->snd_holes, prev, hole, sackhole_q); 205 } else { 206 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, sackhole_q); 207 } 208 return hole; 209 } 210 211 static struct sackhole * 212 sack_removehole(struct tcpcb *tp, struct sackhole *hole) 213 { 214 struct sackhole *next; 215 216 next = TAILQ_NEXT(hole, sackhole_q); 217 tp->snd_numholes--; 218 tcp_sack_globalholes--; 219 TAILQ_REMOVE(&tp->snd_holes, hole, sackhole_q); 220 pool_put(&sackhole_pool, hole); 221 222 return next; 223 } 224 225 /* 226 * tcp_new_dsack: record the reception of a duplicated segment. 227 */ 228 229 void 230 tcp_new_dsack(struct tcpcb *tp, tcp_seq seq, u_int32_t len) 231 { 232 233 if (TCP_SACK_ENABLED(tp)) { 234 tp->rcv_dsack_block.left = seq; 235 tp->rcv_dsack_block.right = seq + len; 236 tp->rcv_sack_flags |= TCPSACK_HAVED; 237 } 238 } 239 240 /* 241 * tcp_sack_option: parse the given SACK option and update the scoreboard. 242 */ 243 244 void 245 tcp_sack_option(struct tcpcb *tp, const struct tcphdr *th, const u_char *cp, 246 int optlen) 247 { 248 struct sackblk 249 t_sack_block[(MAX_TCPOPTLEN - 2) / (sizeof(u_int32_t) * 2)]; 250 struct sackblk *sack = NULL; 251 struct sackhole *cur = NULL; 252 struct sackhole *tmp = NULL; 253 const char *lp = cp + 2; 254 int i, j, num_sack_blks; 255 tcp_seq left, right, acked; 256 257 /* 258 * If we aren't processing SACK responses, this is not an ACK 259 * or the peer sends us a sack option with invalid length, don't 260 * update the scoreboard. 261 */ 262 if (!TCP_SACK_ENABLED(tp) || ((th->th_flags & TH_ACK) == 0) || 263 (optlen % 8 != 2 || optlen < 10)) { 264 return; 265 } 266 267 /* 268 * If we don't want any SACK holes to be allocated, just return. 269 */ 270 if (tcp_sack_globalmaxholes == 0 || tcp_sack_tp_maxholes == 0) { 271 return; 272 } 273 274 /* If the ACK is outside [snd_una, snd_max], ignore the SACK options. */ 275 if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max)) 276 return; 277 278 /* 279 * Extract SACK blocks. 280 * 281 * Note that t_sack_block is sorted so that we only need to do 282 * one pass over the sequence number space. (SACK "fast-path") 283 */ 284 num_sack_blks = optlen / 8; 285 acked = (SEQ_GT(th->th_ack, tp->snd_una)) ? th->th_ack : tp->snd_una; 286 for (i = 0; i < num_sack_blks; i++, lp += sizeof(uint32_t) * 2) { 287 memcpy(&left, lp, sizeof(uint32_t)); 288 memcpy(&right, lp + sizeof(uint32_t), sizeof(uint32_t)); 289 left = ntohl(left); 290 right = ntohl(right); 291 292 if (SEQ_LEQ(right, acked) || SEQ_GT(right, tp->snd_max) || 293 SEQ_GEQ(left, right)) { 294 /* SACK entry that's old, or invalid. */ 295 i--; 296 num_sack_blks--; 297 continue; 298 } 299 300 /* Insertion sort. */ 301 for (j = i; (j > 0) && SEQ_LT(left, t_sack_block[j - 1].left); 302 j--) { 303 t_sack_block[j].left = t_sack_block[j - 1].left; 304 t_sack_block[j].right = t_sack_block[j - 1].right; 305 } 306 t_sack_block[j].left = left; 307 t_sack_block[j].right = right; 308 } 309 310 /* Update the scoreboard. */ 311 cur = TAILQ_FIRST(&tp->snd_holes); 312 for (i = 0; i < num_sack_blks; i++) { 313 sack = &t_sack_block[i]; 314 /* 315 * FACK TCP. Update snd_fack so we can enter Fast 316 * Recovery early. 317 */ 318 if (SEQ_GEQ(sack->right, tp->snd_fack)) 319 tp->snd_fack = sack->right; 320 321 if (TAILQ_EMPTY(&tp->snd_holes)) { 322 /* First hole. */ 323 cur = sack_inserthole(tp, th->th_ack, sack->left, NULL); 324 if (cur == NULL) { 325 /* ENOBUFS, bail out*/ 326 return; 327 } 328 tp->rcv_lastsack = sack->right; 329 continue; /* With next sack block */ 330 } 331 332 /* Go through the list of holes. */ 333 while (cur) { 334 if (SEQ_LEQ(sack->right, cur->start)) 335 /* SACKs data before the current hole */ 336 break; /* No use going through more holes */ 337 338 if (SEQ_GEQ(sack->left, cur->end)) { 339 /* SACKs data beyond the current hole */ 340 cur = TAILQ_NEXT(cur, sackhole_q); 341 continue; 342 } 343 344 if (SEQ_LEQ(sack->left, cur->start)) { 345 /* Data acks at least the beginning of hole */ 346 if (SEQ_GEQ(sack->right, cur->end)) { 347 /* Acks entire hole, so delete hole */ 348 cur = sack_removehole(tp, cur); 349 break; 350 } 351 352 /* Otherwise, move start of hole forward */ 353 cur->start = sack->right; 354 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 355 break; 356 } 357 358 if (SEQ_GEQ(sack->right, cur->end)) { 359 /* Move end of hole backward. */ 360 cur->end = sack->left; 361 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 362 cur = TAILQ_NEXT(cur, sackhole_q); 363 break; 364 } 365 366 if (SEQ_LT(cur->start, sack->left) && 367 SEQ_GT(cur->end, sack->right)) { 368 /* 369 * ACKs some data in middle of a hole; need to 370 * split current hole 371 */ 372 tmp = sack_inserthole(tp, sack->right, cur->end, 373 cur); 374 if (tmp == NULL) { 375 return; 376 } 377 tmp->rxmit = SEQ_MAX(cur->rxmit, tmp->start); 378 cur->end = sack->left; 379 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 380 cur = tmp; 381 break; 382 } 383 } 384 385 /* At this point, we have reached the tail of the list. */ 386 if (SEQ_LT(tp->rcv_lastsack, sack->left)) { 387 /* 388 * Need to append new hole at end. 389 */ 390 cur = sack_inserthole(tp, tp->rcv_lastsack, sack->left, 391 NULL); 392 if (cur == NULL) { 393 return; 394 } 395 } 396 if (SEQ_LT(tp->rcv_lastsack, sack->right)) { 397 tp->rcv_lastsack = sack->right; 398 } 399 } 400 } 401 402 /* 403 * tcp_del_sackholes: remove holes covered by a cumulative ACK. 404 */ 405 406 void 407 tcp_del_sackholes(struct tcpcb *tp, const struct tcphdr *th) 408 { 409 /* Max because this could be an older ack that just arrived. */ 410 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ? 411 th->th_ack : tp->snd_una; 412 struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes); 413 414 while (cur) { 415 if (SEQ_LEQ(cur->end, lastack)) { 416 cur = sack_removehole(tp, cur); 417 } else if (SEQ_LT(cur->start, lastack)) { 418 cur->start = lastack; 419 if (SEQ_LT(cur->rxmit, cur->start)) 420 cur->rxmit = cur->start; 421 break; 422 } else 423 break; 424 } 425 } 426 427 /* 428 * tcp_free_sackholes: clear the scoreboard. 429 */ 430 431 void 432 tcp_free_sackholes(struct tcpcb *tp) 433 { 434 struct sackhole *sack; 435 436 /* Free up the SACK hole list. */ 437 while ((sack = TAILQ_FIRST(&tp->snd_holes)) != NULL) { 438 sack_removehole(tp, sack); 439 } 440 KASSERT(tp->snd_numholes == 0); 441 } 442 443 /* 444 * Returns pointer to a sackhole if there are any pending retransmissions; 445 * NULL otherwise. 446 */ 447 struct sackhole * 448 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 449 { 450 struct sackhole *cur = NULL; 451 452 if (!TCP_SACK_ENABLED(tp)) 453 return (NULL); 454 455 *sack_bytes_rexmt = 0; 456 TAILQ_FOREACH(cur, &tp->snd_holes, sackhole_q) { 457 if (SEQ_LT(cur->rxmit, cur->end)) { 458 if (SEQ_LT(cur->rxmit, tp->snd_una)) { 459 /* old SACK hole */ 460 continue; 461 } 462 *sack_bytes_rexmt += (cur->rxmit - cur->start); 463 break; 464 } 465 *sack_bytes_rexmt += (cur->rxmit - cur->start); 466 } 467 468 return (cur); 469 } 470 471 /* 472 * After a timeout, the SACK list may be rebuilt. This SACK information 473 * should be used to avoid retransmitting SACKed data. This function 474 * traverses the SACK list to see if snd_nxt should be moved forward. 475 */ 476 void 477 tcp_sack_adjust(struct tcpcb *tp) 478 { 479 struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes); 480 struct sackhole *n = NULL; 481 482 if (TAILQ_EMPTY(&tp->snd_holes)) 483 return; /* No holes */ 484 if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack)) 485 return; /* We're already beyond any SACKed blocks */ 486 487 /* 488 * Two cases for which we want to advance snd_nxt: 489 * i) snd_nxt lies between end of one hole and beginning of another 490 * ii) snd_nxt lies between end of last hole and rcv_lastsack 491 */ 492 while ((n = TAILQ_NEXT(cur, sackhole_q)) != NULL) { 493 if (SEQ_LT(tp->snd_nxt, cur->end)) 494 return; 495 if (SEQ_GEQ(tp->snd_nxt, n->start)) 496 cur = n; 497 else { 498 tp->snd_nxt = n->start; 499 return; 500 } 501 } 502 if (SEQ_LT(tp->snd_nxt, cur->end)) 503 return; 504 tp->snd_nxt = tp->rcv_lastsack; 505 506 return; 507 } 508 509 /* 510 * tcp_sack_numblks: return the number of SACK blocks to send. 511 */ 512 513 int 514 tcp_sack_numblks(const struct tcpcb *tp) 515 { 516 int numblks; 517 518 if (!TCP_SACK_ENABLED(tp)) { 519 return 0; 520 } 521 522 numblks = (((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) ? 1 : 0) + 523 tp->t_segqlen; 524 525 if (numblks == 0) { 526 return 0; 527 } 528 529 if (numblks > TCP_SACK_MAX) { 530 numblks = TCP_SACK_MAX; 531 } 532 533 return numblks; 534 } 535 536 #if defined(DDB) 537 void sack_dump(const struct tcpcb *); 538 539 void 540 sack_dump(const struct tcpcb *tp) 541 { 542 const struct sackhole *cur; 543 544 printf("snd_una=%" PRIu32 ", snd_max=%" PRIu32 "\n", 545 tp->snd_una, tp->snd_max); 546 printf("rcv_lastsack=%" PRIu32 ", snd_fack=%" PRIu32 "\n", 547 tp->rcv_lastsack, tp->snd_fack); 548 printf("numholes=%d\n", tp->snd_numholes); 549 TAILQ_FOREACH(cur, &tp->snd_holes, sackhole_q) { 550 printf("\t%" PRIu32 "-%" PRIu32 ", rxmit=%" PRIu32 "\n", 551 cur->start, cur->end, cur->rxmit); 552 } 553 } 554 #endif /* defined(DDB) */ 555