| Commit | Line | Data |
|---|---|---|
| 91489f6b JH |
1 | /* |
| 2 | * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. | |
| 3 | * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. | |
| 4 | * | |
| 5 | * This code is derived from software contributed to The DragonFly Project | |
| 6 | * by Jeffrey M. Hsu. | |
| 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 DragonFly Project nor the names of its | |
| 17 | * contributors may be used to endorse or promote products derived | |
| 18 | * from this software without specific, prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 31 | * SUCH DAMAGE. | |
| 32 | * | |
| 12f68b86 | 33 | * $DragonFly: src/sys/netinet/tcp_sack.c,v 1.8 2008/08/15 21:37:16 nth Exp $ |
| 91489f6b JH |
34 | */ |
| 35 | ||
| 36 | #include <sys/param.h> | |
| 37 | #include <sys/systm.h> | |
| 38 | #include <sys/kernel.h> | |
| 39 | #include <sys/malloc.h> | |
| 40 | #include <sys/queue.h> | |
| 41 | #include <sys/thread.h> | |
| 42 | #include <sys/types.h> | |
| 43 | #include <sys/socket.h> | |
| 44 | #include <sys/socketvar.h> | |
| 45 | ||
| 46 | #include <net/if.h> | |
| 47 | ||
| 48 | #include <netinet/in.h> | |
| 49 | #include <netinet/in_systm.h> | |
| 50 | #include <netinet/ip.h> | |
| 51 | #include <netinet/in_var.h> | |
| 52 | #include <netinet/in_pcb.h> | |
| 53 | #include <netinet/ip_var.h> | |
| 54 | #include <netinet/tcp.h> | |
| 55 | #include <netinet/tcp_seq.h> | |
| 56 | #include <netinet/tcp_var.h> | |
| 57 | ||
| bf55dc6d SZ |
58 | /* |
| 59 | * Implemented: | |
| 60 | * | |
| 61 | * RFC 2018 | |
| 62 | * RFC 2883 | |
| 63 | * RFC 3517 | |
| 64 | */ | |
| 65 | ||
| 91489f6b JH |
66 | struct sackblock { |
| 67 | tcp_seq sblk_start; | |
| 68 | tcp_seq sblk_end; | |
| 69 | TAILQ_ENTRY(sackblock) sblk_list; | |
| 70 | }; | |
| 71 | ||
| 72 | #define MAXSAVEDBLOCKS 8 /* per connection limit */ | |
| 73 | ||
| 01d244e6 | 74 | static int insert_block(struct scoreboard *scb, |
| 6c1bbf57 | 75 | const struct raw_sackblock *raw_sb, boolean_t *update); |
| 91489f6b | 76 | static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una, |
| 7e4852bb | 77 | u_int maxseg, int rxtthresh); |
| 91489f6b | 78 | |
| 12f68b86 | 79 | static MALLOC_DEFINE(M_SACKBLOCK, "sblk", "sackblock struct"); |
| 91489f6b JH |
80 | |
| 81 | /* | |
| 82 | * Per-tcpcb initialization. | |
| 83 | */ | |
| 84 | void | |
| 85 | tcp_sack_tcpcb_init(struct tcpcb *tp) | |
| 86 | { | |
| 87 | struct scoreboard *scb = &tp->scb; | |
| 88 | ||
| 89 | scb->nblocks = 0; | |
| 90 | TAILQ_INIT(&scb->sackblocks); | |
| 91 | scb->lastfound = NULL; | |
| 92 | } | |
| 93 | ||
| 94 | /* | |
| 95 | * Find the SACK block containing or immediately preceding "seq". | |
| 96 | * The boolean result indicates whether the sequence is actually | |
| 97 | * contained in the SACK block. | |
| 98 | */ | |
| 99 | static boolean_t | |
| 100 | sack_block_lookup(struct scoreboard *scb, tcp_seq seq, struct sackblock **sb) | |
| 101 | { | |
| 102 | struct sackblock *hint = scb->lastfound; | |
| 103 | struct sackblock *cur, *last, *prev; | |
| 104 | ||
| 105 | if (TAILQ_EMPTY(&scb->sackblocks)) { | |
| 106 | *sb = NULL; | |
| 107 | return FALSE; | |
| 108 | } | |
| 109 | ||
| 110 | if (hint == NULL) { | |
| 111 | /* No hint. Search from start to end. */ | |
| 112 | cur = TAILQ_FIRST(&scb->sackblocks); | |
| 113 | last = NULL; | |
| 114 | prev = TAILQ_LAST(&scb->sackblocks, sackblock_list); | |
| 115 | } else { | |
| 116 | if (SEQ_GEQ(seq, hint->sblk_start)) { | |
| 117 | /* Search from hint to end of list. */ | |
| 118 | cur = hint; | |
| 119 | last = NULL; | |
| 120 | prev = TAILQ_LAST(&scb->sackblocks, sackblock_list); | |
| 121 | } else { | |
| 122 | /* Search from front of list to hint. */ | |
| 123 | cur = TAILQ_FIRST(&scb->sackblocks); | |
| 124 | last = hint; | |
| 125 | prev = TAILQ_PREV(hint, sackblock_list, sblk_list); | |
| 126 | } | |
| 127 | } | |
| 128 | ||
| 129 | do { | |
| 130 | if (SEQ_GT(cur->sblk_end, seq)) { | |
| 131 | if (SEQ_GEQ(seq, cur->sblk_start)) { | |
| 132 | *sb = scb->lastfound = cur; | |
| 133 | return TRUE; | |
| 134 | } else { | |
| 135 | *sb = scb->lastfound = | |
| 136 | TAILQ_PREV(cur, sackblock_list, sblk_list); | |
| 137 | return FALSE; | |
| 138 | } | |
| 139 | } | |
| 140 | cur = TAILQ_NEXT(cur, sblk_list); | |
| 141 | } while (cur != last); | |
| 142 | ||
| 143 | *sb = scb->lastfound = prev; | |
| 144 | return FALSE; | |
| 145 | } | |
| 146 | ||
| 147 | /* | |
| 148 | * Allocate a SACK block. | |
| 149 | */ | |
| 150 | static __inline struct sackblock * | |
| 006af0db | 151 | alloc_sackblock(struct scoreboard *scb, const struct raw_sackblock *raw_sb) |
| 91489f6b | 152 | { |
| 26d3f1fb SZ |
153 | struct sackblock *sb; |
| 154 | ||
| 006af0db SZ |
155 | if (scb->freecache != NULL) { |
| 156 | sb = scb->freecache; | |
| 157 | scb->freecache = NULL; | |
| 158 | tcpstat.tcps_sacksbfast++; | |
| 01d244e6 | 159 | } else { |
| 006af0db SZ |
160 | sb = kmalloc(sizeof(struct sackblock), M_SACKBLOCK, M_NOWAIT); |
| 161 | if (sb == NULL) { | |
| 162 | tcpstat.tcps_sacksbfailed++; | |
| 163 | return NULL; | |
| 164 | } | |
| 26d3f1fb | 165 | } |
| 006af0db SZ |
166 | sb->sblk_start = raw_sb->rblk_start; |
| 167 | sb->sblk_end = raw_sb->rblk_end; | |
| 26d3f1fb | 168 | return sb; |
| 91489f6b JH |
169 | } |
| 170 | ||
| 01d244e6 SZ |
171 | static __inline struct sackblock * |
| 172 | alloc_sackblock_limit(struct scoreboard *scb, | |
| 173 | const struct raw_sackblock *raw_sb) | |
| 174 | { | |
| 175 | if (scb->nblocks == MAXSAVEDBLOCKS) { | |
| 176 | /* | |
| 177 | * Should try to kick out older blocks XXX JH | |
| 178 | * May be able to coalesce with existing block. | |
| 179 | * Or, go other way and free all blocks if we hit | |
| 180 | * this limit. | |
| 181 | */ | |
| 182 | tcpstat.tcps_sacksboverflow++; | |
| 183 | return NULL; | |
| 184 | } | |
| 006af0db | 185 | return alloc_sackblock(scb, raw_sb); |
| 01d244e6 SZ |
186 | } |
| 187 | ||
| 91489f6b JH |
188 | /* |
| 189 | * Free a SACK block. | |
| 190 | */ | |
| 191 | static __inline void | |
| 006af0db | 192 | free_sackblock(struct scoreboard *scb, struct sackblock *s) |
| 91489f6b | 193 | { |
| 006af0db SZ |
194 | if (scb->freecache == NULL) { |
| 195 | /* YYY Maybe use the latest freed block? */ | |
| 196 | scb->freecache = s; | |
| 197 | return; | |
| 198 | } | |
| 12f68b86 | 199 | kfree(s, M_SACKBLOCK); |
| 91489f6b JH |
200 | } |
| 201 | ||
| 202 | /* | |
| 203 | * Free up SACK blocks for data that's been acked. | |
| 204 | */ | |
| 205 | static void | |
| 206 | tcp_sack_ack_blocks(struct scoreboard *scb, tcp_seq th_ack) | |
| 207 | { | |
| 208 | struct sackblock *sb, *nb; | |
| 209 | ||
| 210 | sb = TAILQ_FIRST(&scb->sackblocks); | |
| 211 | while (sb && SEQ_LEQ(sb->sblk_end, th_ack)) { | |
| 212 | nb = TAILQ_NEXT(sb, sblk_list); | |
| 9e3d6c96 | 213 | if (scb->lastfound == sb) |
| 91489f6b JH |
214 | scb->lastfound = NULL; |
| 215 | TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list); | |
| 006af0db | 216 | free_sackblock(scb, sb); |
| 91489f6b JH |
217 | --scb->nblocks; |
| 218 | KASSERT(scb->nblocks >= 0, | |
| 219 | ("SACK block count underflow: %d < 0", scb->nblocks)); | |
| 220 | sb = nb; | |
| 221 | } | |
| 222 | if (sb && SEQ_GT(th_ack, sb->sblk_start)) | |
| 223 | sb->sblk_start = th_ack; /* other side reneged? XXX */ | |
| 224 | } | |
| 225 | ||
| 226 | /* | |
| 227 | * Delete and free SACK blocks saved in scoreboard. | |
| 228 | */ | |
| 229 | void | |
| 230 | tcp_sack_cleanup(struct scoreboard *scb) | |
| 231 | { | |
| 232 | struct sackblock *sb, *nb; | |
| 233 | ||
| 234 | TAILQ_FOREACH_MUTABLE(sb, &scb->sackblocks, sblk_list, nb) { | |
| 006af0db | 235 | free_sackblock(scb, sb); |
| 91489f6b JH |
236 | --scb->nblocks; |
| 237 | } | |
| 238 | KASSERT(scb->nblocks == 0, | |
| 239 | ("SACK block %d count not zero", scb->nblocks)); | |
| 240 | TAILQ_INIT(&scb->sackblocks); | |
| 241 | scb->lastfound = NULL; | |
| 242 | } | |
| 243 | ||
| 244 | /* | |
| 006af0db SZ |
245 | * Delete and free SACK blocks saved in scoreboard. |
| 246 | * Delete the one slot block cache. | |
| 247 | */ | |
| 248 | void | |
| 249 | tcp_sack_destroy(struct scoreboard *scb) | |
| 250 | { | |
| 251 | tcp_sack_cleanup(scb); | |
| 252 | if (scb->freecache != NULL) { | |
| 253 | kfree(scb->freecache, M_SACKBLOCK); | |
| 254 | scb->freecache = NULL; | |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 258 | /* | |
| d58ca578 SZ |
259 | * Cleanup the reported SACK block information |
| 260 | */ | |
| 261 | void | |
| 262 | tcp_sack_report_cleanup(struct tcpcb *tp) | |
| 263 | { | |
| 264 | tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT); | |
| 265 | tp->reportblk.rblk_start = tp->reportblk.rblk_end; | |
| 266 | } | |
| 267 | ||
| 268 | /* | |
| 91489f6b JH |
269 | * Returns 0 if not D-SACK block, |
| 270 | * 1 if D-SACK, | |
| 271 | * 2 if duplicate of out-of-order D-SACK block. | |
| 272 | */ | |
| 273 | int | |
| 274 | tcp_sack_ndsack_blocks(struct raw_sackblock *blocks, const int numblocks, | |
| 275 | tcp_seq snd_una) | |
| 276 | { | |
| 277 | if (numblocks == 0) | |
| 278 | return 0; | |
| 279 | ||
| 280 | if (SEQ_LT(blocks[0].rblk_start, snd_una)) | |
| 281 | return 1; | |
| 282 | ||
| 283 | /* block 0 inside block 1 */ | |
| 284 | if (numblocks > 1 && | |
| 285 | SEQ_GEQ(blocks[0].rblk_start, blocks[1].rblk_start) && | |
| 286 | SEQ_LEQ(blocks[0].rblk_end, blocks[1].rblk_end)) | |
| 287 | return 2; | |
| 288 | ||
| 289 | return 0; | |
| 290 | } | |
| 291 | ||
| 292 | /* | |
| 293 | * Update scoreboard on new incoming ACK. | |
| 294 | */ | |
| 295 | static void | |
| 296 | tcp_sack_add_blocks(struct tcpcb *tp, struct tcpopt *to) | |
| 297 | { | |
| 298 | const int numblocks = to->to_nsackblocks; | |
| 299 | struct raw_sackblock *blocks = to->to_sackblocks; | |
| 300 | struct scoreboard *scb = &tp->scb; | |
| 6c1bbf57 | 301 | int startblock, i; |
| 91489f6b JH |
302 | |
| 303 | if (tcp_sack_ndsack_blocks(blocks, numblocks, tp->snd_una) > 0) | |
| 304 | startblock = 1; | |
| 305 | else | |
| 306 | startblock = 0; | |
| 307 | ||
| 6c1bbf57 | 308 | to->to_flags |= TOF_SACK_REDUNDANT; |
| 91489f6b JH |
309 | for (i = startblock; i < numblocks; i++) { |
| 310 | struct raw_sackblock *newsackblock = &blocks[i]; | |
| 6c1bbf57 SZ |
311 | boolean_t update; |
| 312 | int error; | |
| 91489f6b | 313 | |
| 331721f5 SZ |
314 | /* Guard against ACK reordering */ |
| 315 | if (SEQ_LT(newsackblock->rblk_start, tp->snd_una)) | |
| 316 | continue; | |
| 317 | ||
| 318 | /* Don't accept bad SACK blocks */ | |
| 02cc2f35 SZ |
319 | if (SEQ_GT(newsackblock->rblk_end, tp->snd_max)) { |
| 320 | tcpstat.tcps_rcvbadsackopt++; | |
| 91489f6b | 321 | break; /* skip all other blocks */ |
| 02cc2f35 SZ |
322 | } |
| 323 | tcpstat.tcps_sacksbupdate++; | |
| 91489f6b | 324 | |
| 6c1bbf57 SZ |
325 | error = insert_block(scb, newsackblock, &update); |
| 326 | if (update) | |
| 327 | to->to_flags &= ~TOF_SACK_REDUNDANT; | |
| 328 | if (error) | |
| 01d244e6 | 329 | break; |
| 91489f6b JH |
330 | } |
| 331 | } | |
| 332 | ||
| 333 | void | |
| 334 | tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to) | |
| 335 | { | |
| 336 | struct scoreboard *scb = &tp->scb; | |
| a098966f | 337 | int rexmt_high_update = 0; |
| 91489f6b JH |
338 | |
| 339 | tcp_sack_ack_blocks(scb, tp->snd_una); | |
| 340 | tcp_sack_add_blocks(tp, to); | |
| 7e4852bb | 341 | update_lostseq(scb, tp->snd_una, tp->t_maxseg, tp->t_rxtthresh); |
| a098966f | 342 | if (SEQ_LT(tp->rexmt_high, tp->snd_una)) { |
| 91489f6b | 343 | tp->rexmt_high = tp->snd_una; |
| a098966f SZ |
344 | rexmt_high_update = 1; |
| 345 | } | |
| 346 | if (tp->t_flags & TF_SACKRESCUED) { | |
| 347 | if (SEQ_LT(tp->rexmt_rescue, tp->snd_una)) { | |
| 348 | tp->t_flags &= ~TF_SACKRESCUED; | |
| 21f337ca | 349 | } else if (tcp_aggressive_rescuesack && rexmt_high_update && |
| a098966f SZ |
350 | SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) { |
| 351 | /* Drag RescueRxt along with HighRxt */ | |
| 352 | tp->rexmt_rescue = tp->rexmt_high; | |
| 353 | } | |
| 354 | } | |
| 91489f6b JH |
355 | } |
| 356 | ||
| 357 | /* | |
| 358 | * Insert SACK block into sender's scoreboard. | |
| 359 | */ | |
| 01d244e6 | 360 | static int |
| 6c1bbf57 SZ |
361 | insert_block(struct scoreboard *scb, const struct raw_sackblock *raw_sb, |
| 362 | boolean_t *update) | |
| 91489f6b JH |
363 | { |
| 364 | struct sackblock *sb, *workingblock; | |
| 365 | boolean_t overlap_front; | |
| 366 | ||
| 6c1bbf57 | 367 | *update = TRUE; |
| 2ed615e0 | 368 | if (TAILQ_EMPTY(&scb->sackblocks)) { |
| 01d244e6 SZ |
369 | struct sackblock *newblock; |
| 370 | ||
| 2ed615e0 | 371 | KASSERT(scb->nblocks == 0, ("emply scb w/ blocks")); |
| 01d244e6 | 372 | |
| 006af0db | 373 | newblock = alloc_sackblock(scb, raw_sb); |
| 01d244e6 SZ |
374 | if (newblock == NULL) |
| 375 | return ENOMEM; | |
| 2ed615e0 | 376 | TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list); |
| 01d244e6 SZ |
377 | scb->nblocks = 1; |
| 378 | return 0; | |
| 2ed615e0 | 379 | } |
| 91489f6b | 380 | |
| 01d244e6 SZ |
381 | KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks")); |
| 382 | KASSERT(scb->nblocks <= MAXSAVEDBLOCKS, | |
| 91489f6b JH |
383 | ("too many SACK blocks %d", scb->nblocks)); |
| 384 | ||
| 01d244e6 | 385 | overlap_front = sack_block_lookup(scb, raw_sb->rblk_start, &sb); |
| 91489f6b JH |
386 | |
| 387 | if (sb == NULL) { | |
| 01d244e6 SZ |
388 | workingblock = alloc_sackblock_limit(scb, raw_sb); |
| 389 | if (workingblock == NULL) | |
| 390 | return ENOMEM; | |
| 391 | TAILQ_INSERT_HEAD(&scb->sackblocks, workingblock, sblk_list); | |
| 91489f6b JH |
392 | ++scb->nblocks; |
| 393 | } else { | |
| 01d244e6 SZ |
394 | if (overlap_front || sb->sblk_end == raw_sb->rblk_start) { |
| 395 | /* Extend old block */ | |
| 91489f6b | 396 | workingblock = sb; |
| 01d244e6 SZ |
397 | if (SEQ_GT(raw_sb->rblk_end, sb->sblk_end)) |
| 398 | sb->sblk_end = raw_sb->rblk_end; | |
| 6c1bbf57 SZ |
399 | else |
| 400 | *update = FALSE; | |
| 02cc2f35 | 401 | tcpstat.tcps_sacksbreused++; |
| 91489f6b | 402 | } else { |
| 01d244e6 SZ |
403 | workingblock = alloc_sackblock_limit(scb, raw_sb); |
| 404 | if (workingblock == NULL) | |
| 405 | return ENOMEM; | |
| 406 | TAILQ_INSERT_AFTER(&scb->sackblocks, sb, workingblock, | |
| 407 | sblk_list); | |
| 91489f6b JH |
408 | ++scb->nblocks; |
| 409 | } | |
| 410 | } | |
| 411 | ||
| 412 | /* Consolidate right-hand side. */ | |
| 413 | sb = TAILQ_NEXT(workingblock, sblk_list); | |
| 414 | while (sb != NULL && | |
| 415 | SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) { | |
| 416 | struct sackblock *nextblock; | |
| 417 | ||
| 418 | nextblock = TAILQ_NEXT(sb, sblk_list); | |
| 9e3d6c96 MD |
419 | if (scb->lastfound == sb) |
| 420 | scb->lastfound = NULL; | |
| 91489f6b JH |
421 | /* Remove completely overlapped block */ |
| 422 | TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list); | |
| 006af0db | 423 | free_sackblock(scb, sb); |
| 91489f6b JH |
424 | --scb->nblocks; |
| 425 | KASSERT(scb->nblocks > 0, | |
| 426 | ("removed overlapped block: %d blocks left", scb->nblocks)); | |
| 427 | sb = nextblock; | |
| 428 | } | |
| 429 | if (sb != NULL && | |
| 430 | SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) { | |
| 431 | /* Extend new block to cover partially overlapped old block. */ | |
| 432 | workingblock->sblk_end = sb->sblk_end; | |
| 9e3d6c96 MD |
433 | if (scb->lastfound == sb) |
| 434 | scb->lastfound = NULL; | |
| 91489f6b | 435 | TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list); |
| 006af0db | 436 | free_sackblock(scb, sb); |
| 91489f6b JH |
437 | --scb->nblocks; |
| 438 | KASSERT(scb->nblocks > 0, | |
| 439 | ("removed partial right: %d blocks left", scb->nblocks)); | |
| 440 | } | |
| 01d244e6 | 441 | return 0; |
| 91489f6b JH |
442 | } |
| 443 | ||
| 444 | #ifdef DEBUG_SACK_BLOCKS | |
| 445 | static void | |
| 446 | tcp_sack_dump_blocks(struct scoreboard *scb) | |
| 447 | { | |
| 448 | struct sackblock *sb; | |
| 449 | ||
| a6ec04bc | 450 | kprintf("%d blocks:", scb->nblocks); |
| 91489f6b | 451 | TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) |
| a6ec04bc SW |
452 | kprintf(" [%u, %u)", sb->sblk_start, sb->sblk_end); |
| 453 | kprintf("\n"); | |
| 91489f6b JH |
454 | } |
| 455 | #else | |
| 456 | static __inline void | |
| 457 | tcp_sack_dump_blocks(struct scoreboard *scb) | |
| 458 | { | |
| 459 | } | |
| 460 | #endif | |
| 461 | ||
| 462 | /* | |
| 463 | * Optimization to quickly determine which packets are lost. | |
| 464 | */ | |
| 465 | static void | |
| 7e4852bb SZ |
466 | update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg, |
| 467 | int rxtthresh) | |
| 91489f6b JH |
468 | { |
| 469 | struct sackblock *sb; | |
| 470 | int nsackblocks = 0; | |
| 471 | int bytes_sacked = 0; | |
| ffe35e17 SZ |
472 | int rxtthresh_bytes; |
| 473 | ||
| 474 | if (tcp_do_rfc3517bis) | |
| 475 | rxtthresh_bytes = (rxtthresh - 1) * maxseg; | |
| 476 | else | |
| 477 | rxtthresh_bytes = rxtthresh * maxseg; | |
| 91489f6b JH |
478 | |
| 479 | sb = TAILQ_LAST(&scb->sackblocks, sackblock_list); | |
| 480 | while (sb != NULL) { | |
| 481 | ++nsackblocks; | |
| 482 | bytes_sacked += sb->sblk_end - sb->sblk_start; | |
| 7e4852bb | 483 | if (nsackblocks == rxtthresh || |
| ffe35e17 | 484 | bytes_sacked >= rxtthresh_bytes) { |
| 91489f6b JH |
485 | scb->lostseq = sb->sblk_start; |
| 486 | return; | |
| 487 | } | |
| 488 | sb = TAILQ_PREV(sb, sackblock_list, sblk_list); | |
| 489 | } | |
| 490 | scb->lostseq = snd_una; | |
| 491 | } | |
| 492 | ||
| 493 | /* | |
| 494 | * Return whether the given sequence number is considered lost. | |
| 495 | */ | |
| ffe35e17 SZ |
496 | boolean_t |
| 497 | tcp_sack_islost(struct scoreboard *scb, tcp_seq seqnum) | |
| 91489f6b JH |
498 | { |
| 499 | return SEQ_LT(seqnum, scb->lostseq); | |
| 500 | } | |
| 501 | ||
| 502 | /* | |
| 503 | * True if at least "amount" has been SACKed. Used by Early Retransmit. | |
| 504 | */ | |
| 505 | boolean_t | |
| 506 | tcp_sack_has_sacked(struct scoreboard *scb, u_int amount) | |
| 507 | { | |
| 508 | struct sackblock *sb; | |
| 509 | int bytes_sacked = 0; | |
| 510 | ||
| 511 | TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) { | |
| 512 | bytes_sacked += sb->sblk_end - sb->sblk_start; | |
| 513 | if (bytes_sacked >= amount) | |
| 514 | return TRUE; | |
| 515 | } | |
| 516 | return FALSE; | |
| 517 | } | |
| 518 | ||
| 519 | /* | |
| 9a5b142f JH |
520 | * Number of bytes SACKed below seq. |
| 521 | */ | |
| 522 | int | |
| 523 | tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq) | |
| 524 | { | |
| 525 | struct sackblock *sb; | |
| 526 | int bytes_sacked = 0; | |
| 527 | ||
| 528 | sb = TAILQ_FIRST(&scb->sackblocks); | |
| 529 | while (sb && SEQ_GT(seq, sb->sblk_start)) { | |
| 530 | bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start; | |
| 531 | sb = TAILQ_NEXT(sb, sblk_list); | |
| 532 | } | |
| 533 | return bytes_sacked; | |
| 534 | } | |
| 535 | ||
| 536 | /* | |
| 91489f6b JH |
537 | * Return estimate of the number of bytes outstanding in the network. |
| 538 | */ | |
| 539 | uint32_t | |
| 540 | tcp_sack_compute_pipe(struct tcpcb *tp) | |
| 541 | { | |
| 542 | struct scoreboard *scb = &tp->scb; | |
| 543 | struct sackblock *sb; | |
| 544 | int nlost, nretransmitted; | |
| 545 | tcp_seq end; | |
| 546 | ||
| 547 | nlost = tp->snd_max - scb->lostseq; | |
| 548 | nretransmitted = tp->rexmt_high - tp->snd_una; | |
| 549 | ||
| 550 | TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) { | |
| 551 | if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) { | |
| 552 | end = seq_min(sb->sblk_end, tp->rexmt_high); | |
| 553 | nretransmitted -= end - sb->sblk_start; | |
| 554 | } | |
| 555 | if (SEQ_GEQ(sb->sblk_start, scb->lostseq)) | |
| 556 | nlost -= sb->sblk_end - sb->sblk_start; | |
| 557 | } | |
| 558 | ||
| 559 | return (nlost + nretransmitted); | |
| 560 | } | |
| 561 | ||
| 562 | /* | |
| 563 | * Return the sequence number and length of the next segment to transmit | |
| 564 | * when in Fast Recovery. | |
| 565 | */ | |
| 566 | boolean_t | |
| 567 | tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen, | |
| 1bdfd728 | 568 | boolean_t *rescue) |
| 91489f6b JH |
569 | { |
| 570 | struct scoreboard *scb = &tp->scb; | |
| 571 | struct socket *so = tp->t_inpcb->inp_socket; | |
| 572 | struct sackblock *sb; | |
| 573 | const struct sackblock *lastblock = | |
| 574 | TAILQ_LAST(&scb->sackblocks, sackblock_list); | |
| 575 | tcp_seq torexmt; | |
| 576 | long len, off; | |
| 577 | ||
| 578 | /* skip SACKed data */ | |
| 579 | tcp_sack_skip_sacked(scb, &tp->rexmt_high); | |
| 580 | ||
| 581 | /* Look for lost data. */ | |
| 582 | torexmt = tp->rexmt_high; | |
| 1bdfd728 | 583 | *rescue = FALSE; |
| 91489f6b JH |
584 | if (lastblock != NULL) { |
| 585 | if (SEQ_LT(torexmt, lastblock->sblk_end) && | |
| ffe35e17 | 586 | tcp_sack_islost(scb, torexmt)) { |
| 91489f6b JH |
587 | sendunsacked: |
| 588 | *nextrexmt = torexmt; | |
| 589 | /* If the left-hand edge has been SACKed, pull it in. */ | |
| 590 | if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb)) | |
| 591 | *plen = sb->sblk_start - torexmt; | |
| 592 | else | |
| 593 | *plen = tp->t_maxseg; | |
| 594 | return TRUE; | |
| 595 | } | |
| 596 | } | |
| 597 | ||
| 598 | /* See if unsent data available within send window. */ | |
| 599 | off = tp->snd_max - tp->snd_una; | |
| 6d49aa6f | 600 | len = (long) ulmin(so->so_snd.ssb_cc, tp->snd_wnd) - off; |
| 91489f6b JH |
601 | if (len > 0) { |
| 602 | *nextrexmt = tp->snd_max; /* Send new data. */ | |
| 603 | *plen = tp->t_maxseg; | |
| 604 | return TRUE; | |
| 605 | } | |
| 606 | ||
| 607 | /* We're less certain this data has been lost. */ | |
| 1bdfd728 | 608 | if (lastblock != NULL && SEQ_LT(torexmt, lastblock->sblk_end)) |
| 91489f6b JH |
609 | goto sendunsacked; |
| 610 | ||
| a098966f | 611 | /* Rescue retransmission */ |
| ffe35e17 | 612 | if (tcp_do_rescuesack || tcp_do_rfc3517bis) { |
| 1bdfd728 | 613 | tcpstat.tcps_sackrescue_try++; |
| a098966f SZ |
614 | if (tp->t_flags & TF_SACKRESCUED) { |
| 615 | if (!tcp_aggressive_rescuesack) | |
| 616 | return FALSE; | |
| 1bdfd728 | 617 | |
| a098966f SZ |
618 | /* |
| 619 | * Aggressive variant of the rescue retransmission. | |
| 620 | * | |
| 621 | * The idea of the rescue retransmission is to sustain | |
| 622 | * the ACK clock thus to avoid timeout retransmission. | |
| 623 | * | |
| 624 | * Under some situations, the conservative approach | |
| 625 | * suggested in the draft | |
| 626 | * http://tools.ietf.org/html/ | |
| 627 | * draft-nishida-tcpm-rescue-retransmission-00 | |
| 628 | * could not sustain ACK clock, since it only allows | |
| 629 | * one rescue retransmission before a cumulative ACK | |
| 630 | * covers the segement transmitted by rescue | |
| 631 | * retransmission. | |
| 632 | * | |
| 633 | * We try to locate the next unSACKed segment which | |
| 634 | * follows the previously sent rescue segment. If | |
| 635 | * there is no such segment, we loop back to the first | |
| 636 | * unacknowledged segment. | |
| 637 | */ | |
| 638 | ||
| 639 | /* | |
| 640 | * Skip SACKed data, but here we follow | |
| 641 | * the last transmitted rescue segment. | |
| 642 | */ | |
| 643 | torexmt = tp->rexmt_rescue; | |
| 644 | tcp_sack_skip_sacked(scb, &torexmt); | |
| 645 | if (torexmt == tp->snd_max) { | |
| 646 | /* Nothing left to retransmit; restart */ | |
| 647 | torexmt = tp->snd_una; | |
| 648 | } | |
| 649 | } | |
| 1bdfd728 | 650 | *rescue = TRUE; |
| 1bdfd728 SZ |
651 | goto sendunsacked; |
| 652 | } else if (tcp_do_smartsack && lastblock == NULL) { | |
| a098966f SZ |
653 | tcpstat.tcps_sackrescue_try++; |
| 654 | *rescue = TRUE; | |
| 1bdfd728 SZ |
655 | goto sendunsacked; |
| 656 | } | |
| 657 | ||
| 91489f6b JH |
658 | return FALSE; |
| 659 | } | |
| 660 | ||
| 661 | /* | |
| 662 | * Return the next sequence number higher than "*prexmt" that has | |
| 663 | * not been SACKed. | |
| 664 | */ | |
| 665 | void | |
| 666 | tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt) | |
| 667 | { | |
| 668 | struct sackblock *sb; | |
| 669 | ||
| 670 | /* skip SACKed data */ | |
| 671 | if (sack_block_lookup(scb, *prexmt, &sb)) | |
| 672 | *prexmt = sb->sblk_end; | |
| 673 | } | |
| 674 | ||
| 675 | #ifdef later | |
| 676 | void | |
| 677 | tcp_sack_save_scoreboard(struct scoreboard *scb) | |
| 678 | { | |
| 679 | struct scoreboard *scb = &tp->scb; | |
| 680 | ||
| 681 | scb->sackblocks_prev = scb->sackblocks; | |
| 682 | TAILQ_INIT(&scb->sackblocks); | |
| 683 | } | |
| 684 | ||
| 685 | void | |
| 686 | tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una, | |
| 687 | u_int maxseg) | |
| 688 | { | |
| 689 | struct sackblock *sb; | |
| 690 | ||
| 691 | scb->sackblocks = scb->sackblocks_prev; | |
| 692 | scb->nblocks = 0; | |
| 693 | TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) | |
| 694 | ++scb->nblocks; | |
| 695 | tcp_sack_ack_blocks(scb, snd_una); | |
| 696 | scb->lastfound = NULL; | |
| 697 | } | |
| 698 | #endif | |
| 699 | ||
| 700 | #ifdef DEBUG_SACK_HISTORY | |
| 701 | static void | |
| 702 | tcp_sack_dump_history(char *msg, struct tcpcb *tp) | |
| 703 | { | |
| 704 | int i; | |
| 705 | static int ndumped; | |
| 706 | ||
| 707 | /* only need a couple of these to debug most problems */ | |
| 708 | if (++ndumped > 900) | |
| 709 | return; | |
| 710 | ||
| a6ec04bc | 711 | kprintf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory); |
| 91489f6b | 712 | for (i = 0; i < tp->nsackhistory; ++i) |
| a6ec04bc | 713 | kprintf("[%u, %u) ", tp->sackhistory[i].rblk_start, |
| 91489f6b | 714 | tp->sackhistory[i].rblk_end); |
| a6ec04bc | 715 | kprintf("\n"); |
| 91489f6b JH |
716 | } |
| 717 | #else | |
| 718 | static __inline void | |
| 719 | tcp_sack_dump_history(char *msg, struct tcpcb *tp) | |
| 720 | { | |
| 721 | } | |
| 722 | #endif | |
| 723 | ||
| 724 | /* | |
| 725 | * Remove old SACK blocks from the SACK history that have already been ACKed. | |
| 726 | */ | |
| 727 | static void | |
| 728 | tcp_sack_ack_history(struct tcpcb *tp) | |
| 729 | { | |
| 730 | int i, nblocks, openslot; | |
| 731 | ||
| 732 | tcp_sack_dump_history("before tcp_sack_ack_history", tp); | |
| 733 | nblocks = tp->nsackhistory; | |
| 734 | for (i = openslot = 0; i < nblocks; ++i) { | |
| 735 | if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) { | |
| 736 | --tp->nsackhistory; | |
| 737 | continue; | |
| 738 | } | |
| 739 | if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt)) | |
| 740 | tp->sackhistory[i].rblk_start = tp->rcv_nxt; | |
| 741 | if (i == openslot) | |
| 742 | ++openslot; | |
| 743 | else | |
| 744 | tp->sackhistory[openslot++] = tp->sackhistory[i]; | |
| 745 | } | |
| 746 | tcp_sack_dump_history("after tcp_sack_ack_history", tp); | |
| 747 | KASSERT(openslot == tp->nsackhistory, | |
| 748 | ("tcp_sack_ack_history miscounted: %d != %d", | |
| 749 | openslot, tp->nsackhistory)); | |
| 750 | } | |
| 751 | ||
| 752 | /* | |
| 753 | * Add or merge newblock into reported history. | |
| 754 | * Also remove or update SACK blocks that will be acked. | |
| 755 | */ | |
| 756 | static void | |
| 757 | tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end) | |
| 758 | { | |
| 759 | struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS]; | |
| 760 | int i, cindex; | |
| 761 | ||
| 762 | tcp_sack_dump_history("before tcp_sack_update_reported_history", tp); | |
| 763 | /* | |
| 764 | * Six cases: | |
| 765 | * 0) no overlap | |
| 766 | * 1) newblock == oldblock | |
| 767 | * 2) oldblock contains newblock | |
| 768 | * 3) newblock contains oldblock | |
| 769 | * 4) tail of oldblock overlaps or abuts start of newblock | |
| 770 | * 5) tail of newblock overlaps or abuts head of oldblock | |
| 771 | */ | |
| 772 | for (i = cindex = 0; i < tp->nsackhistory; ++i) { | |
| 773 | struct raw_sackblock *oldblock = &tp->sackhistory[i]; | |
| 774 | tcp_seq old_start = oldblock->rblk_start; | |
| 775 | tcp_seq old_end = oldblock->rblk_end; | |
| 776 | ||
| 777 | if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) { | |
| 778 | /* Case 0: no overlap. Copy old block. */ | |
| 779 | copy[cindex++] = *oldblock; | |
| 780 | continue; | |
| 781 | } | |
| 782 | ||
| 783 | if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) { | |
| 784 | /* Cases 1 & 2. Move block to front of history. */ | |
| 785 | int j; | |
| 786 | ||
| 787 | start = old_start; | |
| 788 | end = old_end; | |
| 789 | /* no need to check rest of blocks */ | |
| 790 | for (j = i + 1; j < tp->nsackhistory; ++j) | |
| 791 | copy[cindex++] = tp->sackhistory[j]; | |
| 792 | break; | |
| 793 | } | |
| 794 | ||
| 795 | if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) { | |
| 796 | /* Case 4: extend start of new block. */ | |
| 797 | start = old_start; | |
| 798 | } else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) { | |
| 799 | /* Case 5: extend end of new block */ | |
| 800 | end = old_end; | |
| 801 | } else { | |
| 802 | /* Case 3. Delete old block by not copying it. */ | |
| 803 | KASSERT(SEQ_LEQ(start, old_start) && | |
| 804 | SEQ_GEQ(end, old_end), | |
| 805 | ("bad logic: old [%u, %u), new [%u, %u)", | |
| 806 | old_start, old_end, start, end)); | |
| 807 | } | |
| 808 | } | |
| 809 | ||
| 810 | /* insert new block */ | |
| 811 | tp->sackhistory[0].rblk_start = start; | |
| 812 | tp->sackhistory[0].rblk_end = end; | |
| 813 | cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1); | |
| 814 | for (i = 0; i < cindex; ++i) | |
| 815 | tp->sackhistory[i + 1] = copy[i]; | |
| 816 | tp->nsackhistory = cindex + 1; | |
| 817 | tcp_sack_dump_history("after tcp_sack_update_reported_history", tp); | |
| 818 | } | |
| 819 | ||
| 820 | /* | |
| 821 | * Fill in SACK report to return to data sender. | |
| 822 | */ | |
| 823 | void | |
| 824 | tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen) | |
| 825 | { | |
| 826 | u_int optlen = *plen; | |
| 827 | uint32_t *lp = (uint32_t *)(opt + optlen); | |
| 828 | uint32_t *olp; | |
| 829 | tcp_seq hstart = tp->rcv_nxt, hend; | |
| 830 | int nblocks; | |
| 831 | ||
| 832 | KASSERT(TCP_MAXOLEN - optlen >= | |
| 833 | TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK, | |
| 834 | ("no room for SACK header and one block: optlen %d", optlen)); | |
| 835 | ||
| 02cc2f35 SZ |
836 | if (tp->t_flags & TF_DUPSEG) |
| 837 | tcpstat.tcps_snddsackopt++; | |
| 838 | else | |
| 839 | tcpstat.tcps_sndsackopt++; | |
| 840 | ||
| 91489f6b JH |
841 | olp = lp++; |
| 842 | optlen += TCPOLEN_SACK_ALIGNED; | |
| 843 | ||
| 844 | tcp_sack_ack_history(tp); | |
| 845 | if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) { | |
| 846 | *lp++ = htonl(tp->reportblk.rblk_start); | |
| 847 | *lp++ = htonl(tp->reportblk.rblk_end); | |
| 848 | optlen += TCPOLEN_SACK_BLOCK; | |
| 849 | hstart = tp->reportblk.rblk_start; | |
| 850 | hend = tp->reportblk.rblk_end; | |
| 851 | if (tp->t_flags & TF_ENCLOSESEG) { | |
| 852 | KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK, | |
| 853 | ("no room for enclosing SACK block: oplen %d", | |
| 854 | optlen)); | |
| 855 | *lp++ = htonl(tp->encloseblk.rblk_start); | |
| 856 | *lp++ = htonl(tp->encloseblk.rblk_end); | |
| 857 | optlen += TCPOLEN_SACK_BLOCK; | |
| 858 | hstart = tp->encloseblk.rblk_start; | |
| 859 | hend = tp->encloseblk.rblk_end; | |
| 860 | } | |
| 861 | if (SEQ_GT(hstart, tp->rcv_nxt)) | |
| 862 | tcp_sack_update_reported_history(tp, hstart, hend); | |
| 863 | } | |
| 864 | if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) { | |
| 865 | /* Fill in from left! Walk re-assembly queue. */ | |
| 866 | struct tseg_qent *q; | |
| 867 | ||
| 868 | q = LIST_FIRST(&tp->t_segq); | |
| 869 | while (q != NULL && | |
| 870 | TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) { | |
| 871 | *lp++ = htonl(q->tqe_th->th_seq); | |
| 3a5d999b SZ |
872 | *lp++ = htonl(TCP_SACK_BLKEND( |
| 873 | q->tqe_th->th_seq + q->tqe_len, | |
| 874 | q->tqe_th->th_flags)); | |
| 91489f6b JH |
875 | optlen += TCPOLEN_SACK_BLOCK; |
| 876 | q = LIST_NEXT(q, tqe_q); | |
| 877 | } | |
| 878 | } else { | |
| 879 | int n = 0; | |
| 880 | ||
| 881 | /* Fill in SACK blocks from right side. */ | |
| 882 | while (n < tp->nsackhistory && | |
| 883 | TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) { | |
| 884 | if (tp->sackhistory[n].rblk_start != hstart) { | |
| 885 | *lp++ = htonl(tp->sackhistory[n].rblk_start); | |
| 886 | *lp++ = htonl(tp->sackhistory[n].rblk_end); | |
| 887 | optlen += TCPOLEN_SACK_BLOCK; | |
| 888 | } | |
| 889 | ++n; | |
| 890 | } | |
| 891 | } | |
| 892 | tp->reportblk.rblk_start = tp->reportblk.rblk_end; | |
| 893 | tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT); | |
| 894 | nblocks = (lp - olp - 1) / 2; | |
| 895 | *olp = htonl(TCPOPT_SACK_ALIGNED | | |
| 896 | (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK)); | |
| 897 | *plen = optlen; | |
| 898 | } |