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 scoreboard_clear (sb);
281 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
283 last_hole->is_lost = 1;
284 sb->tail = scoreboard_hole_index (sb, last_hole);
285 sb->high_sacked = start;
286 scoreboard_init_rxt (sb, start);
290 * Test that scoreboard is sane after recovery
292 * Returns 1 if scoreboard is empty or if first hole beyond
296 tcp_scoreboard_is_sane_post_recovery (tcp_connection_t * tc)
298 sack_scoreboard_hole_t *hole;
299 hole = scoreboard_first_hole (&tc->sack_sb);
300 return (!hole || (seq_geq (hole->start, tc->snd_una)
301 && seq_lt (hole->end, tc->snd_nxt)));
305 tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
307 sack_scoreboard_hole_t *hole, *next_hole;
308 sack_scoreboard_t *sb = &tc->sack_sb;
309 sack_block_t *blk, *rcv_sacks;
310 u32 blk_index = 0, i, j;
313 sb->last_sacked_bytes = 0;
314 sb->last_bytes_delivered = 0;
317 if (!tcp_opts_sack (&tc->rcv_opts) && !sb->sacked_bytes
318 && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
321 has_rxt = tcp_in_cong_recovery (tc);
323 /* Remove invalid blocks */
324 blk = tc->rcv_opts.sacks;
325 while (blk < vec_end (tc->rcv_opts.sacks))
327 if (seq_lt (blk->start, blk->end)
328 && seq_gt (blk->start, tc->snd_una)
329 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
334 vec_del1 (tc->rcv_opts.sacks, blk - tc->rcv_opts.sacks);
337 /* Add block for cumulative ack */
338 if (seq_gt (ack, tc->snd_una))
340 vec_add2 (tc->rcv_opts.sacks, blk, 1);
341 blk->start = tc->snd_una;
345 if (vec_len (tc->rcv_opts.sacks) == 0)
348 tcp_scoreboard_trace_add (tc, ack);
350 /* Make sure blocks are ordered */
351 rcv_sacks = tc->rcv_opts.sacks;
352 for (i = 0; i < vec_len (rcv_sacks); i++)
353 for (j = i + 1; j < vec_len (rcv_sacks); j++)
354 if (seq_lt (rcv_sacks[j].start, rcv_sacks[i].start))
356 sack_block_t tmp = rcv_sacks[i];
357 rcv_sacks[i] = rcv_sacks[j];
361 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
363 /* Handle reneging as a special case */
364 if (PREDICT_FALSE (sb->is_reneging))
366 /* No holes, only sacked bytes */
367 if (seq_leq (tc->snd_nxt, sb->high_sacked))
369 /* No progress made so return */
370 if (seq_leq (ack, tc->snd_una))
373 /* Update sacked bytes delivered and return */
374 sb->last_bytes_delivered = ack - tc->snd_una;
375 sb->sacked_bytes -= sb->last_bytes_delivered;
376 sb->is_reneging = seq_lt (ack, sb->high_sacked);
380 /* New hole above high sacked. Add it and process normally */
381 hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
382 sb->high_sacked, tc->snd_nxt);
383 sb->tail = scoreboard_hole_index (sb, hole);
385 /* Not reneging and no holes. Insert the first that covers all
386 * outstanding bytes */
389 hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
390 tc->snd_una, tc->snd_nxt);
391 sb->tail = scoreboard_hole_index (sb, hole);
393 sb->high_sacked = rcv_sacks[vec_len (rcv_sacks) - 1].end;
397 /* If we have holes but snd_nxt is beyond the last hole, update
398 * last hole end or add new hole after high sacked */
399 hole = scoreboard_last_hole (sb);
400 if (seq_gt (tc->snd_nxt, hole->end))
402 if (seq_geq (hole->start, sb->high_sacked))
404 hole->end = tc->snd_nxt;
406 /* New hole after high sacked block */
407 else if (seq_lt (sb->high_sacked, tc->snd_nxt))
409 scoreboard_insert_hole (sb, sb->tail, sb->high_sacked,
414 /* Keep track of max byte sacked for when the last hole
416 sb->high_sacked = seq_max (rcv_sacks[vec_len (rcv_sacks) - 1].end,
420 /* Walk the holes with the SACK blocks */
421 hole = pool_elt_at_index (sb->holes, sb->head);
423 if (PREDICT_FALSE (sb->is_reneging))
425 sb->last_bytes_delivered += clib_min (hole->start - tc->snd_una,
427 sb->is_reneging = seq_lt (ack, hole->start);
430 while (hole && blk_index < vec_len (rcv_sacks))
432 blk = &rcv_sacks[blk_index];
433 if (seq_leq (blk->start, hole->start))
435 /* Block covers hole. Remove hole */
436 if (seq_geq (blk->end, hole->end))
438 next_hole = scoreboard_next_hole (sb, hole);
440 /* If covered by ack, compute delivered bytes */
443 u32 sacked = next_hole ? next_hole->start : sb->high_sacked;
444 if (PREDICT_FALSE (seq_lt (ack, sacked)))
446 sb->last_bytes_delivered += ack - hole->end;
451 sb->last_bytes_delivered += sacked - hole->end;
455 scoreboard_update_sacked_rxt (sb, hole->start, hole->end,
457 scoreboard_remove_hole (sb, hole);
460 /* Partial 'head' overlap */
463 if (seq_gt (blk->end, hole->start))
465 scoreboard_update_sacked_rxt (sb, hole->start, blk->end,
467 hole->start = blk->end;
474 /* Hole must be split */
475 if (seq_lt (blk->end, hole->end))
477 u32 hole_index = scoreboard_hole_index (sb, hole);
478 next_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
480 /* Pool might've moved */
481 hole = scoreboard_get_hole (sb, hole_index);
482 hole->end = blk->start;
484 scoreboard_update_sacked_rxt (sb, blk->start, blk->end,
488 ASSERT (hole->next == scoreboard_hole_index (sb, next_hole));
490 else if (seq_lt (blk->start, hole->end))
492 scoreboard_update_sacked_rxt (sb, blk->start, hole->end,
494 hole->end = blk->start;
496 hole = scoreboard_next_hole (sb, hole);
500 scoreboard_update_bytes (sb, ack, tc->snd_mss);
502 ASSERT (sb->last_sacked_bytes <= sb->sacked_bytes || tcp_in_recovery (tc));
503 ASSERT (sb->sacked_bytes == 0 || tcp_in_recovery (tc)
504 || sb->sacked_bytes <= tc->snd_nxt - seq_max (tc->snd_una, ack));
505 ASSERT (sb->last_sacked_bytes + sb->lost_bytes <= tc->snd_nxt
506 - seq_max (tc->snd_una, ack) || tcp_in_recovery (tc));
507 ASSERT (sb->head == TCP_INVALID_SACK_HOLE_INDEX || tcp_in_recovery (tc)
508 || sb->is_reneging || sb->holes[sb->head].start == ack);
509 ASSERT (sb->last_lost_bytes <= sb->lost_bytes);
510 ASSERT ((ack - tc->snd_una) + sb->last_sacked_bytes
511 - sb->last_bytes_delivered >= sb->rxt_sacked);
512 ASSERT ((ack - tc->snd_una) >= tc->sack_sb.last_bytes_delivered
513 || (tc->flags & TCP_CONN_FINSNT));
515 TCP_EVT (TCP_EVT_CC_SCOREBOARD, tc);
519 tcp_sack_vector_is_sane (sack_block_t * sacks)
522 for (i = 1; i < vec_len (sacks); i++)
524 if (sacks[i - 1].end == sacks[i].start)
531 * Build SACK list as per RFC2018.
533 * Makes sure the first block contains the segment that generated the current
534 * ACK and the following ones are the ones most recently reported in SACK
537 * @param tc TCP connection for which the SACK list is updated
538 * @param start Start sequence number of the newest SACK block
539 * @param end End sequence of the newest SACK block
542 tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
544 sack_block_t *new_list = tc->snd_sacks_fl, *block = 0;
547 /* If the first segment is ooo add it to the list. Last write might've moved
548 * rcv_nxt over the first segment. */
549 if (seq_lt (tc->rcv_nxt, start))
551 vec_add2 (new_list, block, 1);
552 block->start = start;
556 /* Find the blocks still worth keeping. */
557 for (i = 0; i < vec_len (tc->snd_sacks); i++)
559 /* Discard if rcv_nxt advanced beyond current block */
560 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
563 /* Merge or drop if segment overlapped by the new segment */
564 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
565 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
567 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
568 new_list[0].start = tc->snd_sacks[i].start;
569 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
570 new_list[0].end = tc->snd_sacks[i].end;
574 /* Save to new SACK list if we have space. */
575 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
576 vec_add1 (new_list, tc->snd_sacks[i]);
579 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
581 /* Replace old vector with new one */
582 vec_reset_length (tc->snd_sacks);
583 tc->snd_sacks_fl = tc->snd_sacks;
584 tc->snd_sacks = new_list;
586 /* Segments should not 'touch' */
587 ASSERT (tcp_sack_vector_is_sane (tc->snd_sacks));
591 tcp_sack_list_bytes (tcp_connection_t * tc)
594 for (i = 0; i < vec_len (tc->snd_sacks); i++)
595 bytes += tc->snd_sacks[i].end - tc->snd_sacks[i].start;
600 * fd.io coding-style-patch-verification: ON
603 * eval: (c-set-style "gnu")