2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/tcp/tcp_sack.h>
19 scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
21 sack_scoreboard_hole_t *next, *prev;
23 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
25 next = pool_elt_at_index (sb->holes, hole->next);
26 next->prev = hole->prev;
30 sb->tail = hole->prev;
33 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
35 prev = pool_elt_at_index (sb->holes, hole->prev);
36 prev->next = hole->next;
40 sb->head = hole->next;
43 if (scoreboard_hole_index (sb, hole) == sb->cur_rxt_hole)
44 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
46 /* Poison the entry */
48 clib_memset (hole, 0xfe, sizeof (*hole));
50 pool_put (sb->holes, hole);
53 static sack_scoreboard_hole_t *
54 scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
57 sack_scoreboard_hole_t *hole, *next, *prev;
60 pool_get (sb->holes, hole);
61 clib_memset (hole, 0, sizeof (*hole));
65 hole_index = scoreboard_hole_index (sb, hole);
67 prev = scoreboard_get_hole (sb, prev_index);
70 hole->prev = prev_index;
71 hole->next = prev->next;
73 if ((next = scoreboard_next_hole (sb, hole)))
74 next->prev = hole_index;
76 sb->tail = hole_index;
78 prev->next = hole_index;
82 sb->head = hole_index;
83 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
84 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
91 scoreboard_update_sacked_rxt (sack_scoreboard_t * sb, u32 start, u32 end,
94 if (!has_rxt || seq_geq (start, sb->high_rxt))
98 seq_lt (end, sb->high_rxt) ? (end - start) : (sb->high_rxt - start);
102 scoreboard_update_bytes (sack_scoreboard_t * sb, u32 ack, u32 snd_mss)
104 sack_scoreboard_hole_t *left, *right;
105 u32 sacked = 0, blks = 0, old_sacked;
107 old_sacked = sb->sacked_bytes;
109 sb->last_lost_bytes = 0;
111 sb->sacked_bytes = 0;
113 right = scoreboard_last_hole (sb);
116 sb->sacked_bytes = sb->high_sacked - ack;
117 sb->last_sacked_bytes = sb->sacked_bytes
118 - (old_sacked - sb->last_bytes_delivered);
122 if (seq_gt (sb->high_sacked, right->end))
124 sacked = sb->high_sacked - right->end;
128 while (sacked < (TCP_DUPACK_THRESHOLD - 1) * snd_mss
129 && blks < TCP_DUPACK_THRESHOLD)
132 sb->lost_bytes += scoreboard_hole_bytes (right);
134 left = scoreboard_prev_hole (sb, right);
137 ASSERT (right->start == ack || sb->is_reneging);
138 sacked += right->start - ack;
143 sacked += right->start - left->end;
148 /* right is first lost */
151 sb->lost_bytes += scoreboard_hole_bytes (right);
152 sb->last_lost_bytes += right->is_lost ? 0 : (right->end - right->start);
154 left = scoreboard_prev_hole (sb, right);
157 ASSERT (right->start == ack || sb->is_reneging);
158 sacked += right->start - ack;
161 sacked += right->start - left->end;
165 sb->sacked_bytes = sacked;
166 sb->last_sacked_bytes = sacked - (old_sacked - sb->last_bytes_delivered);
170 * Figure out the next hole to retransmit
172 * Follows logic proposed in RFC6675 Sec. 4, NextSeg()
174 sack_scoreboard_hole_t *
175 scoreboard_next_rxt_hole (sack_scoreboard_t * sb,
176 sack_scoreboard_hole_t * start,
177 u8 have_unsent, u8 * can_rescue, u8 * snd_limited)
179 sack_scoreboard_hole_t *hole = 0;
181 hole = start ? start : scoreboard_first_hole (sb);
182 while (hole && seq_leq (hole->end, sb->high_rxt) && hole->is_lost)
183 hole = scoreboard_next_hole (sb, hole);
185 /* Nothing, return */
188 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
192 /* Rule (1): if higher than rxt, less than high_sacked and lost */
193 if (hole->is_lost && seq_lt (hole->start, sb->high_sacked))
195 sb->cur_rxt_hole = scoreboard_hole_index (sb, hole);
199 /* Rule (2): available unsent data */
202 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
205 /* Rule (3): if hole not lost */
206 else if (seq_lt (hole->start, sb->high_sacked))
208 /* And we didn't already retransmit it */
209 if (seq_leq (hole->end, sb->high_rxt))
211 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
215 sb->cur_rxt_hole = scoreboard_hole_index (sb, hole);
217 /* Rule (4): if hole beyond high_sacked */
220 ASSERT (seq_geq (hole->start, sb->high_sacked));
223 /* HighRxt MUST NOT be updated */
228 if (hole && seq_lt (sb->high_rxt, hole->start))
229 sb->high_rxt = hole->start;
235 scoreboard_init_rxt (sack_scoreboard_t * sb, u32 snd_una)
237 sack_scoreboard_hole_t *hole;
238 hole = scoreboard_first_hole (sb);
241 snd_una = seq_gt (snd_una, hole->start) ? snd_una : hole->start;
242 sb->cur_rxt_hole = sb->head;
244 sb->high_rxt = snd_una;
245 sb->rescue_rxt = snd_una - 1;
249 scoreboard_init (sack_scoreboard_t * sb)
251 sb->head = TCP_INVALID_SACK_HOLE_INDEX;
252 sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
253 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
257 scoreboard_clear (sack_scoreboard_t * sb)
259 sack_scoreboard_hole_t *hole;
260 while ((hole = scoreboard_first_hole (sb)))
262 scoreboard_remove_hole (sb, hole);
264 ASSERT (sb->head == sb->tail && sb->head == TCP_INVALID_SACK_HOLE_INDEX);
265 ASSERT (pool_elts (sb->holes) == 0);
266 sb->sacked_bytes = 0;
267 sb->last_sacked_bytes = 0;
268 sb->last_bytes_delivered = 0;
270 sb->last_lost_bytes = 0;
271 sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
276 scoreboard_clear_reneging (sack_scoreboard_t * sb, u32 start, u32 end)
278 sack_scoreboard_hole_t *last_hole;
280 clib_warning ("sack reneging");
282 scoreboard_clear (sb);
283 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
285 last_hole->is_lost = 1;
286 sb->tail = scoreboard_hole_index (sb, last_hole);
287 sb->high_sacked = start;
288 scoreboard_init_rxt (sb, start);
292 * Test that scoreboard is sane after recovery
294 * Returns 1 if scoreboard is empty or if first hole beyond
298 tcp_scoreboard_is_sane_post_recovery (tcp_connection_t * tc)
300 sack_scoreboard_hole_t *hole;
301 hole = scoreboard_first_hole (&tc->sack_sb);
302 return (!hole || (seq_geq (hole->start, tc->snd_una)
303 && seq_lt (hole->end, tc->snd_nxt)));
307 tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
309 sack_scoreboard_hole_t *hole, *next_hole;
310 sack_scoreboard_t *sb = &tc->sack_sb;
311 sack_block_t *blk, *rcv_sacks;
312 u32 blk_index = 0, i, j;
315 sb->last_sacked_bytes = 0;
316 sb->last_bytes_delivered = 0;
319 if (!tcp_opts_sack (&tc->rcv_opts) && !sb->sacked_bytes
320 && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
323 has_rxt = tcp_in_cong_recovery (tc);
325 /* Remove invalid blocks */
326 blk = tc->rcv_opts.sacks;
327 while (blk < vec_end (tc->rcv_opts.sacks))
329 if (seq_lt (blk->start, blk->end)
330 && seq_gt (blk->start, tc->snd_una)
331 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
336 vec_del1 (tc->rcv_opts.sacks, blk - tc->rcv_opts.sacks);
339 /* Add block for cumulative ack */
340 if (seq_gt (ack, tc->snd_una))
342 vec_add2 (tc->rcv_opts.sacks, blk, 1);
343 blk->start = tc->snd_una;
347 if (vec_len (tc->rcv_opts.sacks) == 0)
350 tcp_scoreboard_trace_add (tc, ack);
352 /* Make sure blocks are ordered */
353 rcv_sacks = tc->rcv_opts.sacks;
354 for (i = 0; i < vec_len (rcv_sacks); i++)
355 for (j = i + 1; j < vec_len (rcv_sacks); j++)
356 if (seq_lt (rcv_sacks[j].start, rcv_sacks[i].start))
358 sack_block_t tmp = rcv_sacks[i];
359 rcv_sacks[i] = rcv_sacks[j];
363 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
365 /* Handle reneging as a special case */
366 if (PREDICT_FALSE (sb->is_reneging))
368 /* No holes, only sacked bytes */
369 if (seq_leq (tc->snd_nxt, sb->high_sacked))
371 /* No progress made so return */
372 if (seq_leq (ack, tc->snd_una))
375 /* Update sacked bytes delivered and return */
376 sb->last_bytes_delivered = ack - tc->snd_una;
377 sb->sacked_bytes -= sb->last_bytes_delivered;
378 sb->is_reneging = seq_lt (ack, sb->high_sacked);
382 /* New hole above high sacked. Add it and process normally */
383 hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
384 sb->high_sacked, tc->snd_nxt);
385 sb->tail = scoreboard_hole_index (sb, hole);
387 /* Not reneging and no holes. Insert the first that covers all
388 * outstanding bytes */
391 hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
392 tc->snd_una, tc->snd_nxt);
393 sb->tail = scoreboard_hole_index (sb, hole);
395 sb->high_sacked = rcv_sacks[vec_len (rcv_sacks) - 1].end;
399 /* If we have holes but snd_nxt is beyond the last hole, update
400 * last hole end or add new hole after high sacked */
401 hole = scoreboard_last_hole (sb);
402 if (seq_gt (tc->snd_nxt, hole->end))
404 if (seq_geq (hole->start, sb->high_sacked))
406 hole->end = tc->snd_nxt;
408 /* New hole after high sacked block */
409 else if (seq_lt (sb->high_sacked, tc->snd_nxt))
411 scoreboard_insert_hole (sb, sb->tail, sb->high_sacked,
416 /* Keep track of max byte sacked for when the last hole
418 sb->high_sacked = seq_max (rcv_sacks[vec_len (rcv_sacks) - 1].end,
422 /* Walk the holes with the SACK blocks */
423 hole = pool_elt_at_index (sb->holes, sb->head);
425 if (PREDICT_FALSE (sb->is_reneging))
427 sb->last_bytes_delivered += clib_min (hole->start - tc->snd_una,
429 sb->is_reneging = seq_lt (ack, hole->start);
432 while (hole && blk_index < vec_len (rcv_sacks))
434 blk = &rcv_sacks[blk_index];
435 if (seq_leq (blk->start, hole->start))
437 /* Block covers hole. Remove hole */
438 if (seq_geq (blk->end, hole->end))
440 next_hole = scoreboard_next_hole (sb, hole);
442 /* If covered by ack, compute delivered bytes */
445 u32 sacked = next_hole ? next_hole->start : sb->high_sacked;
446 if (PREDICT_FALSE (seq_lt (ack, sacked)))
448 sb->last_bytes_delivered += ack - hole->end;
453 sb->last_bytes_delivered += sacked - hole->end;
457 scoreboard_update_sacked_rxt (sb, hole->start, hole->end,
459 scoreboard_remove_hole (sb, hole);
462 /* Partial 'head' overlap */
465 if (seq_gt (blk->end, hole->start))
467 scoreboard_update_sacked_rxt (sb, hole->start, blk->end,
469 hole->start = blk->end;
476 /* Hole must be split */
477 if (seq_lt (blk->end, hole->end))
479 u32 hole_index = scoreboard_hole_index (sb, hole);
480 next_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
482 /* Pool might've moved */
483 hole = scoreboard_get_hole (sb, hole_index);
484 hole->end = blk->start;
486 scoreboard_update_sacked_rxt (sb, blk->start, blk->end,
490 ASSERT (hole->next == scoreboard_hole_index (sb, next_hole));
492 else if (seq_lt (blk->start, hole->end))
494 scoreboard_update_sacked_rxt (sb, blk->start, hole->end,
496 hole->end = blk->start;
498 hole = scoreboard_next_hole (sb, hole);
502 scoreboard_update_bytes (sb, ack, tc->snd_mss);
504 ASSERT (sb->last_sacked_bytes <= sb->sacked_bytes || tcp_in_recovery (tc));
505 ASSERT (sb->sacked_bytes == 0 || tcp_in_recovery (tc)
506 || sb->sacked_bytes <= tc->snd_nxt - seq_max (tc->snd_una, ack));
507 ASSERT (sb->last_sacked_bytes + sb->lost_bytes <= tc->snd_nxt
508 - seq_max (tc->snd_una, ack) || tcp_in_recovery (tc));
509 ASSERT (sb->head == TCP_INVALID_SACK_HOLE_INDEX || tcp_in_recovery (tc)
510 || sb->is_reneging || sb->holes[sb->head].start == ack);
511 ASSERT (sb->last_lost_bytes <= sb->lost_bytes);
512 ASSERT ((ack - tc->snd_una) + sb->last_sacked_bytes
513 - sb->last_bytes_delivered >= sb->rxt_sacked);
514 ASSERT ((ack - tc->snd_una) >= tc->sack_sb.last_bytes_delivered
515 || (tc->flags & TCP_CONN_FINSNT));
517 TCP_EVT (TCP_EVT_CC_SCOREBOARD, tc);
521 tcp_sack_vector_is_sane (sack_block_t * sacks)
524 for (i = 1; i < vec_len (sacks); i++)
526 if (sacks[i - 1].end == sacks[i].start)
533 * Build SACK list as per RFC2018.
535 * Makes sure the first block contains the segment that generated the current
536 * ACK and the following ones are the ones most recently reported in SACK
539 * @param tc TCP connection for which the SACK list is updated
540 * @param start Start sequence number of the newest SACK block
541 * @param end End sequence of the newest SACK block
544 tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
546 sack_block_t *new_list = tc->snd_sacks_fl, *block = 0;
549 /* If the first segment is ooo add it to the list. Last write might've moved
550 * rcv_nxt over the first segment. */
551 if (seq_lt (tc->rcv_nxt, start))
553 vec_add2 (new_list, block, 1);
554 block->start = start;
558 /* Find the blocks still worth keeping. */
559 for (i = 0; i < vec_len (tc->snd_sacks); i++)
561 /* Discard if rcv_nxt advanced beyond current block */
562 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
565 /* Merge or drop if segment overlapped by the new segment */
566 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
567 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
569 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
570 new_list[0].start = tc->snd_sacks[i].start;
571 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
572 new_list[0].end = tc->snd_sacks[i].end;
576 /* Save to new SACK list if we have space. */
577 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
578 vec_add1 (new_list, tc->snd_sacks[i]);
581 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
583 /* Replace old vector with new one */
584 vec_reset_length (tc->snd_sacks);
585 tc->snd_sacks_fl = tc->snd_sacks;
586 tc->snd_sacks = new_list;
588 /* Segments should not 'touch' */
589 ASSERT (tcp_sack_vector_is_sane (tc->snd_sacks));
593 tcp_sack_list_bytes (tcp_connection_t * tc)
596 for (i = 0; i < vec_len (tc->snd_sacks); i++)
597 bytes += tc->snd_sacks[i].end - tc->snd_sacks[i].start;
602 * fd.io coding-style-patch-verification: ON
605 * eval: (c-set-style "gnu")