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_set_time (u32 thread_index, u32 val)
1009 session_main.wrk[thread_index].last_vlib_time = val;
1010 tcp_set_time_now (&tcp_main.wrk_ctx[thread_index], val);
1014 tcp_test_delivery (vlib_main_t * vm, unformat_input_t * input)
1016 u32 thread_index = 0, snd_una, *min_seqs = 0;
1017 tcp_rate_sample_t _rs = { 0 }, *rs = &_rs;
1018 tcp_connection_t _tc, *tc = &_tc;
1019 sack_scoreboard_t *sb = &tc->sack_sb;
1020 int __clib_unused verbose = 0, i;
1021 u64 rate = 1000, burst = 100;
1022 sack_block_t *sacks = 0;
1023 tcp_byte_tracker_t *bt;
1024 rb_node_t *root, *rbn;
1025 tcp_bt_sample_t *bts;
1027 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1029 if (unformat (input, "verbose"))
1033 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1039 /* Init data structures */
1040 memset (tc, 0, sizeof (*tc));
1041 tcp_test_set_time (thread_index, 1);
1042 transport_connection_tx_pacer_update (&tc->connection, rate, 1e6);
1048 * Track simple bursts without rxt
1051 /* 1) track first burst a time 1 */
1052 tcp_bt_track_tx (tc, burst);
1054 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1055 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1056 bts = pool_elt_at_index (bt->samples, bt->head);
1057 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1058 TCP_TEST (bts->next == TCP_BTS_INVALID_INDEX, "next should be invalid");
1059 TCP_TEST (bts->prev == TCP_BTS_INVALID_INDEX, "prev should be invalid");
1060 TCP_TEST (bts->delivered_time == 1, "delivered time should be 1");
1061 TCP_TEST (bts->delivered == 0, "delivered should be 0");
1062 TCP_TEST (!(bts->flags & TCP_BTS_IS_RXT), "not retransmitted");
1063 TCP_TEST (!(bts->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1065 /* 2) check delivery rate at time 2 */
1066 tcp_test_set_time (thread_index, 2);
1067 tc->snd_una = tc->snd_nxt = burst;
1068 tc->bytes_acked = burst;
1070 tcp_bt_sample_delivery_rate (tc, rs);
1072 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1073 TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1074 TCP_TEST (tc->delivered_time == 2, "delivered time should be 2");
1075 TCP_TEST (tc->delivered == burst, "delivered should be 100");
1076 TCP_TEST (rs->interval_time == 1, "ack time should be 1");
1077 TCP_TEST (rs->delivered == burst, "delivered should be 100");
1078 TCP_TEST (rs->prior_delivered == 0, "sample delivered should be 0");
1079 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1080 TCP_TEST (tc->first_tx_time == 1, "first_tx_time %u", tc->first_tx_time);
1082 /* 3) track second burst at time 2 */
1083 tcp_bt_track_tx (tc, burst);
1084 tc->snd_nxt += burst;
1086 /* 4) track second burst at time 3 */
1087 tcp_test_set_time (thread_index, 3);
1088 tcp_bt_track_tx (tc, burst);
1089 tc->snd_nxt += burst;
1091 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1093 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1094 bts = pool_elt_at_index (bt->samples, bt->head);
1095 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1096 TCP_TEST (bts->next == bt->tail, "next should tail");
1098 bts = pool_elt_at_index (bt->samples, bt->tail);
1099 TCP_TEST (bts->min_seq == tc->snd_nxt - burst,
1100 "min seq should be snd_nxt prior to burst");
1101 TCP_TEST (bts->prev == bt->head, "prev should be head");
1103 /* 5) check delivery rate at time 4 */
1104 tcp_test_set_time (thread_index, 4);
1105 tc->snd_una = tc->snd_nxt;
1106 tc->bytes_acked = 2 * burst;
1108 tcp_bt_sample_delivery_rate (tc, rs);
1110 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1111 TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1112 TCP_TEST (tc->delivered_time == 4, "delivered time should be 4");
1113 TCP_TEST (tc->delivered == 3 * burst, "delivered should be 300 is %u",
1115 TCP_TEST (rs->interval_time == 2, "ack time should be 2");
1116 TCP_TEST (rs->delivered == 2 * burst, "delivered should be 200");
1117 TCP_TEST (rs->prior_delivered == burst, "delivered should be 100");
1118 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1119 TCP_TEST (tc->first_tx_time == 2, "first_tx_time %u", tc->first_tx_time);
1122 * Track retransmissions
1124 * snd_una should be 300 at this point
1127 snd_una = tc->snd_una;
1129 /* 1) track first burst at time 4 */
1130 tcp_bt_track_tx (tc, burst);
1131 tc->snd_nxt += burst;
1133 /* 2) track second burst at time 5 */
1134 tcp_test_set_time (thread_index, 5);
1135 tcp_bt_track_tx (tc, burst);
1136 tc->snd_nxt += burst;
1138 /* 3) track third burst at time 6 */
1139 tcp_test_set_time (thread_index, 6);
1140 tcp_bt_track_tx (tc, burst);
1141 tc->snd_nxt += burst;
1143 /* 4) track fourth burst at time 7 */
1144 tcp_test_set_time (thread_index, 7);
1145 /* Limited until last burst is acked */
1146 tc->app_limited = snd_una + 4 * burst - 1;
1147 tcp_bt_track_tx (tc, burst);
1148 tc->snd_nxt += burst;
1150 /* 5) check delivery rate at time 8
1152 * tc->snd_una = snd_una + 10
1154 * [snd_una + burst, snd_una + burst + 10]
1155 * [snd_una + 2 * burst + 10, snd_una + 2 * burst + 20]
1157 tcp_test_set_time (thread_index, 8);
1159 tc->bytes_acked = 10;
1160 sb->last_sacked_bytes = 20;
1162 TCP_TEST (pool_elts (bt->samples) == 4, "there should be 4 samples");
1164 vec_validate (sacks, 1);
1165 sacks[0].start = snd_una + burst;
1166 sacks[0].end = snd_una + burst + 10;
1167 sacks[1].start = snd_una + 2 * burst + 10;
1168 sacks[1].end = snd_una + 2 * burst + 20;
1169 tc->rcv_opts.sacks = sacks;
1171 tcp_bt_sample_delivery_rate (tc, rs);
1173 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1174 TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1175 pool_elts (bt->samples));
1176 TCP_TEST (tc->delivered_time == 8, "delivered time should be 8");
1177 TCP_TEST (tc->delivered == 3 * burst + 30, "delivered should be %u is %u",
1178 3 * burst + 30, tc->delivered);
1179 /* All 3 samples have the same delivered number of bytes. So the first is
1180 * the reference for delivery estimate. */
1181 TCP_TEST (rs->interval_time == 4, "ack time should be 4 is %.2f",
1183 TCP_TEST (rs->delivered == 30, "delivered should be 30");
1184 TCP_TEST (rs->prior_delivered == 3 * burst,
1185 "sample delivered should be %u", 3 * burst);
1186 TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1187 TCP_TEST (!(rs->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1188 /* All 3 samples have the same delivered number of bytes. The first
1189 * sets the first tx time */
1190 TCP_TEST (tc->first_tx_time == 4, "first_tx_time %u", tc->first_tx_time);
1192 /* 6) Retransmit and track at time 9
1194 * delivered = 3 * burst + 30
1195 * delivered_time = 8 (last ack)
1198 * [snd_una + 10, snd_una + burst]
1199 * [snd_una + burst + 10, snd_una + 2 * burst + 10]
1200 * [snd_una + 2 * burst + 20, snd_una + 4 * burst]
1202 tcp_test_set_time (thread_index, 9);
1204 tcp_bt_track_rxt (tc, snd_una + 10, snd_una + burst);
1205 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1206 /* The retransmit covers everything left from first burst */
1207 TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1208 pool_elts (bt->samples));
1210 tcp_bt_track_rxt (tc, snd_una + burst + 10, snd_una + 2 * burst + 10);
1211 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1212 TCP_TEST (pool_elts (bt->samples) == 6, "there should be 6 samples %u",
1213 pool_elts (bt->samples));
1215 /* Retransmit covers last sample entirely so it should be removed */
1216 tcp_bt_track_rxt (tc, snd_una + 2 * burst + 20, snd_una + 4 * burst);
1217 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1218 TCP_TEST (pool_elts (bt->samples) == 5, "there should be 5 samples %u",
1219 pool_elts (bt->samples));
1221 vec_validate (min_seqs, 4);
1222 min_seqs[0] = snd_una + 10;
1223 min_seqs[1] = snd_una + burst;
1224 min_seqs[2] = snd_una + burst + 10;
1225 min_seqs[3] = snd_una + 2 * burst + 10;
1226 min_seqs[4] = snd_una + 2 * burst + 20;
1228 root = bt->sample_lookup.nodes + bt->sample_lookup.root;
1229 bts = bt->samples + bt->head;
1230 for (i = 0; i < vec_len (min_seqs); i++)
1232 if (bts->min_seq != min_seqs[i])
1233 TCP_TEST (0, "should be %u is %u", min_seqs[i], bts->min_seq);
1234 rbn = rb_tree_search_subtree_custom (&bt->sample_lookup, root,
1235 bts->min_seq, tbt_seq_lt);
1236 if (rbn->opaque != bts - bt->samples)
1237 TCP_TEST (0, "lookup should work");
1238 bts = bt->samples + bts->next;
1241 /* 7) check delivery rate at time 10
1243 * tc->snd_una = snd_una + 2 * burst
1245 * [snd_una + 2 * burst + 20, snd_una + 2 * burst + 30]
1246 * [snd_una + 2 * burst + 50, snd_una + 2 * burst + 60]
1248 tcp_test_set_time (thread_index, 10);
1249 tc->snd_una = snd_una + 2 * burst;
1250 tc->bytes_acked = 2 * burst - 10;
1251 sb->last_sacked_bytes = 20;
1253 sacks[0].start = snd_una + 2 * burst + 20;
1254 sacks[0].end = snd_una + 2 * burst + 30;
1255 sacks[1].start = snd_una + 2 * burst + 50;
1256 sacks[1].end = snd_una + 2 * burst + 60;
1258 tcp_bt_sample_delivery_rate (tc, rs);
1260 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1261 TCP_TEST (pool_elts (bt->samples) == 5, "num samples should be 5 is %u",
1262 pool_elts (bt->samples));
1263 TCP_TEST (tc->delivered_time == 10, "delivered time should be 10");
1264 TCP_TEST (tc->delivered == 5 * burst + 40, "delivered should be %u is %u",
1265 5 * burst + 40, tc->delivered);
1266 /* A rxt was acked and delivered time for it is 8 (last ack time) so
1267 * ack_time is 2 (8 - 10). However, first_tx_time for rxt was 4 and rxt
1268 * time 9. Therefore snd_time is 5 (9 - 4)*/
1269 TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1271 /* delivered_now - delivered_rxt ~ 5 * burst + 40 - 3 * burst - 30 */
1272 TCP_TEST (rs->delivered == 2 * burst + 10, "delivered should be 210 is %u",
1274 TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1275 "sample delivered should be %u", 3 * burst + 30);
1276 TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1277 /* Sample is app limited because of the retransmits */
1278 TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1279 TCP_TEST (tc->app_limited, "app limited should be set");
1280 TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1284 * 8) check delivery rate at time 11
1286 tcp_test_set_time (thread_index, 11);
1287 tc->snd_una = tc->snd_nxt;
1288 tc->bytes_acked = 2 * burst;
1289 sb->last_sacked_bytes = 0;
1290 sb->last_bytes_delivered = 40;
1292 memset (rs, 0, sizeof (*rs));
1293 tcp_bt_sample_delivery_rate (tc, rs);
1295 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1296 TCP_TEST (pool_elts (bt->samples) == 0, "num samples should be 0 is %u",
1297 pool_elts (bt->samples));
1298 TCP_TEST (tc->delivered_time == 11, "delivered time should be 11");
1299 TCP_TEST (tc->delivered == 7 * burst, "delivered should be %u is %u",
1300 7 * burst, tc->delivered);
1301 /* Delivered time at retransmit was 8 so ack_time is 11 - 8 = 3. However,
1302 * first_tx_time for rxt was 4 and rxt time was 9. Therefore snd_time
1304 TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1306 /* delivered_now - delivered_rxt ~ 7 * burst - 3 * burst - 30.
1307 * That's because we didn't retransmit any new segment. */
1308 TCP_TEST (rs->delivered == 4 * burst - 30, "delivered should be 160 is %u",
1310 TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1311 "sample delivered should be %u", 3 * burst + 30);
1312 TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1313 TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1314 TCP_TEST (tc->app_limited == 0, "app limited should be cleared");
1315 TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1321 tcp_bt_track_tx (tc, burst);
1322 tc->snd_nxt += burst;
1324 tcp_test_set_time (thread_index, 12);
1325 tcp_bt_track_tx (tc, burst);
1326 tc->snd_nxt += burst;
1328 tcp_bt_flush_samples (tc);
1334 vec_free (min_seqs);
1335 tcp_bt_cleanup (tc);
1340 tcp_test_bt (vlib_main_t * vm, unformat_input_t * input)
1342 u32 thread_index = 0;
1343 tcp_rate_sample_t _rs = { 0 }, *rs = &_rs;
1344 tcp_connection_t _tc, *tc = &_tc;
1345 int __clib_unused verbose = 0, i;
1346 tcp_byte_tracker_t *bt;
1347 tcp_bt_sample_t *bts;
1351 /* Init data structures */
1352 memset (tc, 0, sizeof (*tc));
1356 /* 1) track first burst at time 1 */
1357 /* [] --> [0:100] */
1358 session_main.wrk[thread_index].last_vlib_time = 1;
1359 tcp_bt_track_tx (tc, 100);
1362 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1363 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1364 bts = pool_elt_at_index (bt->samples, bt->head);
1366 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1367 TCP_TEST (bts->next == TCP_BTS_INVALID_INDEX, "next should be invalid");
1368 TCP_TEST (bts->prev == TCP_BTS_INVALID_INDEX, "prev should be invalid");
1369 TCP_TEST (bts->tx_time == 1, "tx time should be 1");
1370 TCP_TEST (!(bts->flags & TCP_BTS_IS_RXT), "not retransmitted");
1371 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1373 /* 2) track second butst at time 2 */
1374 /* --> [0:100][100:200] */
1375 session_main.wrk[thread_index].last_vlib_time = 2;
1376 tcp_bt_track_tx (tc, 100);
1379 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1380 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1381 bts = pool_elt_at_index (bt->samples, bt->head);
1382 TCP_TEST (head == bt->head, "head is not updated");
1383 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1384 TCP_TEST (bts->tx_time == 1, "tx time of head should be 1");
1386 /* 3) acked partially at time 3 */
1389 session_main.wrk[thread_index].last_vlib_time = 3;
1391 tc->bytes_acked = 150;
1392 tc->sack_sb.last_sacked_bytes = 0;
1393 tcp_bt_sample_delivery_rate (tc, rs);
1395 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1396 TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1397 TCP_TEST (head != bt->head, "head is updated");
1399 bts = pool_elt_at_index (bt->samples, bt->head);
1400 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1401 TCP_TEST (bts->tx_time == 2, "tx time should be 2");
1403 /* 4) track another burst at time 4 */
1404 /* --> [150:200][200:300] */
1405 session_main.wrk[thread_index].last_vlib_time = 4;
1406 tcp_bt_track_tx (tc, 100);
1409 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1410 TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1411 bts = pool_elt_at_index (bt->samples, bt->head);
1412 TCP_TEST (head == bt->head, "head is not updated");
1413 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1414 TCP_TEST (bts->tx_time == 2, "tx time of head should be 2");
1416 /* 5) track another burst at time 5 */
1417 /* --> [150:200][200:300][300:400] */
1418 session_main.wrk[thread_index].last_vlib_time = 5;
1419 tcp_bt_track_tx (tc, 100);
1422 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1423 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1424 bts = pool_elt_at_index (bt->samples, bt->head);
1425 TCP_TEST (head == bt->head, "head is not updated");
1426 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1427 TCP_TEST (bts->tx_time == 2, "tx time of head should be 2");
1429 /* 6) acked with SACK option at time 6 */
1430 /* ACK:250 + SACK[350:400] */
1431 /* --> [250:300][300:350][350:400/sacked] */
1432 session_main.wrk[thread_index].last_vlib_time = 6;
1434 tc->bytes_acked = 100;
1435 tc->sack_sb.last_sacked_bytes = 50;
1436 vec_add2 (tc->rcv_opts.sacks, blk, 1);
1439 tcp_bt_sample_delivery_rate (tc, rs);
1441 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1442 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1443 TCP_TEST (head != bt->head, "head is updated");
1445 bts = pool_elt_at_index (bt->samples, bt->head);
1446 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1447 TCP_TEST (bts->tx_time == 4, "tx time of head should be 4");
1448 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1449 bts = pool_elt_at_index (bt->samples, bts->next);
1450 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1451 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1452 bts = pool_elt_at_index (bt->samples, bt->tail);
1453 TCP_TEST (bts->tx_time == 5, "tx time of tail should be 5");
1454 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1456 /* 7) track another burst at time 7 */
1457 /* --> [250:300][300:350][350:400/sacked][400-500] */
1458 session_main.wrk[thread_index].last_vlib_time = 7;
1459 tcp_bt_track_tx (tc, 100);
1462 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1463 TCP_TEST (pool_elts (bt->samples) == 4, "should have 4 samples");
1464 bts = pool_elt_at_index (bt->samples, bt->head);
1465 TCP_TEST (head == bt->head, "head is not updated");
1466 bts = pool_elt_at_index (bt->samples, bt->head);
1467 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1468 TCP_TEST (bts->tx_time == 4, "tx time of head should be 4");
1469 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1470 bts = pool_elt_at_index (bt->samples, bts->next);
1471 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1472 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1473 bts = pool_elt_at_index (bt->samples, bts->next);
1474 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1475 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1476 bts = pool_elt_at_index (bt->samples, bt->tail);
1477 TCP_TEST (bts->tx_time == 7, "tx time of tail should be 7");
1478 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1480 /* 8) retransmit lost one at time 8 */
1481 /* retransmit [250:300] */
1482 /* --> [250:300][300:350][350:400/sacked][400-500] */
1483 session_main.wrk[thread_index].last_vlib_time = 8;
1484 tcp_bt_track_rxt (tc, 250, 300);
1485 tcp_bt_sample_delivery_rate (tc, rs);
1487 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1488 TCP_TEST (pool_elts (bt->samples) == 4, "should have 4 samples");
1489 TCP_TEST (head == bt->head, "head is not updated");
1490 bts = pool_elt_at_index (bt->samples, bt->head);
1491 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1492 TCP_TEST (bts->tx_time == 8, "tx time of head should be 8");
1493 bts = pool_elt_at_index (bt->samples, bts->next);
1494 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1495 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1496 bts = pool_elt_at_index (bt->samples, bts->next);
1497 TCP_TEST (bts->tx_time == 5, "tx time of next should be 5");
1498 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1499 bts = pool_elt_at_index (bt->samples, bt->tail);
1500 TCP_TEST (bts->tx_time == 7, "tx time of tail should be 7");
1501 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1503 /* 9) acked with SACK option at time 9 */
1504 /* ACK:350 + SACK[420:450] */
1505 /* --> [400:420][420:450/sacked][450:400] */
1506 session_main.wrk[thread_index].last_vlib_time = 6;
1508 tc->bytes_acked = 150;
1509 tc->sack_sb.last_sacked_bytes = 30;
1510 vec_add2 (tc->rcv_opts.sacks, blk, 1);
1513 tcp_bt_sample_delivery_rate (tc, rs);
1515 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1516 TCP_TEST (pool_elts (bt->samples) == 3, "should have 3 samples");
1517 TCP_TEST (head != bt->head, "head is updated");
1519 bts = pool_elt_at_index (bt->samples, bt->head);
1520 TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1521 TCP_TEST (bts->min_seq == 400 && bts->max_seq == 420, "bts [400:420]");
1522 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1523 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1524 bts = pool_elt_at_index (bt->samples, bts->next);
1525 TCP_TEST (bts->min_seq == 420 && bts->max_seq == 450, "bts [420:450]");
1526 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1527 TCP_TEST ((bts->flags & TCP_BTS_IS_SACKED), "sacked");
1528 bts = pool_elt_at_index (bt->samples, bts->next);
1529 TCP_TEST (bts->min_seq == 450 && bts->max_seq == 500, "bts [450:500]");
1530 TCP_TEST (bts->tx_time == 7, "tx time of head should be 7");
1531 TCP_TEST (!(bts->flags & TCP_BTS_IS_SACKED), "not sacked");
1533 /* 10) acked partially at time 10 */
1536 session_main.wrk[thread_index].last_vlib_time = 3;
1538 tc->bytes_acked = 100;
1539 tc->sack_sb.last_sacked_bytes = 0;
1540 tcp_bt_sample_delivery_rate (tc, rs);
1542 TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1543 TCP_TEST (pool_elts (bt->samples) == 0, "should have 0 samples");
1544 TCP_TEST (bt->head == TCP_BTS_INVALID_INDEX, "bt->head is invalidated");
1545 TCP_TEST (tc->snd_una == tc->snd_nxt, "snd_una == snd_nxt");
1550 static clib_error_t *
1551 tcp_test (vlib_main_t * vm,
1552 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1556 vnet_session_enable_disable (vm, 1);
1558 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1560 if (unformat (input, "sack"))
1562 res = tcp_test_sack (vm, input);
1564 else if (unformat (input, "session"))
1566 res = tcp_test_session (vm, input);
1568 else if (unformat (input, "lookup"))
1570 res = tcp_test_lookup (vm, input);
1572 else if (unformat (input, "delivery"))
1574 res = tcp_test_delivery (vm, input);
1576 else if (unformat (input, "bt"))
1578 res = tcp_test_bt (vm, input);
1580 else if (unformat (input, "all"))
1582 if ((res = tcp_test_sack (vm, input)))
1584 if ((res = tcp_test_lookup (vm, input)))
1586 if ((res = tcp_test_delivery (vm, input)))
1595 return clib_error_return (0, "TCP unit test failed");
1600 VLIB_CLI_COMMAND (tcp_test_command, static) =
1603 .short_help = "internal tcp unit tests",
1604 .function = tcp_test,
1609 * fd.io coding-style-patch-verification: ON
1612 * eval: (c-set-style "gnu")