2 * Copyright (c) 2017 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>
17 #define TCP_TEST_I(_cond, _comment, _args...) \
19 int _evald = (_cond); \
21 fformat(stderr, "FAIL:%d: " _comment "\n", \
24 fformat(stderr, "PASS:%d: " _comment "\n", \
30 #define TCP_TEST(_cond, _comment, _args...) \
32 if (!TCP_TEST_I(_cond, _comment, ##_args)) { \
38 scoreboard_trace_elt_t sb_trace[] = {};
42 tcp_test_scoreboard_replay (vlib_main_t * vm, unformat_input_t * input)
45 tcp_connection_t _tc, *tc = &_tc;
48 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
50 if (unformat (input, "detail"))
54 clib_error_t *e = clib_error_return
55 (0, "unknown input `%U'", format_unformat_error, input);
56 clib_error_report (e);
61 #if TCP_SCOREBOARD_TRACE
62 tc->sack_sb.trace = sb_trace;
64 s = tcp_scoreboard_replay (s, tc, verbose);
65 vlib_cli_output (vm, "%v", s);
70 tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
72 tcp_connection_t _tc, *tc = &_tc;
73 sack_scoreboard_t *sb = &tc->sack_sb;
74 sack_block_t *sacks = 0, block;
75 sack_scoreboard_hole_t *hole;
78 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
80 if (unformat (input, "verbose"))
82 else if (unformat (input, "replay"))
83 return tcp_test_scoreboard_replay (vm, input);
86 memset (tc, 0, sizeof (*tc));
89 tc->snd_una_max = 1000;
91 tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
93 scoreboard_init (&tc->sack_sb);
95 for (i = 0; i < 1000 / 100; i++)
97 block.start = i * 100;
98 block.end = (i + 1) * 100;
99 vec_add1 (sacks, block);
106 for (i = 0; i < 1000 / 200; i++)
108 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
110 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
111 tcp_rcv_sacks (tc, 0);
114 vlib_cli_output (vm, "sb after even blocks (mss %u):\n%U",
115 tc->snd_mss, format_tcp_scoreboard, sb, tc);
117 TCP_TEST ((pool_elts (sb->holes) == 5),
118 "scoreboard has %d elements", pool_elts (sb->holes));
120 /* First SACK block should be rejected */
121 hole = scoreboard_first_hole (sb);
122 TCP_TEST ((hole->start == 0 && hole->end == 200),
123 "first hole start %u end %u", hole->start, hole->end);
124 hole = scoreboard_last_hole (sb);
125 TCP_TEST ((hole->start == 900 && hole->end == 1000),
126 "last hole start %u end %u", hole->start, hole->end);
127 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
128 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
129 TCP_TEST ((sb->last_sacked_bytes == 400),
130 "last sacked bytes %d", sb->last_sacked_bytes);
131 TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
132 TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
138 vec_reset_length (tc->rcv_opts.sacks);
139 for (i = 0; i < 1000 / 200; i++)
141 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
143 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
144 tcp_rcv_sacks (tc, 0);
147 vlib_cli_output (vm, "\nsb after odd blocks:\n%U", format_tcp_scoreboard,
150 hole = scoreboard_first_hole (sb);
151 TCP_TEST ((pool_elts (sb->holes) == 1),
152 "scoreboard has %d holes", pool_elts (sb->holes));
153 TCP_TEST ((hole->start == 0 && hole->end == 100),
154 "first hole start %u end %u", hole->start, hole->end);
155 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
156 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
157 TCP_TEST ((sb->high_sacked == 1000), "high sacked %u", sb->high_sacked);
158 TCP_TEST ((sb->last_sacked_bytes == 500),
159 "last sacked bytes %d", sb->last_sacked_bytes);
160 TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
163 * Ack until byte 100, all bytes are now acked + sacked
165 tcp_rcv_sacks (tc, 100);
167 vlib_cli_output (vm, "\nack until byte 100:\n%U", format_tcp_scoreboard,
170 TCP_TEST ((pool_elts (sb->holes) == 0),
171 "scoreboard has %d elements", pool_elts (sb->holes));
172 TCP_TEST ((sb->snd_una_adv == 900),
173 "snd_una_adv after ack %u", sb->snd_una_adv);
174 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
175 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
176 TCP_TEST ((sb->last_sacked_bytes == 0),
177 "last sacked bytes %d", sb->last_sacked_bytes);
178 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
184 vec_reset_length (tc->rcv_opts.sacks);
188 vec_add1 (tc->rcv_opts.sacks, block);
190 tc->snd_una_max = 1500;
193 tcp_rcv_sacks (tc, 1000);
196 vlib_cli_output (vm, "\nadd [1200, 1300] snd_una_max 1500, snd_una 1000:"
197 " \n%U", format_tcp_scoreboard, sb, tc);
199 TCP_TEST ((sb->snd_una_adv == 0),
200 "snd_una_adv after ack %u", sb->snd_una_adv);
201 TCP_TEST ((pool_elts (sb->holes) == 2),
202 "scoreboard has %d holes", pool_elts (sb->holes));
203 hole = scoreboard_first_hole (sb);
204 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
205 "first hole start %u end %u", hole->start, hole->end);
206 TCP_TEST ((sb->snd_una_adv == 0),
207 "snd_una_adv after ack %u", sb->snd_una_adv);
208 TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
209 hole = scoreboard_last_hole (sb);
210 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
211 "last hole start %u end %u", hole->start, hole->end);
212 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
213 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
219 vec_reset_length (tc->rcv_opts.sacks);
220 tcp_rcv_sacks (tc, 1200);
223 vlib_cli_output (vm, "\nsb ack up to byte 1200:\n%U",
224 format_tcp_scoreboard, sb, tc);
226 TCP_TEST ((sb->snd_una_adv == 100),
227 "snd_una_adv after ack %u", sb->snd_una_adv);
228 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
229 TCP_TEST ((pool_elts (sb->holes) == 0),
230 "scoreboard has %d elements", pool_elts (sb->holes));
231 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
232 sb->last_bytes_delivered);
233 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
234 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
235 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
238 * Add some more blocks and then remove all
240 vec_reset_length (tc->rcv_opts.sacks);
241 tc->snd_una += sb->snd_una_adv;
242 tc->snd_una_max = 1900;
243 for (i = 0; i < 5; i++)
245 block.start = i * 100 + 1200;
246 block.end = (i + 1) * 100 + 1200;
247 vec_add1 (tc->rcv_opts.sacks, block);
249 tcp_rcv_sacks (tc, 1900);
251 scoreboard_clear (sb);
253 vlib_cli_output (vm, "\nsb cleared all:\n%U", format_tcp_scoreboard, sb,
256 TCP_TEST ((pool_elts (sb->holes) == 0),
257 "number of holes %d", pool_elts (sb->holes));
258 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
259 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
262 * Re-inject odd blocks and ack them all
266 tc->snd_una_max = 1000;
268 for (i = 0; i < 5; i++)
270 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
272 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
273 tcp_rcv_sacks (tc, 0);
275 vlib_cli_output (vm, "\nsb added odd blocks snd_una 0 snd_una_max 1500:"
276 "\n%U", format_tcp_scoreboard, sb, tc);
277 TCP_TEST ((pool_elts (sb->holes) == 5),
278 "scoreboard has %d elements", pool_elts (sb->holes));
279 TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
281 tcp_rcv_sacks (tc, 950);
284 vlib_cli_output (vm, "\nack [0, 950]:\n%U", format_tcp_scoreboard, sb,
287 TCP_TEST ((pool_elts (sb->holes) == 0),
288 "scoreboard has %d elements", pool_elts (sb->holes));
289 TCP_TEST ((sb->snd_una_adv == 50), "snd_una_adv %u", sb->snd_una_adv);
290 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
291 TCP_TEST ((sb->last_sacked_bytes == 0),
292 "last sacked bytes %d", sb->last_sacked_bytes);
293 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
296 * Inject one block, ack it and overlap hole
300 tc->snd_una_max = 1000;
305 vec_add1 (tc->rcv_opts.sacks, block);
306 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
308 tcp_rcv_sacks (tc, 0);
311 vlib_cli_output (vm, "\nsb added [100, 500] snd_una 0 snd_una_max 1000:"
312 "\n%U", format_tcp_scoreboard, sb, tc);
314 tcp_rcv_sacks (tc, 800);
317 vlib_cli_output (vm, "\nsb ack [0, 800]:\n%U", format_tcp_scoreboard, sb,
320 TCP_TEST ((pool_elts (sb->holes) == 0),
321 "scoreboard has %d elements", pool_elts (sb->holes));
322 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
323 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
324 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
325 sb->last_sacked_bytes);
326 TCP_TEST ((sb->last_bytes_delivered == 400),
327 "last bytes delivered %d", sb->last_bytes_delivered);
328 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
329 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
330 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
333 * One hole close to head, patch head, split in two and start acking
336 scoreboard_clear (sb);
338 tc->snd_una_max = 1000;
343 vec_add1 (tc->rcv_opts.sacks, block);
344 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
346 tcp_rcv_sacks (tc, 0);
348 vlib_cli_output (vm, "\nsb added [500, 1000]:\n%U",
349 format_tcp_scoreboard, sb, tc);
350 TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
351 TCP_TEST ((sb->last_sacked_bytes == 500), "last sacked bytes %d",
352 sb->last_sacked_bytes);
353 TCP_TEST ((sb->lost_bytes == 500), "lost bytes %u", sb->lost_bytes);
355 vec_reset_length (tc->rcv_opts.sacks);
358 vec_add1 (tc->rcv_opts.sacks, block);
359 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
360 tcp_rcv_sacks (tc, 100);
362 vlib_cli_output (vm, "\nsb added [0, 100] [300, 400]:\n%U",
363 format_tcp_scoreboard, sb, tc);
364 TCP_TEST ((pool_elts (sb->holes) == 2),
365 "scoreboard has %d elements", pool_elts (sb->holes));
366 TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
367 TCP_TEST ((sb->last_sacked_bytes == 100), "last sacked bytes %d",
368 sb->last_sacked_bytes);
369 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
370 sb->last_bytes_delivered);
371 TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
374 tcp_rcv_sacks (tc, 200);
376 tcp_rcv_sacks (tc, 300);
378 vlib_cli_output (vm, "\nacked [0, 300] in two steps:\n%U",
379 format_tcp_scoreboard, sb, tc);
380 TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
381 TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
382 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
383 sb->last_bytes_delivered);
386 tcp_rcv_sacks (tc, 500);
388 vlib_cli_output (vm, "\nacked [400, 500]:\n%U", format_tcp_scoreboard, sb,
390 TCP_TEST ((pool_elts (sb->holes) == 0),
391 "scoreboard has %d elements", pool_elts (sb->holes));
392 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
393 TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
394 sb->last_sacked_bytes);
395 TCP_TEST ((sb->last_bytes_delivered == 500), "last bytes delivered %d",
396 sb->last_bytes_delivered);
397 TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
398 TCP_TEST ((sb->snd_una_adv == 500), "snd_una_adv %u", sb->snd_una_adv);
399 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
400 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
403 * Re-ack high sacked, to make sure last_bytes_delivered and
404 * snd_una_adv are 0-ed
406 tcp_rcv_sacks (tc, 1000);
408 vlib_cli_output (vm, "\nAck high sacked:\n%U", format_tcp_scoreboard, sb,
410 TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
411 sb->last_bytes_delivered);
412 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
417 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
419 tcp_connection_t _tc, *tc = &_tc;
421 int i, verbose = 0, expected;
423 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
425 if (unformat (input, "verbose"))
429 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
435 memset (tc, 0, sizeof (*tc));
438 * Add odd sack block pairs
440 for (i = 1; i < 10; i += 2)
442 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
445 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
446 vec_len (tc->snd_sacks), 5);
447 TCP_TEST ((tc->snd_sacks[0].start = 900),
448 "first sack block start %u expected %u", tc->snd_sacks[0].start,
452 * Try to add one extra
454 sacks = vec_dup (tc->snd_sacks);
456 tcp_update_sack_list (tc, 1100, 1200);
458 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
459 format_tcp_sacks, tc);
460 expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
461 TCP_TEST ((vec_len (tc->snd_sacks) == expected),
462 "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
463 TCP_TEST ((tc->snd_sacks[0].start == 1100),
464 "first sack block start %u expected %u", tc->snd_sacks[0].start,
468 vec_free (tc->snd_sacks);
469 tc->snd_sacks = sacks;
472 * Overlap first 2 segment
475 tcp_update_sack_list (tc, 300, 300);
477 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
478 format_tcp_sacks, tc);
479 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
480 vec_len (tc->snd_sacks), 3);
481 TCP_TEST ((tc->snd_sacks[0].start == 900),
482 "first sack block start %u expected %u", tc->snd_sacks[0].start,
488 tcp_update_sack_list (tc, 1100, 1200);
490 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
491 format_tcp_sacks, tc);
492 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
493 vec_len (tc->snd_sacks), 4);
494 TCP_TEST ((tc->snd_sacks[0].start == 1100),
495 "first sack block start %u expected %u", tc->snd_sacks[0].start,
499 * Join middle segments
501 tcp_update_sack_list (tc, 800, 900);
503 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
504 format_tcp_sacks, tc);
506 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
507 vec_len (tc->snd_sacks), 3);
508 TCP_TEST ((tc->snd_sacks[0].start == 700),
509 "first sack block start %u expected %u", tc->snd_sacks[0].start,
513 * Advance rcv_nxt to overlap all
516 tcp_update_sack_list (tc, 1200, 1200);
518 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
519 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
520 vec_len (tc->snd_sacks), 0);
524 * Add 2 blocks, overwrite first and update rcv_nxt to also remove it
527 vec_reset_length (tc->snd_sacks);
530 tcp_update_sack_list (tc, 100, 200);
531 tcp_update_sack_list (tc, 300, 400);
534 vlib_cli_output (vm, "add [100, 200] [300, 400]\n%U",
535 format_tcp_sacks, tc);
536 TCP_TEST ((vec_len (tc->snd_sacks) == 2),
537 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 2);
538 TCP_TEST ((tc->snd_sacks[0].start == 300),
539 "first sack block start %u expected %u", tc->snd_sacks[0].start,
543 tcp_update_sack_list (tc, 100, 100);
545 vlib_cli_output (vm, "add [100, 200] rcv_nxt = 100\n%U",
546 format_tcp_sacks, tc);
547 TCP_TEST ((vec_len (tc->snd_sacks) == 1),
548 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 1);
549 TCP_TEST ((tc->snd_sacks[0].start == 300),
550 "first sack block start %u expected %u", tc->snd_sacks[0].start,
556 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
561 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
563 if (tcp_test_sack_tx (vm, input))
568 if (tcp_test_sack_rx (vm, input))
575 if (unformat (input, "tx"))
577 res = tcp_test_sack_tx (vm, input);
579 else if (unformat (input, "rx"))
581 res = tcp_test_sack_rx (vm, input);
596 test_pattern_t test_pattern[] = {
597 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
598 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
599 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
600 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
601 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
602 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
603 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
604 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
605 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
606 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
607 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
608 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
609 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
610 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
611 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
612 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
613 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
614 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
615 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
616 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
617 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
618 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
619 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
620 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
621 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
622 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
623 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
624 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
625 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
626 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
627 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
628 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
629 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
630 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
631 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
632 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
633 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
634 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
635 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
636 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
637 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
638 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
639 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
640 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
641 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
642 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
643 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
644 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
645 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
646 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
647 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
648 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
649 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
651 /* missing from original data set */
652 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
657 pattern_cmp (const void *arg1, const void *arg2)
659 test_pattern_t *a1 = (test_pattern_t *) arg1;
660 test_pattern_t *a2 = (test_pattern_t *) arg2;
662 if (a1->offset < a2->offset)
664 else if (a1->offset > a2->offset)
670 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
673 test_pattern_t *tp = pattern;
676 /* Go through the pattern and make 100% sure it's sane */
677 for (i = 0; i < pattern_length - 1; i++)
679 if (tp->offset + tp->len != (tp + 1)->offset)
681 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
682 (tp->offset + tp->len),
683 (tp + 1)->offset - (tp->offset + tp->len));
691 static test_pattern_t *
692 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
695 test_pattern_t *validate_pattern = 0;
697 /* Validate, and try segments in order... */
698 vec_validate (validate_pattern, test_data_len - 1);
699 memcpy (validate_pattern, test_data,
700 test_data_len * sizeof (test_pattern_t));
701 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
704 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
707 return validate_pattern;
711 fifo_prepare (u32 fifo_size)
714 f = svm_fifo_create (fifo_size);
716 /* Paint fifo data vector with -1's */
717 memset (f->data, 0xFF, fifo_size);
723 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
727 for (i = start; i < len; i++)
729 if (data1[i] != data2[i])
739 tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
742 u32 fifo_size = 1 << 20;
745 int i, rv, verbose = 0;
746 u32 data_word, test_data_len, j;
747 ooo_segment_t *ooo_seg;
748 u8 *data, *s, *data_buf = 0;
750 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
752 if (unformat (input, "verbose"))
756 test_data_len = fifo_size / sizeof (u32);
757 vec_validate (test_data, test_data_len - 1);
759 for (i = 0; i < vec_len (test_data); i++)
762 f = fifo_prepare (fifo_size);
765 * Enqueue an initial (un-dequeued) chunk
767 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
768 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
769 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
772 * Create 3 chunks in the future. The offsets are relative
773 * to the current fifo tail
775 for (i = 0; i < 3; i++)
777 offset = (2 * i + 1) * sizeof (u32) - f->tail;
778 data = (u8 *) (test_data + (2 * i + 1));
781 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
782 rv = rv > 0 ? 0 : rv;
785 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
787 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
788 offset + sizeof (u32));
791 clib_warning ("enqueue returned %d", rv);
797 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
799 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
800 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
801 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
804 * Try adding a completely overlapped segment
806 offset = 3 * sizeof (u32) - f->tail;
807 data = (u8 *) (test_data + 3);
808 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
811 clib_warning ("enqueue returned %d", rv);
816 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
818 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
819 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
822 * Make sure format functions are not buggy
824 s = format (0, "%U", format_svm_fifo, f, 2);
828 * Paint some of missing data backwards
830 for (i = 3; i > 1; i--)
832 offset = (2 * i + 0) * sizeof (u32) - f->tail;
833 data = (u8 *) (test_data + (2 * i + 0));
834 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
836 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
837 offset + sizeof (u32));
840 clib_warning ("enqueue returned %d", rv);
846 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
848 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
849 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
850 ooo_seg = svm_fifo_first_ooo_segment (f);
851 TCP_TEST ((ooo_seg->start == 12),
852 "first ooo seg position %u", ooo_seg->start);
853 TCP_TEST ((ooo_seg->length == 16),
854 "first ooo seg length %u", ooo_seg->length);
857 * Enqueue the missing u32
859 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
861 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
863 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
864 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
865 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
870 for (i = 0; i < 7; i++)
872 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
873 if (rv != sizeof (u32))
875 clib_warning ("bytes dequeues %u", rv);
878 if (data_word != test_data[i])
880 clib_warning ("recovered [%d] %d not %d", i, data_word,
887 * Test segment overlaps: last ooo segment overlaps all
890 f = fifo_prepare (fifo_size);
892 for (i = 0; i < 4; i++)
894 offset = (2 * i + 1) * sizeof (u32) - f->tail;
895 data = (u8 *) (test_data + (2 * i + 1));
896 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
898 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
899 offset + sizeof (u32));
902 clib_warning ("enqueue returned %d", rv);
907 rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data);
908 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
909 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
910 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
912 vec_validate (data_buf, vec_len (data));
913 svm_fifo_peek (f, 0, vec_len (data), data_buf);
914 if (compare_data (data_buf, data, 8, vec_len (data), &j))
916 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
918 vec_reset_length (data_buf);
921 * Test segment overlaps: enqueue and overlap ooo segments
924 f = fifo_prepare (fifo_size);
926 for (i = 0; i < 4; i++)
928 offset = (2 * i + 1) * sizeof (u32) - f->tail;
929 data = (u8 *) (test_data + (2 * i + 1));
930 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
932 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
933 offset + sizeof (u32));
936 clib_warning ("enqueue returned %d", rv);
942 vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
944 rv = svm_fifo_enqueue_nowait (f, 29, data);
946 vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
948 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
949 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
950 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
952 vec_validate (data_buf, vec_len (data));
953 svm_fifo_peek (f, 0, vec_len (data), data_buf);
954 if (compare_data (data_buf, data, 0, vec_len (data), &j))
956 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
959 /* Try to peek beyond the data */
960 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
961 TCP_TEST ((rv == 0), "peeked %u expected 0", rv);
965 vec_free (test_data);
971 vec_free (test_data);
976 tcp_test_fifo2 (vlib_main_t * vm)
979 u32 fifo_size = 1 << 20;
980 int i, rv, test_data_len;
982 test_pattern_t *tp, *vp, *test_data;
983 ooo_segment_t *ooo_seg;
985 test_data = test_pattern;
986 test_data_len = ARRAY_LEN (test_pattern);
988 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
991 f = fifo_prepare (fifo_size);
994 * Try with sorted data
996 for (i = 0; i < test_data_len; i++)
1000 svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
1004 /* Expected result: one big fat chunk at offset 4 */
1005 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1006 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1007 ooo_seg = svm_fifo_first_ooo_segment (f);
1008 TCP_TEST ((ooo_seg->start == 4),
1009 "first ooo seg position %u", ooo_seg->start);
1010 TCP_TEST ((ooo_seg->length == 2996),
1011 "first ooo seg length %u", ooo_seg->length);
1014 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
1015 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
1021 * Now try it again w/ unsorted data...
1024 f = fifo_prepare (fifo_size);
1026 for (i = 0; i < test_data_len; i++)
1029 data64 = tp->offset;
1030 rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
1034 clib_warning ("enqueue returned %d", rv);
1038 /* Expecting the same result: one big fat chunk at offset 4 */
1039 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1040 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1041 ooo_seg = svm_fifo_first_ooo_segment (f);
1042 TCP_TEST ((ooo_seg->start == 4),
1043 "first ooo seg position %u", ooo_seg->start);
1044 TCP_TEST ((ooo_seg->length == 2996),
1045 "first ooo seg length %u", ooo_seg->length);
1048 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
1050 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
1058 tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
1061 u32 fifo_size = 4 << 10;
1062 u32 fifo_initial_offset = 0;
1063 u32 total_size = 2 << 10;
1064 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
1065 u8 *data_pattern = 0, *data_buf = 0;
1066 test_pattern_t *tp, *generate = 0;
1067 u32 nsegs = 2, seg_size, length_so_far;
1068 u32 current_offset, offset_increment, len_this_chunk;
1069 u32 seed = 0xdeaddabe, j;
1072 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1074 if (unformat (input, "fifo-size %d", &fifo_size))
1076 else if (unformat (input, "total-size %d", &total_size))
1078 else if (unformat (input, "verbose"))
1080 else if (unformat (input, "overlap"))
1082 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
1084 else if (unformat (input, "seed %d", &seed))
1086 else if (unformat (input, "nsegs %d", &nsegs))
1088 else if (unformat (input, "no-randomize"))
1090 else if (unformat (input, "in-seq-all"))
1092 else if (unformat (input, "drop"))
1096 clib_error_t *e = clib_error_return
1097 (0, "unknown input `%U'", format_unformat_error, input);
1098 clib_error_report (e);
1103 if (total_size > fifo_size)
1105 clib_warning ("total_size %d greater than fifo size %d", total_size,
1109 if (overlap && randomize == 0)
1111 clib_warning ("Can't enqueue in-order with overlap");
1118 vec_validate (data_pattern, total_size - 1);
1119 for (i = 0; i < vec_len (data_pattern); i++)
1120 data_pattern[i] = i & 0xff;
1125 seg_size = total_size / nsegs;
1127 current_offset = randomize;
1128 while (length_so_far < total_size)
1130 vec_add2 (generate, tp, 1);
1131 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
1132 tp->offset = current_offset;
1133 tp->len = len_this_chunk;
1135 if (overlap && (len_this_chunk == seg_size))
1138 offset_increment = len_this_chunk
1139 % (1 + (random_u32 (&seed) % len_this_chunk));
1141 while (offset_increment == 0);
1143 offset_increment = len_this_chunk;
1145 current_offset += offset_increment;
1146 length_so_far = tp->offset + tp->len;
1150 * Validate segment list. Only valid for non-overlap cases.
1153 fifo_validate_pattern (vm, generate, vec_len (generate));
1157 vlib_cli_output (vm, "raw data pattern:");
1158 for (i = 0; i < vec_len (generate); i++)
1160 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1161 generate[i].offset, generate[i].len);
1165 /* Randomize data pattern */
1168 for (i = 0; i < vec_len (generate) / 2; i++)
1170 u32 src_index, dst_index;
1171 test_pattern_t _tmp, *tmp = &_tmp;
1173 src_index = random_u32 (&seed) % vec_len (generate);
1174 dst_index = random_u32 (&seed) % vec_len (generate);
1176 tmp[0] = generate[dst_index];
1177 generate[dst_index] = generate[src_index];
1178 generate[src_index] = tmp[0];
1182 vlib_cli_output (vm, "randomized data pattern:");
1183 for (i = 0; i < vec_len (generate); i++)
1185 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1186 generate[i].offset, generate[i].len);
1192 * Create a fifo and add segments
1194 f = fifo_prepare (fifo_size);
1196 /* manually set head and tail pointers to validate modular arithmetic */
1197 fifo_initial_offset = fifo_initial_offset % fifo_size;
1198 f->head = fifo_initial_offset;
1199 f->tail = fifo_initial_offset;
1201 for (i = !randomize; i < vec_len (generate); i++)
1204 svm_fifo_enqueue_with_offset (f,
1205 fifo_initial_offset + tp->offset -
1207 (u8 *) data_pattern + tp->offset);
1210 /* Add the first segment in order for non random data */
1212 svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
1215 * Expected result: one big fat chunk at offset 1 if randomize == 1
1219 vlib_cli_output (vm, "fifo before missing link: %U",
1220 format_svm_fifo, f, 1 /* verbose */ );
1223 * Add the missing byte if segments were randomized
1227 u32 bytes_to_enq = 1;
1229 bytes_to_enq = total_size;
1230 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
1233 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
1235 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
1240 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
1241 svm_fifo_number_ooo_segments (f));
1244 * Test if peeked data is the same as original data
1246 vec_validate (data_buf, vec_len (data_pattern));
1247 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
1248 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1250 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1253 vec_reset_length (data_buf);
1256 * Dequeue or drop all data
1260 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
1264 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
1266 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1268 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1273 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
1274 svm_fifo_max_dequeue (f));
1277 vec_free (data_pattern);
1278 vec_free (data_buf);
1284 tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
1287 u32 fifo_size = 6 << 10;
1288 u32 fifo_initial_offset = 1000000000;
1289 u32 test_n_bytes = 5000, j;
1290 u8 *test_data = 0, *data_buf = 0;
1291 int i, rv, verbose = 0;
1293 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1295 if (unformat (input, "verbose"))
1299 clib_error_t *e = clib_error_return
1300 (0, "unknown input `%U'", format_unformat_error, input);
1301 clib_error_report (e);
1307 * Create a fifo and add segments
1309 f = fifo_prepare (fifo_size);
1311 /* Set head and tail pointers */
1312 fifo_initial_offset = fifo_initial_offset % fifo_size;
1313 svm_fifo_init_pointers (f, fifo_initial_offset);
1315 vec_validate (test_data, test_n_bytes - 1);
1316 for (i = 0; i < vec_len (test_data); i++)
1319 for (i = test_n_bytes - 1; i > 0; i--)
1321 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
1322 sizeof (u8), &test_data[i]);
1324 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1327 clib_warning ("enqueue returned %d", rv);
1329 vec_free (test_data);
1334 svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1336 vec_validate (data_buf, vec_len (test_data));
1338 svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1339 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1341 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1343 TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1346 vec_free (test_data);
1351 fifo_pos (svm_fifo_t * f, u32 pos)
1353 return pos % f->nitems;
1357 tcp_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
1360 u32 fifo_size = 400, j = 0, offset = 200;
1361 int i, rv, verbose = 0;
1362 u8 *test_data = 0, *data_buf = 0;
1363 ooo_segment_t *ooo_seg;
1365 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1367 if (unformat (input, "verbose"))
1371 clib_error_t *e = clib_error_return
1372 (0, "unknown input `%U'", format_unformat_error, input);
1373 clib_error_report (e);
1378 f = fifo_prepare (fifo_size);
1379 svm_fifo_init_pointers (f, offset);
1381 vec_validate (test_data, 399);
1382 for (i = 0; i < vec_len (test_data); i++)
1383 test_data[i] = i % 0xff;
1386 * Start with [100, 200] and [300, 400]
1388 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1389 svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
1391 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
1392 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1393 TCP_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
1395 vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
1396 format_svm_fifo, f, 2 /* verbose */ );
1402 rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[200]);
1404 vlib_cli_output (vm, "fifo after [225, 275] : %U",
1405 format_svm_fifo, f, 2 /* verbose */ );
1406 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 3),
1407 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1408 ooo_seg = svm_fifo_first_ooo_segment (f);
1409 TCP_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
1410 "first seg start %u expected %u", ooo_seg->start,
1411 fifo_pos (f, 100 + offset));
1412 TCP_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
1413 ooo_seg->length, 100);
1414 ooo_seg = ooo_segment_next (f, ooo_seg);
1415 TCP_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
1416 "second seg start %u expected %u",
1417 ooo_seg->start, fifo_pos (f, 225 + offset));
1418 TCP_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
1419 ooo_seg->length, 50);
1420 ooo_seg = ooo_segment_next (f, ooo_seg);
1421 TCP_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
1422 "third seg start %u expected %u",
1423 ooo_seg->start, fifo_pos (f, 300 + offset));
1424 TCP_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
1425 ooo_seg->length, 100);
1426 TCP_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
1430 rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
1432 vlib_cli_output (vm, "fifo after [190, 310] : %U",
1433 format_svm_fifo, f, 1 /* verbose */ );
1434 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1435 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1436 ooo_seg = svm_fifo_first_ooo_segment (f);
1437 TCP_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
1438 "first seg start %u expected %u",
1439 ooo_seg->start, fifo_pos (f, offset + 100));
1440 TCP_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
1441 ooo_seg->length, 300);
1446 rv = svm_fifo_enqueue_nowait (f, 150, test_data);
1449 vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
1452 TCP_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
1453 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
1454 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1456 vec_validate (data_buf, 399);
1457 svm_fifo_peek (f, 0, 400, data_buf);
1458 if (compare_data (data_buf, test_data, 0, 400, &j))
1460 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1465 * Add [100 200] and overlap it with [50 250]
1468 f = fifo_prepare (fifo_size);
1470 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1471 svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
1472 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1473 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1474 ooo_seg = svm_fifo_first_ooo_segment (f);
1475 TCP_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
1476 ooo_seg->start, 50);
1477 TCP_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
1478 ooo_seg->length, 200);
1481 vec_free (test_data);
1486 svm_fifo_trace_elem_t fifo_trace[] = {};
1490 tcp_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
1494 u8 no_read = 0, *str = 0;
1496 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1498 if (unformat (input, "verbose"))
1500 else if (unformat (input, "no-read"))
1504 clib_error_t *e = clib_error_return
1505 (0, "unknown input `%U'", format_unformat_error, input);
1506 clib_error_report (e);
1512 f.trace = fifo_trace;
1515 str = svm_fifo_replay (str, &f, no_read, verbose);
1516 vlib_cli_output (vm, "%v", str);
1521 tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1527 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1529 res = tcp_test_fifo1 (vm, input);
1533 res = tcp_test_fifo2 (vm);
1538 * Run a number of fifo3 configs
1540 str = "nsegs 10 overlap seed 123";
1541 unformat_init_cstring (input, str);
1542 if (tcp_test_fifo3 (vm, input))
1544 unformat_free (input);
1546 str = "nsegs 10 overlap seed 123 in-seq-all";
1547 unformat_init_cstring (input, str);
1548 if (tcp_test_fifo3 (vm, input))
1550 unformat_free (input);
1552 str = "nsegs 10 overlap seed 123 initial-offset 3917";
1553 unformat_init_cstring (input, str);
1554 if (tcp_test_fifo3 (vm, input))
1556 unformat_free (input);
1558 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1559 unformat_init_cstring (input, str);
1560 if (tcp_test_fifo3 (vm, input))
1562 unformat_free (input);
1564 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1565 unformat_init_cstring (input, str);
1566 if (tcp_test_fifo3 (vm, input))
1568 unformat_free (input);
1570 res = tcp_test_fifo4 (vm, input);
1574 res = tcp_test_fifo5 (vm, input);
1580 if (unformat (input, "fifo3"))
1582 res = tcp_test_fifo3 (vm, input);
1584 else if (unformat (input, "fifo2"))
1586 res = tcp_test_fifo2 (vm);
1588 else if (unformat (input, "fifo1"))
1590 res = tcp_test_fifo1 (vm, input);
1592 else if (unformat (input, "fifo4"))
1594 res = tcp_test_fifo4 (vm, input);
1596 else if (unformat (input, "fifo5"))
1598 res = tcp_test_fifo5 (vm, input);
1600 else if (unformat (input, "replay"))
1602 res = tcp_test_fifo_replay (vm, input);
1610 tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
1612 session_manager_main_t *smm = &session_manager_main;
1613 tcp_main_t *tm = &tcp_main;
1614 transport_connection_t _tc1, *tc1 = &_tc1, _tc2, *tc2 = &_tc2, *tconn;
1615 tcp_connection_t *tc;
1616 stream_session_t *s, *s1;
1617 u8 cmp = 0, is_filtered = 0;
1621 * Allocate fake session and connection 1
1623 pool_get (smm->sessions[0], s);
1624 memset (s, 0, sizeof (*s));
1625 s->session_index = sidx = s - smm->sessions[0];
1627 pool_get (tm->connections[0], tc);
1628 memset (tc, 0, sizeof (*tc));
1629 tc->connection.c_index = tc - tm->connections[0];
1630 tc->connection.s_index = s->session_index;
1631 s->connection_index = tc->connection.c_index;
1633 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1634 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000103);
1635 tc->connection.lcl_port = 35051;
1636 tc->connection.rmt_port = 53764;
1637 tc->connection.proto = TRANSPORT_PROTO_TCP;
1638 tc->connection.is_ip4 = 1;
1639 clib_memcpy (tc1, &tc->connection, sizeof (*tc1));
1642 * Allocate fake session and connection 2
1644 pool_get (session_manager_main.sessions[0], s);
1645 memset (s, 0, sizeof (*s));
1646 s->session_index = s - smm->sessions[0];
1648 pool_get (tm->connections[0], tc);
1649 memset (tc, 0, sizeof (*tc));
1650 tc->connection.c_index = tc - tm->connections[0];
1651 tc->connection.s_index = s->session_index;
1652 s->connection_index = tc->connection.c_index;
1654 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1655 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000102);
1656 tc->connection.lcl_port = 38225;
1657 tc->connection.rmt_port = 53764;
1658 tc->connection.proto = TRANSPORT_PROTO_TCP;
1659 tc->connection.is_ip4 = 1;
1660 clib_memcpy (tc2, &tc->connection, sizeof (*tc2));
1663 * Confirm that connection lookup works
1666 s1 = pool_elt_at_index (smm->sessions[0], sidx);
1667 session_lookup_add_connection (tc1, session_handle (s1));
1668 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1670 tc1->lcl_port, tc1->rmt_port,
1671 tc1->proto, 0, &is_filtered);
1673 TCP_TEST ((tconn != 0), "connection exists");
1674 cmp = (memcmp (&tconn->rmt_ip, &tc1->rmt_ip, sizeof (tc1->rmt_ip)) == 0);
1675 TCP_TEST ((cmp), "rmt ip is identical %d", cmp);
1676 TCP_TEST ((tconn->lcl_port == tc1->lcl_port),
1677 "rmt port is identical %d", tconn->lcl_port == tc1->lcl_port);
1680 * Non-existing connection lookup should not work
1683 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1685 tc2->lcl_port, tc2->rmt_port,
1686 tc2->proto, 0, &is_filtered);
1687 TCP_TEST ((tconn == 0), "lookup result should be null");
1690 * Delete and lookup again
1692 session_lookup_del_connection (tc1);
1693 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1695 tc1->lcl_port, tc1->rmt_port,
1696 tc1->proto, 0, &is_filtered);
1697 TCP_TEST ((tconn == 0), "lookup result should be null");
1698 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1700 tc2->lcl_port, tc2->rmt_port,
1701 tc2->proto, 0, &is_filtered);
1702 TCP_TEST ((tconn == 0), "lookup result should be null");
1705 * Re-add and lookup tc2
1707 session_lookup_add_connection (tc1, tc1->s_index);
1708 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1710 tc2->lcl_port, tc2->rmt_port,
1711 tc2->proto, 0, &is_filtered);
1712 TCP_TEST ((tconn == 0), "lookup result should be null");
1718 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1721 tcp_connection_t *tc0;
1722 ip4_address_t local, remote;
1723 u16 local_port, remote_port;
1724 tcp_main_t *tm = vnet_get_tcp_main ();
1728 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1730 if (unformat (input, "del"))
1732 else if (unformat (input, "add"))
1740 local.as_u32 = clib_host_to_net_u32 (0x06000101);
1741 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1742 local_port = clib_host_to_net_u16 (1234);
1743 remote_port = clib_host_to_net_u16 (11234);
1745 pool_get (tm->connections[0], tc0);
1746 memset (tc0, 0, sizeof (*tc0));
1748 tc0->state = TCP_STATE_ESTABLISHED;
1750 tc0->c_c_index = tc0 - tm->connections[0];
1751 tc0->c_lcl_port = local_port;
1752 tc0->c_rmt_port = remote_port;
1754 tc0->c_thread_index = 0;
1755 tc0->c_lcl_ip4.as_u32 = local.as_u32;
1756 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
1757 tc0->rcv_opts.mss = 1450;
1758 tcp_connection_init_vars (tc0);
1760 TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1762 if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1764 clib_warning ("stream_session_accept failed");
1766 stream_session_accept_notify (&tc0->connection);
1770 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1771 tc0->state = TCP_STATE_CLOSED;
1772 stream_session_disconnect_notify (&tc0->connection);
1778 static clib_error_t *
1779 tcp_test (vlib_main_t * vm,
1780 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1784 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1786 if (unformat (input, "sack"))
1788 res = tcp_test_sack (vm, input);
1790 else if (unformat (input, "fifo"))
1792 res = tcp_test_fifo (vm, input);
1794 else if (unformat (input, "session"))
1796 res = tcp_test_session (vm, input);
1798 else if (unformat (input, "lookup"))
1800 res = tcp_test_lookup (vm, input);
1802 else if (unformat (input, "all"))
1804 if ((res = tcp_test_sack (vm, input)))
1806 if ((res = tcp_test_fifo (vm, input)))
1808 if ((res = tcp_test_lookup (vm, input)))
1817 return clib_error_return (0, "TCP unit test failed");
1822 VLIB_CLI_COMMAND (tcp_test_command, static) =
1825 .short_help = "internal tcp unit tests",
1826 .function = tcp_test,
1831 * fd.io coding-style-patch-verification: ON
1834 * eval: (c-set-style "gnu")