2 * Copyright (c) 2017-2019 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.
15 #include <vnet/tcp/tcp.h>
16 #include <vnet/tcp/tcp_inlines.h>
18 #define TCP_TEST_I(_cond, _comment, _args...) \
20 int _evald = (_cond); \
22 fformat(stderr, "FAIL:%d: " _comment "\n", \
25 fformat(stderr, "PASS:%d: " _comment "\n", \
31 #define TCP_TEST(_cond, _comment, _args...) \
33 if (!TCP_TEST_I(_cond, _comment, ##_args)) { \
39 scoreboard_trace_elt_t sb_trace[] = {};
43 tcp_test_scoreboard_replay (vlib_main_t * vm, unformat_input_t * input)
46 tcp_connection_t _tc, *tc = &_tc;
49 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
51 if (unformat (input, "detail"))
55 clib_error_t *e = clib_error_return
56 (0, "unknown input `%U'", format_unformat_error, input);
57 clib_error_report (e);
62 #if TCP_SCOREBOARD_TRACE
63 tc->sack_sb.trace = sb_trace;
65 s = tcp_scoreboard_replay (s, tc, verbose);
66 vlib_cli_output (vm, "%v", s);
71 tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
73 tcp_connection_t _tc, *tc = &_tc;
74 sack_scoreboard_t *sb = &tc->sack_sb;
75 sack_block_t *sacks = 0, block;
76 sack_scoreboard_hole_t *hole;
79 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
81 if (unformat (input, "verbose"))
83 else if (unformat (input, "replay"))
84 return tcp_test_scoreboard_replay (vm, input);
87 clib_memset (tc, 0, sizeof (*tc));
89 tc->flags |= TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY;
92 tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
94 scoreboard_init (&tc->sack_sb);
96 for (i = 0; i < 1000 / 100; i++)
98 block.start = i * 100;
99 block.end = (i + 1) * 100;
100 vec_add1 (sacks, block);
107 for (i = 0; i < 1000 / 200; i++)
109 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
111 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
112 tcp_rcv_sacks (tc, 0);
115 vlib_cli_output (vm, "sb after even blocks (mss %u):\n%U",
116 tc->snd_mss, format_tcp_scoreboard, sb, tc);
118 TCP_TEST ((pool_elts (sb->holes) == 5),
119 "scoreboard has %d elements", pool_elts (sb->holes));
121 /* First SACK block should be rejected */
122 hole = scoreboard_first_hole (sb);
123 TCP_TEST ((hole->start == 0 && hole->end == 200),
124 "first hole start %u end %u", hole->start, hole->end);
125 hole = scoreboard_last_hole (sb);
126 TCP_TEST ((hole->start == 900 && hole->end == 1000),
127 "last hole start %u end %u", hole->start, hole->end);
128 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
129 TCP_TEST ((!sb->is_reneging), "is not reneging");
130 TCP_TEST ((sb->last_sacked_bytes == 400),
131 "last sacked bytes %d", sb->last_sacked_bytes);
132 TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
133 TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
136 * Inject odd blocks except the last
140 vec_reset_length (tc->rcv_opts.sacks);
141 for (i = 0; i < 800 / 200; i++)
143 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
145 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
146 tcp_rcv_sacks (tc, 0);
149 vlib_cli_output (vm, "\nsb after odd blocks:\n%U", format_tcp_scoreboard,
152 hole = scoreboard_first_hole (sb);
153 TCP_TEST ((pool_elts (sb->holes) == 2),
154 "scoreboard has %d holes", pool_elts (sb->holes));
155 TCP_TEST ((hole->start == 0 && hole->end == 100),
156 "first hole start %u end %u", hole->start, hole->end);
157 TCP_TEST ((sb->sacked_bytes == 800), "sacked bytes %d", sb->sacked_bytes);
158 TCP_TEST ((!sb->is_reneging), "is not reneging");
159 TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
160 TCP_TEST ((sb->last_sacked_bytes == 400),
161 "last sacked bytes %d", sb->last_sacked_bytes);
162 TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
165 * Ack until byte 100 - this is reneging because we should ack until 900
167 tcp_rcv_sacks (tc, 100);
169 vlib_cli_output (vm, "\nack until byte 100:\n%U", format_tcp_scoreboard,
172 TCP_TEST ((pool_elts (sb->holes) == 1), "scoreboard has %d elements",
173 pool_elts (sb->holes));
174 TCP_TEST ((sb->is_reneging), "is reneging");
177 * Make sure we accept duplicate acks while reneging.
184 vec_add1 (tc->rcv_opts.sacks, block);
186 tcp_rcv_sacks (tc, 100);
187 TCP_TEST ((pool_elts (sb->holes) == 1), "scoreboard has %d elements",
188 pool_elts (sb->holes));
189 TCP_TEST ((sb->is_reneging), "is reneging");
190 TCP_TEST ((sb->last_sacked_bytes == 50), "last sacked bytes %d",
191 sb->last_sacked_bytes);
192 TCP_TEST ((sb->rxt_sacked == 50), "last rxt sacked bytes %d",
198 tcp_rcv_sacks (tc, 950);
199 TCP_TEST ((sb->high_sacked == 950), "max sacked byte %u", sb->high_sacked);
200 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
201 TCP_TEST ((sb->last_sacked_bytes == 0),
202 "last sacked bytes %d", sb->last_sacked_bytes);
203 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
204 TCP_TEST ((!sb->is_reneging), "is not reneging");
207 * Sack [960 970] [980 990]
212 vec_reset_length (tc->rcv_opts.sacks);
215 vec_add1 (tc->rcv_opts.sacks, block);
219 vec_add1 (tc->rcv_opts.sacks, block);
221 tcp_rcv_sacks (tc, 950);
222 TCP_TEST ((sb->high_sacked == 990), "max sacked byte %u", sb->high_sacked);
223 TCP_TEST ((sb->sacked_bytes == 20), "sacked bytes %d", sb->sacked_bytes);
224 TCP_TEST ((sb->last_sacked_bytes == 20),
225 "last sacked bytes %d", sb->last_sacked_bytes);
226 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
227 TCP_TEST ((!sb->is_reneging), "is not reneging");
228 TCP_TEST ((sb->rxt_sacked == 15), "last rxt sacked bytes %d",
232 * Ack up to 960 (reneging) + [961 971]
234 tc->rcv_opts.sacks[0].start = 961;
235 tc->rcv_opts.sacks[0].end = 971;
237 tcp_rcv_sacks (tc, 960);
239 TCP_TEST ((sb->is_reneging), "is reneging");
240 TCP_TEST ((sb->sacked_bytes == 21), "sacked bytes %d", sb->sacked_bytes);
241 TCP_TEST ((sb->last_sacked_bytes == 1),
242 "last sacked bytes %d", sb->last_sacked_bytes);
243 TCP_TEST ((sb->rxt_sacked == 11), "last rxt sacked bytes %d",
245 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
246 sb->last_bytes_delivered);
249 * Ack up to 960 (reneging) + [961 990]
252 tc->rcv_opts.sacks[0].start = 961;
253 tc->rcv_opts.sacks[0].end = 990;
255 tcp_rcv_sacks (tc, 960);
257 TCP_TEST ((sb->is_reneging), "is reneging");
258 TCP_TEST ((sb->sacked_bytes == 30), "sacked bytes %d", sb->sacked_bytes);
259 TCP_TEST ((sb->last_sacked_bytes == 9),
260 "last sacked bytes %d", sb->last_sacked_bytes);
261 TCP_TEST ((sb->rxt_sacked == 9), "last rxt sacked bytes %d",
265 * Sack remaining bytes [990 1000]
267 tc->rcv_opts.sacks[0].start = 990;
268 tc->rcv_opts.sacks[0].end = 1000;
270 tcp_rcv_sacks (tc, 960);
272 TCP_TEST ((sb->is_reneging), "is reneging");
273 TCP_TEST ((sb->sacked_bytes == 40), "sacked bytes %d", sb->sacked_bytes);
274 TCP_TEST ((sb->last_sacked_bytes == 10),
275 "last sacked bytes %d", sb->last_sacked_bytes);
276 TCP_TEST ((sb->rxt_sacked == 0), "last rxt sacked bytes %d",
278 TCP_TEST (pool_elts (sb->holes) == 0, "no holes left");
281 * Ack up to 970 no sack blocks
283 vec_reset_length (tc->rcv_opts.sacks);
284 tc->rcv_opts.flags &= ~TCP_OPTS_FLAG_SACK;
285 tcp_rcv_sacks (tc, 970);
287 TCP_TEST ((sb->is_reneging), "is reneging");
288 TCP_TEST ((sb->sacked_bytes == 30), "sacked bytes %d", sb->sacked_bytes);
289 TCP_TEST ((sb->last_sacked_bytes == 0),
290 "last sacked bytes %d", sb->last_sacked_bytes);
291 TCP_TEST ((sb->rxt_sacked == 0), "last rxt sacked bytes %d",
298 tcp_rcv_sacks (tc, 1000);
299 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
300 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
301 TCP_TEST (sb->last_bytes_delivered == 30, "last bytes delivered %d",
302 sb->last_bytes_delivered);
303 TCP_TEST ((sb->last_sacked_bytes == 0),
304 "last sacked bytes %d", sb->last_sacked_bytes);
305 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
306 TCP_TEST ((!sb->is_reneging), "is not reneging");
312 tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
313 vec_reset_length (tc->rcv_opts.sacks);
317 vec_add1 (tc->rcv_opts.sacks, block);
321 tcp_rcv_sacks (tc, 1000);
324 vlib_cli_output (vm, "\nadd [1200, 1300] snd_una_max 1500, snd_una 1000:"
325 " \n%U", format_tcp_scoreboard, sb, tc);
327 TCP_TEST ((!sb->is_reneging), "is not reneging");
328 TCP_TEST ((pool_elts (sb->holes) == 2),
329 "scoreboard has %d holes", pool_elts (sb->holes));
330 hole = scoreboard_first_hole (sb);
331 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
332 "first hole start %u end %u", hole->start, hole->end);
333 TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
334 hole = scoreboard_last_hole (sb);
335 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
336 "last hole start %u end %u", hole->start, hole->end);
337 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
338 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
344 vec_reset_length (tc->rcv_opts.sacks);
345 /* Ack up to 1300 to avoid reneging */
346 tcp_rcv_sacks (tc, 1300);
349 vlib_cli_output (vm, "\nsb ack up to byte 1300:\n%U",
350 format_tcp_scoreboard, sb, tc);
352 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
353 TCP_TEST ((pool_elts (sb->holes) == 1),
354 "scoreboard has %d elements", pool_elts (sb->holes));
355 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
356 sb->last_bytes_delivered);
357 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
358 TCP_TEST ((sb->head != TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
359 TCP_TEST ((sb->tail != TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
360 TCP_TEST ((!sb->is_reneging), "is not reneging");
363 * Add some more blocks and then remove all
365 vec_reset_length (tc->rcv_opts.sacks);
368 for (i = 0; i < 5; i++)
370 block.start = i * 100 + 1200;
371 block.end = (i + 1) * 100 + 1200;
372 vec_add1 (tc->rcv_opts.sacks, block);
374 tcp_rcv_sacks (tc, 1900);
376 scoreboard_clear (sb);
378 vlib_cli_output (vm, "\nsb cleared all:\n%U", format_tcp_scoreboard, sb,
381 TCP_TEST ((pool_elts (sb->holes) == 0),
382 "number of holes %d", pool_elts (sb->holes));
383 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
384 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
387 * Re-inject odd blocks and ack them all
392 vec_reset_length (tc->rcv_opts.sacks);
393 for (i = 0; i < 5; i++)
395 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
397 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
398 tcp_rcv_sacks (tc, 0);
400 vlib_cli_output (vm, "\nsb added odd blocks snd_una 0 snd_una_max 1000:"
401 "\n%U", format_tcp_scoreboard, sb, tc);
402 TCP_TEST ((pool_elts (sb->holes) == 5),
403 "scoreboard has %d elements", pool_elts (sb->holes));
404 TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
405 hole = scoreboard_last_hole (sb);
406 TCP_TEST ((hole->end == 900), "last hole end %u", hole->end);
407 TCP_TEST ((sb->high_sacked == 1000), "high sacked %u", sb->high_sacked);
410 * Renege bytes from 950 to 1000
412 tcp_rcv_sacks (tc, 950);
415 vlib_cli_output (vm, "\nack [0, 950]:\n%U", format_tcp_scoreboard, sb,
418 TCP_TEST ((pool_elts (sb->holes) == 0), "scoreboard has %d elements",
419 pool_elts (sb->holes));
420 TCP_TEST ((sb->is_reneging), "is reneging");
421 TCP_TEST ((sb->sacked_bytes == 50), "sacked bytes %d", sb->sacked_bytes);
422 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
423 sb->last_sacked_bytes);
424 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
425 TCP_TEST ((sb->high_sacked == 1000), "high sacked %u", sb->high_sacked);
427 scoreboard_clear (sb);
430 * Inject one block, ack it and overlap hole
438 vec_add1 (tc->rcv_opts.sacks, block);
439 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
441 tcp_rcv_sacks (tc, 0);
444 vlib_cli_output (vm, "\nsb added [100, 500] snd_una 0 snd_una_max 1000:"
445 "\n%U", format_tcp_scoreboard, sb, tc);
447 tcp_rcv_sacks (tc, 800);
450 vlib_cli_output (vm, "\nsb ack [0, 800]:\n%U", format_tcp_scoreboard, sb,
453 TCP_TEST ((pool_elts (sb->holes) == 1),
454 "scoreboard has %d elements", pool_elts (sb->holes));
455 TCP_TEST ((!sb->is_reneging), "is not reneging");
456 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
457 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
458 sb->last_sacked_bytes);
459 TCP_TEST ((sb->last_bytes_delivered == 400),
460 "last bytes delivered %d", sb->last_bytes_delivered);
461 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
462 TCP_TEST ((sb->head != TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
463 TCP_TEST ((sb->tail != TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
466 * One hole close to head, patch head, split in two and start acking
469 scoreboard_clear (sb);
475 vec_add1 (tc->rcv_opts.sacks, block);
476 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
478 tcp_rcv_sacks (tc, 0);
480 vlib_cli_output (vm, "\nsb added [500, 1000]:\n%U",
481 format_tcp_scoreboard, sb, tc);
482 TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
483 TCP_TEST ((sb->last_sacked_bytes == 500), "last sacked bytes %d",
484 sb->last_sacked_bytes);
485 TCP_TEST ((sb->lost_bytes == 500), "lost bytes %u", sb->lost_bytes);
487 vec_reset_length (tc->rcv_opts.sacks);
490 vec_add1 (tc->rcv_opts.sacks, block);
491 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
492 tcp_rcv_sacks (tc, 100);
494 vlib_cli_output (vm, "\nsb added [0, 100] [300, 400]:\n%U",
495 format_tcp_scoreboard, sb, tc);
496 TCP_TEST ((pool_elts (sb->holes) == 2),
497 "scoreboard has %d elements", pool_elts (sb->holes));
498 TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
499 TCP_TEST ((sb->last_sacked_bytes == 100), "last sacked bytes %d",
500 sb->last_sacked_bytes);
501 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
502 sb->last_bytes_delivered);
503 /* Hole should be split in 2 lost holes that add up to 300 */
504 TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
505 TCP_TEST ((sb->reorder == 7), "reorder %u", sb->reorder);
508 * Ack [100 300] in two steps
510 * Step 1. Ack [100 200] which delivers 100 of the bytes lost
513 tcp_rcv_sacks (tc, 200);
514 TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
515 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
516 sb->last_bytes_delivered);
517 TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
520 * Step 2. Ack up to 300, although 300 400 is sacked, so this is interpreted
524 tcp_rcv_sacks (tc, 300);
526 vlib_cli_output (vm, "\nacked [100, 300] in two steps:\n%U",
527 format_tcp_scoreboard, sb, tc);
528 TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
529 TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
530 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
531 sb->last_bytes_delivered);
532 TCP_TEST ((sb->is_reneging), "is reneging");
535 * Ack [300 500]. Delivers reneged segment [300 400] and reneges bytes
539 tcp_rcv_sacks (tc, 500);
541 vlib_cli_output (vm, "\nacked [400, 500]:\n%U", format_tcp_scoreboard, sb,
543 TCP_TEST ((pool_elts (sb->holes) == 0),
544 "scoreboard has %d elements", pool_elts (sb->holes));
545 TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
546 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
547 sb->last_sacked_bytes);
548 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
549 sb->last_bytes_delivered);
550 TCP_TEST ((sb->is_reneging), "is reneging");
551 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
552 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
555 * Ack up to 1000 to deliver all bytes
558 tcp_rcv_sacks (tc, 1000);
560 vlib_cli_output (vm, "\nAck high sacked:\n%U", format_tcp_scoreboard, sb,
562 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
563 sb->last_sacked_bytes);
564 TCP_TEST ((sb->last_bytes_delivered == 500), "last bytes delivered %d",
565 sb->last_bytes_delivered);
566 TCP_TEST ((!sb->is_reneging), "is not reneging");
569 * Add [1200, 1500] and test that [1000, 1200] is lost (bytes condition)
570 * snd_una = 1000 and snd_una_max = 1600
574 vec_reset_length (tc->rcv_opts.sacks);
577 vec_add1 (tc->rcv_opts.sacks, block);
578 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
579 tcp_rcv_sacks (tc, 1000);
581 vlib_cli_output (vm, "\nacked [1200, 1500] test first hole is lost:\n%U",
582 format_tcp_scoreboard, sb, tc);
583 TCP_TEST ((pool_elts (sb->holes) == 2), "scoreboard has %d elements",
584 pool_elts (sb->holes));
585 TCP_TEST ((sb->sacked_bytes == 300), "sacked bytes %d", sb->sacked_bytes);
586 TCP_TEST ((sb->last_sacked_bytes == 300), "last sacked bytes %d",
587 sb->last_sacked_bytes);
588 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
589 sb->last_bytes_delivered);
590 /* No bytes lost because of reorder */
591 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
592 TCP_TEST ((sb->reorder == 7), "reorder %u", sb->reorder);
593 TCP_TEST ((!sb->is_reneging), "is not reneging");
598 scoreboard_clear (sb);
599 vec_reset_length (tc->rcv_opts.sacks);
605 tc->flags |= TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY;
612 vec_add1 (tc->rcv_opts.sacks, block);
613 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
615 tcp_rcv_sacks (tc, 0);
617 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
618 TCP_TEST ((sb->last_sacked_bytes == 400), "last sacked bytes %d",
619 sb->last_sacked_bytes);
620 TCP_TEST ((!sb->is_reneging), "is not reneging");
623 * Renege, sack all of the remaining bytes and cover some rxt bytes
626 tc->rcv_opts.sacks[0].start = 500;
627 tc->rcv_opts.sacks[0].end = 1000;
629 tcp_rcv_sacks (tc, 100);
631 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
632 TCP_TEST ((sb->last_sacked_bytes == 500), "last sacked bytes %d",
633 sb->last_sacked_bytes);
634 TCP_TEST (sb->is_reneging, "is reneging");
635 TCP_TEST ((sb->rxt_sacked == 300), "last rxt sacked bytes %d",
641 scoreboard_clear (sb);
642 vec_reset_length (tc->rcv_opts.sacks);
646 * block.start > snd_nxt
647 * && block.start < blk.end
648 * && block.end <= snd_nxt
651 block.start = 2147483647;
652 block.end = 4294967295;
653 vec_add1 (tc->rcv_opts.sacks, block);
654 tc->snd_una = tc->snd_nxt = 1969067947;
656 tcp_rcv_sacks (tc, tc->snd_una);
661 scoreboard_clear (sb);
662 vec_reset_length (tc->rcv_opts.sacks);
668 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
670 tcp_connection_t _tc, *tc = &_tc;
672 int i, verbose = 0, expected;
674 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
676 if (unformat (input, "verbose"))
680 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
686 clib_memset (tc, 0, sizeof (*tc));
689 * Add odd sack block pairs
691 for (i = 1; i < 10; i += 2)
693 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
696 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
697 vec_len (tc->snd_sacks), 5);
698 TCP_TEST ((tc->snd_sacks[0].start = 900),
699 "first sack block start %u expected %u", tc->snd_sacks[0].start,
703 * Try to add one extra
705 sacks = vec_dup (tc->snd_sacks);
707 tcp_update_sack_list (tc, 1100, 1200);
709 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
710 format_tcp_sacks, tc);
711 expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
712 TCP_TEST ((vec_len (tc->snd_sacks) == expected),
713 "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
714 TCP_TEST ((tc->snd_sacks[0].start == 1100),
715 "first sack block start %u expected %u", tc->snd_sacks[0].start,
719 vec_free (tc->snd_sacks);
720 tc->snd_sacks = sacks;
723 * Overlap first 2 segment
726 tcp_update_sack_list (tc, 300, 300);
728 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
729 format_tcp_sacks, tc);
730 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
731 vec_len (tc->snd_sacks), 3);
732 TCP_TEST ((tc->snd_sacks[0].start == 900),
733 "first sack block start %u expected %u", tc->snd_sacks[0].start,
739 tcp_update_sack_list (tc, 1100, 1200);
741 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
742 format_tcp_sacks, tc);
743 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
744 vec_len (tc->snd_sacks), 4);
745 TCP_TEST ((tc->snd_sacks[0].start == 1100),
746 "first sack block start %u expected %u", tc->snd_sacks[0].start,
750 * Join middle segments
752 tcp_update_sack_list (tc, 800, 900);
754 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
755 format_tcp_sacks, tc);
757 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
758 vec_len (tc->snd_sacks), 3);
759 TCP_TEST ((tc->snd_sacks[0].start == 700),
760 "first sack block start %u expected %u", tc->snd_sacks[0].start,
764 * Advance rcv_nxt to overlap all
767 tcp_update_sack_list (tc, 1200, 1200);
769 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
770 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
771 vec_len (tc->snd_sacks), 0);
775 * Add 2 blocks, overwrite first and update rcv_nxt to also remove it
778 vec_reset_length (tc->snd_sacks);
781 tcp_update_sack_list (tc, 100, 200);
782 tcp_update_sack_list (tc, 300, 400);
785 vlib_cli_output (vm, "add [100, 200] [300, 400]\n%U",
786 format_tcp_sacks, tc);
787 TCP_TEST ((vec_len (tc->snd_sacks) == 2),
788 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 2);
789 TCP_TEST ((tc->snd_sacks[0].start == 300),
790 "first sack block start %u expected %u", tc->snd_sacks[0].start,
794 tcp_update_sack_list (tc, 100, 100);
796 vlib_cli_output (vm, "add [100, 200] rcv_nxt = 100\n%U",
797 format_tcp_sacks, tc);
798 TCP_TEST ((vec_len (tc->snd_sacks) == 1),
799 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 1);
800 TCP_TEST ((tc->snd_sacks[0].start == 300),
801 "first sack block start %u expected %u", tc->snd_sacks[0].start,
807 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
812 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
814 if (tcp_test_sack_tx (vm, input))
819 if (tcp_test_sack_rx (vm, input))
826 if (unformat (input, "tx"))
828 res = tcp_test_sack_tx (vm, input);
830 else if (unformat (input, "rx"))
832 res = tcp_test_sack_rx (vm, input);
840 tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
842 session_main_t *smm = &session_main;
843 transport_connection_t _tc1, *tc1 = &_tc1, _tc2, *tc2 = &_tc2, *tconn;
844 tcp_connection_t *tc;
846 u8 cmp = 0, is_filtered = 0;
850 * Allocate fake session and connection 1
852 pool_get (smm->wrk[0].sessions, s);
853 clib_memset (s, 0, sizeof (*s));
854 s->session_index = sidx = s - smm->wrk[0].sessions;
856 tc = tcp_connection_alloc (0);
857 tc->connection.s_index = s->session_index;
858 s->connection_index = tc->connection.c_index;
860 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
861 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000103);
862 tc->connection.lcl_port = 35051;
863 tc->connection.rmt_port = 53764;
864 tc->connection.proto = TRANSPORT_PROTO_TCP;
865 tc->connection.is_ip4 = 1;
866 clib_memcpy_fast (tc1, &tc->connection, sizeof (*tc1));
869 * Allocate fake session and connection 2
871 pool_get (smm->wrk[0].sessions, s);
872 clib_memset (s, 0, sizeof (*s));
873 s->session_index = s - smm->wrk[0].sessions;
875 tc = tcp_connection_alloc (0);
876 tc->connection.s_index = s->session_index;
877 s->connection_index = tc->connection.c_index;
879 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
880 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000102);
881 tc->connection.lcl_port = 38225;
882 tc->connection.rmt_port = 53764;
883 tc->connection.proto = TRANSPORT_PROTO_TCP;
884 tc->connection.is_ip4 = 1;
885 clib_memcpy_fast (tc2, &tc->connection, sizeof (*tc2));
888 * Confirm that connection lookup works
891 s1 = pool_elt_at_index (smm->wrk[0].sessions, sidx);
892 session_lookup_add_connection (tc1, session_handle (s1));
893 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
895 tc1->lcl_port, tc1->rmt_port,
896 tc1->proto, 0, &is_filtered);
898 TCP_TEST ((tconn != 0), "connection exists");
899 cmp = (memcmp (&tconn->rmt_ip, &tc1->rmt_ip, sizeof (tc1->rmt_ip)) == 0);
900 TCP_TEST ((cmp), "rmt ip is identical %d", cmp);
901 TCP_TEST ((tconn->lcl_port == tc1->lcl_port),
902 "rmt port is identical %d", tconn->lcl_port == tc1->lcl_port);
905 * Non-existing connection lookup should not work
908 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
910 tc2->lcl_port, tc2->rmt_port,
911 tc2->proto, 0, &is_filtered);
912 TCP_TEST ((tconn == 0), "lookup result should be null");
915 * Delete and lookup again
917 session_lookup_del_connection (tc1);
918 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
920 tc1->lcl_port, tc1->rmt_port,
921 tc1->proto, 0, &is_filtered);
922 TCP_TEST ((tconn == 0), "lookup result should be null");
923 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
925 tc2->lcl_port, tc2->rmt_port,
926 tc2->proto, 0, &is_filtered);
927 TCP_TEST ((tconn == 0), "lookup result should be null");
930 * Re-add and lookup tc2
932 session_lookup_add_connection (tc1, tc1->s_index);
933 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
935 tc2->lcl_port, tc2->rmt_port,
936 tc2->proto, 0, &is_filtered);
937 TCP_TEST ((tconn == 0), "lookup result should be null");
943 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
946 tcp_connection_t *tc0;
947 ip4_address_t local, remote;
948 u16 local_port, remote_port;
952 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
954 if (unformat (input, "del"))
956 else if (unformat (input, "add"))
964 local.as_u32 = clib_host_to_net_u32 (0x06000101);
965 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
966 local_port = clib_host_to_net_u16 (1234);
967 remote_port = clib_host_to_net_u16 (11234);
969 tc0 = tcp_connection_alloc (0);
971 tc0->state = TCP_STATE_ESTABLISHED;
973 tc0->c_lcl_port = local_port;
974 tc0->c_rmt_port = remote_port;
976 tc0->c_thread_index = 0;
977 tc0->c_lcl_ip4.as_u32 = local.as_u32;
978 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
979 tc0->rcv_opts.mss = 1450;
980 tcp_connection_init_vars (tc0);
982 TCP_EVT (TCP_EVT_OPEN, tc0);
984 if (session_stream_accept (&tc0->connection, 0 /* listener index */ ,
985 0 /* thread index */ , 0 /* notify */ ))
986 clib_warning ("stream_session_accept failed");
988 session_stream_accept_notify (&tc0->connection);
992 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
993 tc0->state = TCP_STATE_CLOSED;
994 session_transport_closing_notify (&tc0->connection);
1001 tbt_seq_lt (u32 a, u32 b)
1003 return seq_lt (a, b);
1007 tcp_test_delivery (vlib_main_t * vm, unformat_input_t * input)
1009 u32 thread_index = 0, snd_una, *min_seqs = 0;
1010 tcp_rate_sample_t _rs = { 0 }, *rs = &_rs;
1011 tcp_connection_t _tc, *tc = &_tc;
1012 sack_scoreboard_t *sb = &tc->sack_sb;
1013 int __clib_unused verbose = 0, i;
1014 u64 rate = 1000, burst = 100;
1015 sack_block_t *sacks = 0;
1016 tcp_byte_tracker_t *bt;
1017 rb_node_t *root, *rbn;
1018 tcp_bt_sample_t *bts;
1020 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1022 if (unformat (input, "verbose"))
1026 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1032 /* Init data structures */
1033 memset (tc, 0, sizeof (*tc));
1034 session_main.wrk[thread_index].last_vlib_time = 1;
1035 transport_connection_tx_pacer_update (&tc->connection, rate, 1e6);
1041 * Track simple bursts without rxt
1044 /* 1) track first burst a time 1 */
1045 tcp_bt_track_tx (tc, burst);
1047 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1048 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1049 bts = pool_elt_at_index (bt->samples, bt->head);
1050 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1051 TCP_TEST (bts->next == TCP_BTS_INVALID_INDEX, "next should be invalid");
1052 TCP_TEST (bts->prev == TCP_BTS_INVALID_INDEX, "prev should be invalid");
1053 TCP_TEST (bts->delivered_time == 1, "delivered time should be 1");
1054 TCP_TEST (bts->delivered == 0, "delivered should be 0");
1055 TCP_TEST (!(bts->flags & TCP_BTS_IS_RXT), "not retransmitted");
1056 TCP_TEST (!(bts->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1058 /* 2) check delivery rate at time 2 */
1059 session_main.wrk[thread_index].last_vlib_time = 2;
1060 tc->snd_una = tc->snd_nxt = burst;
1061 tc->bytes_acked = burst;
1063 tcp_bt_sample_delivery_rate (tc, rs);
1065 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1066 TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1067 TCP_TEST (tc->delivered_time == 2, "delivered time should be 2");
1068 TCP_TEST (tc->delivered == burst, "delivered should be 100");
1069 TCP_TEST (rs->interval_time == 1, "ack time should be 1");
1070 TCP_TEST (rs->delivered == burst, "delivered should be 100");
1071 TCP_TEST (rs->prior_delivered == 0, "sample delivered should be 0");
1072 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1073 TCP_TEST (tc->first_tx_time == 1, "first_tx_time %u", tc->first_tx_time);
1075 /* 3) track second burst at time 2 */
1076 tcp_bt_track_tx (tc, burst);
1077 tc->snd_nxt += burst;
1079 /* 4) track second burst at time 3 */
1080 session_main.wrk[thread_index].last_vlib_time = 3;
1081 tcp_bt_track_tx (tc, burst);
1082 tc->snd_nxt += burst;
1084 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1086 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1087 bts = pool_elt_at_index (bt->samples, bt->head);
1088 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1089 TCP_TEST (bts->next == bt->tail, "next should tail");
1091 bts = pool_elt_at_index (bt->samples, bt->tail);
1092 TCP_TEST (bts->min_seq == tc->snd_nxt - burst,
1093 "min seq should be snd_nxt prior to burst");
1094 TCP_TEST (bts->prev == bt->head, "prev should be head");
1096 /* 5) check delivery rate at time 4 */
1097 session_main.wrk[thread_index].last_vlib_time = 4;
1098 tc->snd_una = tc->snd_nxt;
1099 tc->bytes_acked = 2 * burst;
1101 tcp_bt_sample_delivery_rate (tc, rs);
1103 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1104 TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1105 TCP_TEST (tc->delivered_time == 4, "delivered time should be 4");
1106 TCP_TEST (tc->delivered == 3 * burst, "delivered should be 300 is %u",
1108 TCP_TEST (rs->interval_time == 2, "ack time should be 2");
1109 TCP_TEST (rs->delivered == 2 * burst, "delivered should be 200");
1110 TCP_TEST (rs->prior_delivered == burst, "delivered should be 100");
1111 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1112 TCP_TEST (tc->first_tx_time == 2, "first_tx_time %u", tc->first_tx_time);
1115 * Track retransmissions
1117 * snd_una should be 300 at this point
1120 snd_una = tc->snd_una;
1122 /* 1) track first burst at time 4 */
1123 tcp_bt_track_tx (tc, burst);
1124 tc->snd_nxt += burst;
1126 /* 2) track second burst at time 5 */
1127 session_main.wrk[thread_index].last_vlib_time = 5;
1128 tcp_bt_track_tx (tc, burst);
1129 tc->snd_nxt += burst;
1131 /* 3) track third burst at time 6 */
1132 session_main.wrk[thread_index].last_vlib_time = 6;
1133 tcp_bt_track_tx (tc, burst);
1134 tc->snd_nxt += burst;
1136 /* 4) track fourth burst at time 7 */
1137 session_main.wrk[thread_index].last_vlib_time = 7;
1138 /* Limited until last burst is acked */
1139 tc->app_limited = snd_una + 4 * burst - 1;
1140 tcp_bt_track_tx (tc, burst);
1141 tc->snd_nxt += burst;
1143 /* 5) check delivery rate at time 8
1145 * tc->snd_una = snd_una + 10
1147 * [snd_una + burst, snd_una + burst + 10]
1148 * [snd_una + 2 * burst + 10, snd_una + 2 * burst + 20]
1150 session_main.wrk[thread_index].last_vlib_time = 8;
1152 tc->bytes_acked = 10;
1153 sb->last_sacked_bytes = 20;
1155 TCP_TEST (pool_elts (bt->samples) == 4, "there should be 4 samples");
1157 vec_validate (sacks, 1);
1158 sacks[0].start = snd_una + burst;
1159 sacks[0].end = snd_una + burst + 10;
1160 sacks[1].start = snd_una + 2 * burst + 10;
1161 sacks[1].end = snd_una + 2 * burst + 20;
1162 tc->rcv_opts.sacks = sacks;
1164 tcp_bt_sample_delivery_rate (tc, rs);
1166 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1167 TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1168 pool_elts (bt->samples));
1169 TCP_TEST (tc->delivered_time == 8, "delivered time should be 8");
1170 TCP_TEST (tc->delivered == 3 * burst + 30, "delivered should be %u is %u",
1171 3 * burst + 30, tc->delivered);
1172 /* All 3 samples have the same delivered number of bytes. So the first is
1173 * the reference for delivery estimate. */
1174 TCP_TEST (rs->interval_time == 4, "ack time should be 4 is %.2f",
1176 TCP_TEST (rs->delivered == 30, "delivered should be 30");
1177 TCP_TEST (rs->prior_delivered == 3 * burst,
1178 "sample delivered should be %u", 3 * burst);
1179 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1180 TCP_TEST (!(rs->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1181 /* All 3 samples have the same delivered number of bytes. The first
1182 * sets the first tx time */
1183 TCP_TEST (tc->first_tx_time == 4, "first_tx_time %u", tc->first_tx_time);
1185 /* 6) Retransmit and track at time 9
1187 * delivered = 3 * burst + 30
1188 * delivered_time = 8 (last ack)
1191 * [snd_una + 10, snd_una + burst]
1192 * [snd_una + burst + 10, snd_una + 2 * burst + 10]
1193 * [snd_una + 2 * burst + 20, snd_una + 4 * burst]
1195 session_main.wrk[thread_index].last_vlib_time = 9;
1197 tcp_bt_track_rxt (tc, snd_una + 10, snd_una + burst);
1198 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1199 /* The retransmit covers everything left from first burst */
1200 TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1201 pool_elts (bt->samples));
1203 tcp_bt_track_rxt (tc, snd_una + burst + 10, snd_una + 2 * burst + 10);
1204 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1205 TCP_TEST (pool_elts (bt->samples) == 6, "there should be 6 samples %u",
1206 pool_elts (bt->samples));
1208 /* Retransmit covers last sample entirely so it should be removed */
1209 tcp_bt_track_rxt (tc, snd_una + 2 * burst + 20, snd_una + 4 * burst);
1210 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1211 TCP_TEST (pool_elts (bt->samples) == 5, "there should be 5 samples %u",
1212 pool_elts (bt->samples));
1214 vec_validate (min_seqs, 4);
1215 min_seqs[0] = snd_una + 10;
1216 min_seqs[1] = snd_una + burst;
1217 min_seqs[2] = snd_una + burst + 10;
1218 min_seqs[3] = snd_una + 2 * burst + 10;
1219 min_seqs[4] = snd_una + 2 * burst + 20;
1221 root = bt->sample_lookup.nodes + bt->sample_lookup.root;
1222 bts = bt->samples + bt->head;
1223 for (i = 0; i < vec_len (min_seqs); i++)
1225 if (bts->min_seq != min_seqs[i])
1226 TCP_TEST (0, "should be %u is %u", min_seqs[i], bts->min_seq);
1227 rbn = rb_tree_search_subtree_custom (&bt->sample_lookup, root,
1228 bts->min_seq, tbt_seq_lt);
1229 if (rbn->opaque != bts - bt->samples)
1230 TCP_TEST (0, "lookup should work");
1231 bts = bt->samples + bts->next;
1234 /* 7) check delivery rate at time 10
1236 * tc->snd_una = snd_una + 2 * burst
1238 * [snd_una + 2 * burst + 20, snd_una + 2 * burst + 30]
1239 * [snd_una + 2 * burst + 50, snd_una + 2 * burst + 60]
1241 session_main.wrk[thread_index].last_vlib_time = 10;
1242 tc->snd_una = snd_una + 2 * burst;
1243 tc->bytes_acked = 2 * burst - 10;
1244 sb->last_sacked_bytes = 20;
1246 sacks[0].start = snd_una + 2 * burst + 20;
1247 sacks[0].end = snd_una + 2 * burst + 30;
1248 sacks[1].start = snd_una + 2 * burst + 50;
1249 sacks[1].end = snd_una + 2 * burst + 60;
1251 tcp_bt_sample_delivery_rate (tc, rs);
1253 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1254 TCP_TEST (pool_elts (bt->samples) == 5, "num samples should be 5 is %u",
1255 pool_elts (bt->samples));
1256 TCP_TEST (tc->delivered_time == 10, "delivered time should be 10");
1257 TCP_TEST (tc->delivered == 5 * burst + 40, "delivered should be %u is %u",
1258 5 * burst + 40, tc->delivered);
1259 /* A rxt was acked and delivered time for it is 8 (last ack time) so
1260 * ack_time is 2 (8 - 10). However, first_tx_time for rxt was 4 and rxt
1261 * time 9. Therefore snd_time is 5 (9 - 4)*/
1262 TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1264 /* delivered_now - delivered_rxt ~ 5 * burst + 40 - 3 * burst - 30 */
1265 TCP_TEST (rs->delivered == 2 * burst + 10, "delivered should be 210 is %u",
1267 TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1268 "sample delivered should be %u", 3 * burst + 30);
1269 TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1270 /* Sample is app limited because of the retransmits */
1271 TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1272 TCP_TEST (tc->app_limited, "app limited should be set");
1273 TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1277 * 8) check delivery rate at time 11
1279 session_main.wrk[thread_index].last_vlib_time = 11;
1280 tc->snd_una = tc->snd_nxt;
1281 tc->bytes_acked = 2 * burst;
1282 sb->last_sacked_bytes = 0;
1283 sb->last_bytes_delivered = 40;
1285 memset (rs, 0, sizeof (*rs));
1286 tcp_bt_sample_delivery_rate (tc, rs);
1288 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1289 TCP_TEST (pool_elts (bt->samples) == 0, "num samples should be 0 is %u",
1290 pool_elts (bt->samples));
1291 TCP_TEST (tc->delivered_time == 11, "delivered time should be 11");
1292 TCP_TEST (tc->delivered == 7 * burst, "delivered should be %u is %u",
1293 7 * burst, tc->delivered);
1294 /* Delivered time at retransmit was 8 so ack_time is 11 - 8 = 3. However,
1295 * first_tx_time for rxt was 4 and rxt time was 9. Therefore snd_time
1297 TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1299 /* delivered_now - delivered_rxt ~ 7 * burst - 3 * burst - 30.
1300 * That's because we didn't retransmit any new segment. */
1301 TCP_TEST (rs->delivered == 4 * burst - 30, "delivered should be 160 is %u",
1303 TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1304 "sample delivered should be %u", 3 * burst + 30);
1305 TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1306 TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1307 TCP_TEST (tc->app_limited == 0, "app limited should be cleared");
1308 TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1314 tcp_bt_track_tx (tc, burst);
1315 tc->snd_nxt += burst;
1317 session_main.wrk[thread_index].last_vlib_time = 12;
1318 tcp_bt_track_tx (tc, burst);
1319 tc->snd_nxt += burst;
1321 tcp_bt_flush_samples (tc);
1327 vec_free (min_seqs);
1328 tcp_bt_cleanup (tc);
1333 tcp_test_bt (vlib_main_t * vm, unformat_input_t * input)
1335 u32 thread_index = 0;
1336 tcp_rate_sample_t _rs = { 0 }, *rs = &_rs;
1337 tcp_connection_t _tc, *tc = &_tc;
1338 int __clib_unused verbose = 0, i;
1339 tcp_byte_tracker_t *bt;
1340 tcp_bt_sample_t *bts;
1344 /* Init data structures */
1345 memset (tc, 0, sizeof (*tc));
1349 /* 1) track first burst at time 1 */
1350 /* [] --> [0:100] */
1351 session_main.wrk[thread_index].last_vlib_time = 1;
1352 tcp_bt_track_tx (tc, 100);
1355 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1356 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1357 bts = pool_elt_at_index (bt->samples, bt->head);
1359 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1360 TCP_TEST (bts->next == TCP_BTS_INVALID_INDEX, "next should be invalid");
1361 TCP_TEST (bts->prev == TCP_BTS_INVALID_INDEX, "prev should be invalid");
1362 TCP_TEST (bts->tx_time == 1, "tx time should be 1");
1363 TCP_TEST (!(bts->flags & TCP_BTS_IS_RXT), "not retransmitted");
1364 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1366 /* 2) track second butst at time 2 */
1367 /* --> [0:100][100:200] */
1368 session_main.wrk[thread_index].last_vlib_time = 2;
1369 tcp_bt_track_tx (tc, 100);
1372 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1373 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1374 bts = pool_elt_at_index (bt->samples, bt->head);
1375 TCP_TEST (head == bt->head, "head is not updated");
1376 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1377 TCP_TEST (bts->tx_time == 1, "tx time of head should be 1");
1379 /* 3) acked partially at time 3 */
1382 session_main.wrk[thread_index].last_vlib_time = 3;
1384 tc->bytes_acked = 150;
1385 tc->sack_sb.last_sacked_bytes = 0;
1386 tcp_bt_sample_delivery_rate (tc, rs);
1388 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1389 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1390 TCP_TEST (head != bt->head, "head is updated");
1392 bts = pool_elt_at_index (bt->samples, bt->head);
1393 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1394 TCP_TEST (bts->tx_time == 2, "tx time should be 2");
1396 /* 4) track another burst at time 4 */
1397 /* --> [150:200][200:300] */
1398 session_main.wrk[thread_index].last_vlib_time = 4;
1399 tcp_bt_track_tx (tc, 100);
1402 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1403 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1404 bts = pool_elt_at_index (bt->samples, bt->head);
1405 TCP_TEST (head == bt->head, "head is not updated");
1406 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1407 TCP_TEST (bts->tx_time == 2, "tx time of head should be 2");
1409 /* 5) track another burst at time 5 */
1410 /* --> [150:200][200:300][300:400] */
1411 session_main.wrk[thread_index].last_vlib_time = 5;
1412 tcp_bt_track_tx (tc, 100);
1415 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1416 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1417 bts = pool_elt_at_index (bt->samples, bt->head);
1418 TCP_TEST (head == bt->head, "head is not updated");
1419 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1420 TCP_TEST (bts->tx_time == 2, "tx time of head should be 2");
1422 /* 6) acked with SACK option at time 6 */
1423 /* ACK:250 + SACK[350:400] */
1424 /* --> [250:300][300:350][350:400/sacked] */
1425 session_main.wrk[thread_index].last_vlib_time = 6;
1427 tc->bytes_acked = 100;
1428 tc->sack_sb.last_sacked_bytes = 50;
1429 vec_add2 (tc->rcv_opts.sacks, blk, 1);
1432 tcp_bt_sample_delivery_rate (tc, rs);
1434 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1435 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1436 TCP_TEST (head != bt->head, "head is updated");
1438 bts = pool_elt_at_index (bt->samples, bt->head);
1439 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1440 TCP_TEST (bts->tx_time == 4, "tx time of head should be 4");
1441 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1442 bts = pool_elt_at_index (bt->samples, bts->next);
1443 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1444 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1445 bts = pool_elt_at_index (bt->samples, bt->tail);
1446 TCP_TEST (bts->tx_time == 5, "tx time of tail should be 5");
1447 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1449 /* 7) track another burst at time 7 */
1450 /* --> [250:300][300:350][350:400/sacked][400-500] */
1451 session_main.wrk[thread_index].last_vlib_time = 7;
1452 tcp_bt_track_tx (tc, 100);
1455 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1456 TCP_TEST (pool_elts (bt->samples) == 4, "should have 4 samples");
1457 bts = pool_elt_at_index (bt->samples, bt->head);
1458 TCP_TEST (head == bt->head, "head is not updated");
1459 bts = pool_elt_at_index (bt->samples, bt->head);
1460 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1461 TCP_TEST (bts->tx_time == 4, "tx time of head should be 4");
1462 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1463 bts = pool_elt_at_index (bt->samples, bts->next);
1464 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1465 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1466 bts = pool_elt_at_index (bt->samples, bts->next);
1467 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1468 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1469 bts = pool_elt_at_index (bt->samples, bt->tail);
1470 TCP_TEST (bts->tx_time == 7, "tx time of tail should be 7");
1471 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1473 /* 8) retransmit lost one at time 8 */
1474 /* retransmit [250:300] */
1475 /* --> [250:300][300:350][350:400/sacked][400-500] */
1476 session_main.wrk[thread_index].last_vlib_time = 8;
1477 tcp_bt_track_rxt (tc, 250, 300);
1478 tcp_bt_sample_delivery_rate (tc, rs);
1480 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1481 TCP_TEST (pool_elts (bt->samples) == 4, "should have 4 samples");
1482 TCP_TEST (head == bt->head, "head is not updated");
1483 bts = pool_elt_at_index (bt->samples, bt->head);
1484 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1485 TCP_TEST (bts->tx_time == 8, "tx time of head should be 8");
1486 bts = pool_elt_at_index (bt->samples, bts->next);
1487 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1488 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1489 bts = pool_elt_at_index (bt->samples, bts->next);
1490 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1491 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1492 bts = pool_elt_at_index (bt->samples, bt->tail);
1493 TCP_TEST (bts->tx_time == 7, "tx time of tail should be 7");
1494 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1496 /* 9) acked with SACK option at time 9 */
1497 /* ACK:350 + SACK[420:450] */
1498 /* --> [400:420][420:450/sacked][450:400] */
1499 session_main.wrk[thread_index].last_vlib_time = 6;
1501 tc->bytes_acked = 150;
1502 tc->sack_sb.last_sacked_bytes = 30;
1503 vec_add2 (tc->rcv_opts.sacks, blk, 1);
1506 tcp_bt_sample_delivery_rate (tc, rs);
1508 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1509 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1510 TCP_TEST (head != bt->head, "head is updated");
1512 bts = pool_elt_at_index (bt->samples, bt->head);
1513 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1514 TCP_TEST (bts->min_seq == 400 && bts->max_seq == 420, "bts [400:420]");
1515 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1516 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1517 bts = pool_elt_at_index (bt->samples, bts->next);
1518 TCP_TEST (bts->min_seq == 420 && bts->max_seq == 450, "bts [420:450]");
1519 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1520 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1521 bts = pool_elt_at_index (bt->samples, bts->next);
1522 TCP_TEST (bts->min_seq == 450 && bts->max_seq == 500, "bts [450:500]");
1523 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1524 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1526 /* 10) acked partially at time 10 */
1529 session_main.wrk[thread_index].last_vlib_time = 3;
1531 tc->bytes_acked = 100;
1532 tc->sack_sb.last_sacked_bytes = 0;
1533 tcp_bt_sample_delivery_rate (tc, rs);
1535 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1536 TCP_TEST (pool_elts (bt->samples) == 0, "should have 0 samples");
1537 TCP_TEST (bt->head == TCP_BTS_INVALID_INDEX, "bt->head is invalidated");
1538 TCP_TEST (tc->snd_una == tc->snd_nxt, "snd_una == snd_nxt");
1543 static clib_error_t *
1544 tcp_test (vlib_main_t * vm,
1545 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1549 vnet_session_enable_disable (vm, 1);
1551 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1553 if (unformat (input, "sack"))
1555 res = tcp_test_sack (vm, input);
1557 else if (unformat (input, "session"))
1559 res = tcp_test_session (vm, input);
1561 else if (unformat (input, "lookup"))
1563 res = tcp_test_lookup (vm, input);
1565 else if (unformat (input, "delivery"))
1567 res = tcp_test_delivery (vm, input);
1569 else if (unformat (input, "bt"))
1571 res = tcp_test_bt (vm, input);
1573 else if (unformat (input, "all"))
1575 if ((res = tcp_test_sack (vm, input)))
1577 if ((res = tcp_test_lookup (vm, input)))
1579 if ((res = tcp_test_delivery (vm, input)))
1588 return clib_error_return (0, "TCP unit test failed");
1593 VLIB_CLI_COMMAND (tcp_test_command, static) =
1596 .short_help = "internal tcp unit tests",
1597 .function = tcp_test,
1602 * fd.io coding-style-patch-verification: ON
1605 * eval: (c-set-style "gnu")