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