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;
92 scoreboard_init (&tc->sack_sb);
94 for (i = 0; i < 1000 / 100; i++)
96 block.start = i * 100;
97 block.end = (i + 1) * 100;
98 vec_add1 (sacks, block);
105 for (i = 0; i < 1000 / 200; i++)
107 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
109 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
110 tcp_rcv_sacks (tc, 0);
113 vlib_cli_output (vm, "sb after even blocks:\n%U", format_tcp_scoreboard,
116 TCP_TEST ((pool_elts (sb->holes) == 5),
117 "scoreboard has %d elements", pool_elts (sb->holes));
119 /* First SACK block should be rejected */
120 hole = scoreboard_first_hole (sb);
121 TCP_TEST ((hole->start == 0 && hole->end == 200),
122 "first hole start %u end %u", hole->start, hole->end);
123 hole = scoreboard_last_hole (sb);
124 TCP_TEST ((hole->start == 900 && hole->end == 1000),
125 "last hole start %u end %u", hole->start, hole->end);
126 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
127 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
128 TCP_TEST ((sb->last_sacked_bytes == 400),
129 "last sacked bytes %d", sb->last_sacked_bytes);
130 TCP_TEST ((sb->high_sacked == 900), "max byte sacked %u", sb->high_sacked);
135 vec_reset_length (tc->rcv_opts.sacks);
136 for (i = 0; i < 1000 / 200; i++)
138 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
140 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
141 tcp_rcv_sacks (tc, 0);
144 vlib_cli_output (vm, "sb after odd blocks:\n%U", format_tcp_scoreboard,
147 hole = scoreboard_first_hole (sb);
148 TCP_TEST ((pool_elts (sb->holes) == 1),
149 "scoreboard has %d holes", pool_elts (sb->holes));
150 TCP_TEST ((hole->start == 0 && hole->end == 100),
151 "first hole start %u end %u", hole->start, hole->end);
152 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
153 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
154 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
155 TCP_TEST ((sb->last_sacked_bytes == 500),
156 "last sacked bytes %d", sb->last_sacked_bytes);
159 * Ack until byte 100, all bytes are now acked + sacked
161 tcp_rcv_sacks (tc, 100);
163 vlib_cli_output (vm, "ack until byte 100:\n%U", format_tcp_scoreboard,
166 TCP_TEST ((pool_elts (sb->holes) == 0),
167 "scoreboard has %d elements", pool_elts (sb->holes));
168 TCP_TEST ((sb->snd_una_adv == 900),
169 "snd_una_adv after ack %u", sb->snd_una_adv);
170 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
171 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
172 TCP_TEST ((sb->last_sacked_bytes == 0),
173 "last sacked bytes %d", sb->last_sacked_bytes);
179 vec_reset_length (tc->rcv_opts.sacks);
183 vec_add1 (tc->rcv_opts.sacks, block);
186 vlib_cli_output (vm, "add [1200, 1300]:\n%U", format_tcp_scoreboard, sb);
187 tc->snd_una_max = 1500;
190 tcp_rcv_sacks (tc, 1000);
193 vlib_cli_output (vm, "sb snd_una_max 1500, snd_una 1000:\n%U",
194 format_tcp_scoreboard, sb);
196 TCP_TEST ((sb->snd_una_adv == 0),
197 "snd_una_adv after ack %u", sb->snd_una_adv);
198 TCP_TEST ((pool_elts (sb->holes) == 2),
199 "scoreboard has %d holes", pool_elts (sb->holes));
200 hole = scoreboard_first_hole (sb);
201 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
202 "first hole start %u end %u", hole->start, hole->end);
203 TCP_TEST ((sb->snd_una_adv == 0),
204 "snd_una_adv after ack %u", sb->snd_una_adv);
205 TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
206 hole = scoreboard_last_hole (sb);
207 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
208 "last hole start %u end %u", hole->start, hole->end);
209 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
215 vec_reset_length (tc->rcv_opts.sacks);
216 tcp_rcv_sacks (tc, 1200);
219 vlib_cli_output (vm, "sb ack up to byte 1200:\n%U", format_tcp_scoreboard,
222 TCP_TEST ((sb->snd_una_adv == 100),
223 "snd_una_adv after ack %u", sb->snd_una_adv);
224 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
225 TCP_TEST ((pool_elts (sb->holes) == 1),
226 "scoreboard has %d elements", pool_elts (sb->holes));
227 hole = scoreboard_first_hole (sb);
228 TCP_TEST ((hole->prev == TCP_INVALID_SACK_HOLE_INDEX
229 && hole->next == TCP_INVALID_SACK_HOLE_INDEX), "hole is valid");
230 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
231 sb->last_bytes_delivered);
234 * Add some more blocks and then remove all
236 vec_reset_length (tc->rcv_opts.sacks);
237 tc->snd_una += sb->snd_una_adv;
238 tc->snd_una_max = 1900;
239 for (i = 0; i < 5; i++)
241 block.start = i * 100 + 1200;
242 block.end = (i + 1) * 100 + 1200;
243 vec_add1 (tc->rcv_opts.sacks, block);
245 tcp_rcv_sacks (tc, 1900);
247 scoreboard_clear (sb);
249 vlib_cli_output (vm, "sb cleared all:\n%U", format_tcp_scoreboard, sb);
251 TCP_TEST ((pool_elts (sb->holes) == 0),
252 "number of holes %d", pool_elts (sb->holes));
253 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
254 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
257 * Re-inject odd blocks and ack them all
261 tc->snd_una_max = 1000;
263 for (i = 0; i < 5; i++)
265 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
267 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
268 tcp_rcv_sacks (tc, 0);
270 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
271 format_tcp_scoreboard, sb);
273 tcp_rcv_sacks (tc, 950);
276 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
277 format_tcp_scoreboard, sb);
279 TCP_TEST ((pool_elts (sb->holes) == 0),
280 "scoreboard has %d elements", pool_elts (sb->holes));
281 TCP_TEST ((sb->snd_una_adv == 50), "snd_una_adv %u", sb->snd_una_adv);
282 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
283 TCP_TEST ((sb->last_sacked_bytes == 0),
284 "last sacked bytes %d", sb->last_sacked_bytes);
287 * Inject one block, ack it and overlap hole
291 tc->snd_una_max = 1000;
296 vec_add1 (tc->rcv_opts.sacks, block);
297 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
299 tcp_rcv_sacks (tc, 0);
302 vlib_cli_output (vm, "sb added [100, 500]:\n%U",
303 format_tcp_scoreboard, sb);
305 tcp_rcv_sacks (tc, 800);
308 vlib_cli_output (vm, "sb ack [0, 800]:\n%U", format_tcp_scoreboard, sb);
310 TCP_TEST ((pool_elts (sb->holes) == 1),
311 "scoreboard has %d elements", pool_elts (sb->holes));
312 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
313 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
314 TCP_TEST ((sb->last_sacked_bytes == 0),
315 "last sacked bytes %d", sb->last_sacked_bytes);
316 TCP_TEST ((sb->last_bytes_delivered == 400),
317 "last bytes delivered %d", sb->last_bytes_delivered);
320 * One hole close to head, patch head, split in two and start acking
323 scoreboard_clear (sb);
325 tc->snd_una_max = 1000;
330 vec_add1 (tc->rcv_opts.sacks, block);
331 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
333 tcp_rcv_sacks (tc, 0);
335 vlib_cli_output (vm, "sb added [500, 1000]:\n%U",
336 format_tcp_scoreboard, sb);
338 vec_reset_length (tc->rcv_opts.sacks);
341 vec_add1 (tc->rcv_opts.sacks, block);
342 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
343 tcp_rcv_sacks (tc, 100);
345 vlib_cli_output (vm, "sb added [0, 100] [300, 400]:\n%U",
346 format_tcp_scoreboard, sb);
347 TCP_TEST ((pool_elts (sb->holes) == 2),
348 "scoreboard has %d elements", pool_elts (sb->holes));
351 tcp_rcv_sacks (tc, 200);
352 tcp_rcv_sacks (tc, 300);
354 vlib_cli_output (vm, "sb added [0, 300]:\n%U", format_tcp_scoreboard, sb);
355 TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
361 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
363 tcp_connection_t _tc, *tc = &_tc;
365 int i, verbose = 0, expected;
367 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
369 if (unformat (input, "verbose"))
373 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
379 memset (tc, 0, sizeof (*tc));
382 * Add odd sack block pairs
384 for (i = 1; i < 10; i += 2)
386 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
389 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
390 vec_len (tc->snd_sacks), 5);
391 TCP_TEST ((tc->snd_sacks[0].start = 900),
392 "first sack block start %u expected %u", tc->snd_sacks[0].start,
396 * Try to add one extra
398 sacks = vec_dup (tc->snd_sacks);
400 tcp_update_sack_list (tc, 1100, 1200);
402 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
403 format_tcp_sacks, tc);
404 expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
405 TCP_TEST ((vec_len (tc->snd_sacks) == expected),
406 "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
407 TCP_TEST ((tc->snd_sacks[0].start == 1100),
408 "first sack block start %u expected %u", tc->snd_sacks[0].start,
412 vec_free (tc->snd_sacks);
413 tc->snd_sacks = sacks;
416 * Overlap first 2 segment
419 tcp_update_sack_list (tc, 300, 300);
421 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
422 format_tcp_sacks, tc);
423 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
424 vec_len (tc->snd_sacks), 3);
425 TCP_TEST ((tc->snd_sacks[0].start == 900),
426 "first sack block start %u expected %u", tc->snd_sacks[0].start,
432 tcp_update_sack_list (tc, 1100, 1200);
434 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
435 format_tcp_sacks, tc);
436 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
437 vec_len (tc->snd_sacks), 4);
438 TCP_TEST ((tc->snd_sacks[0].start == 1100),
439 "first sack block start %u expected %u", tc->snd_sacks[0].start,
443 * Join middle segments
445 tcp_update_sack_list (tc, 800, 900);
447 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
448 format_tcp_sacks, tc);
450 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
451 vec_len (tc->snd_sacks), 3);
452 TCP_TEST ((tc->snd_sacks[0].start == 700),
453 "first sack block start %u expected %u", tc->snd_sacks[0].start,
457 * Advance rcv_nxt to overlap all
460 tcp_update_sack_list (tc, 1200, 1200);
462 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
463 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
464 vec_len (tc->snd_sacks), 0);
468 * Add 2 blocks, overwrite first and update rcv_nxt to also remove it
471 vec_reset_length (tc->snd_sacks);
474 tcp_update_sack_list (tc, 100, 200);
475 tcp_update_sack_list (tc, 300, 400);
478 vlib_cli_output (vm, "add [100, 200] [300, 400]\n%U",
479 format_tcp_sacks, tc);
480 TCP_TEST ((vec_len (tc->snd_sacks) == 2),
481 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 2);
482 TCP_TEST ((tc->snd_sacks[0].start == 300),
483 "first sack block start %u expected %u", tc->snd_sacks[0].start,
487 tcp_update_sack_list (tc, 100, 100);
489 vlib_cli_output (vm, "add [100, 200] rcv_nxt = 100\n%U",
490 format_tcp_sacks, tc);
491 TCP_TEST ((vec_len (tc->snd_sacks) == 1),
492 "sack blocks %d expected %d", vec_len (tc->snd_sacks), 1);
493 TCP_TEST ((tc->snd_sacks[0].start == 300),
494 "first sack block start %u expected %u", tc->snd_sacks[0].start,
500 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
505 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
507 if (tcp_test_sack_tx (vm, input))
512 if (tcp_test_sack_rx (vm, input))
519 if (unformat (input, "tx"))
521 res = tcp_test_sack_tx (vm, input);
523 else if (unformat (input, "rx"))
525 res = tcp_test_sack_rx (vm, input);
540 test_pattern_t test_pattern[] = {
541 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
542 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
543 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
544 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
545 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
546 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
547 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
548 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
549 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
550 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
551 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
552 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
553 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
554 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
555 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
556 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
557 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
558 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
559 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
560 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
561 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
562 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
563 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
564 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
565 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
566 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
567 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
568 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
569 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
570 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
571 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
572 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
573 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
574 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
575 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
576 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
577 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
578 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
579 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
580 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
581 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
582 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
583 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
584 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
585 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
586 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
587 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
588 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
589 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
590 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
591 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
592 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
593 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
595 /* missing from original data set */
596 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
601 pattern_cmp (const void *arg1, const void *arg2)
603 test_pattern_t *a1 = (test_pattern_t *) arg1;
604 test_pattern_t *a2 = (test_pattern_t *) arg2;
606 if (a1->offset < a2->offset)
608 else if (a1->offset > a2->offset)
614 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
617 test_pattern_t *tp = pattern;
620 /* Go through the pattern and make 100% sure it's sane */
621 for (i = 0; i < pattern_length - 1; i++)
623 if (tp->offset + tp->len != (tp + 1)->offset)
625 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
626 (tp->offset + tp->len),
627 (tp + 1)->offset - (tp->offset + tp->len));
635 static test_pattern_t *
636 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
639 test_pattern_t *validate_pattern = 0;
641 /* Validate, and try segments in order... */
642 vec_validate (validate_pattern, test_data_len - 1);
643 memcpy (validate_pattern, test_data,
644 test_data_len * sizeof (test_pattern_t));
645 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
648 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
651 return validate_pattern;
655 fifo_prepare (u32 fifo_size)
658 f = svm_fifo_create (fifo_size);
660 /* Paint fifo data vector with -1's */
661 memset (f->data, 0xFF, fifo_size);
667 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
671 for (i = start; i < len; i++)
673 if (data1[i] != data2[i])
683 tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
686 u32 fifo_size = 1 << 20;
689 int i, rv, verbose = 0;
690 u32 data_word, test_data_len, j;
691 ooo_segment_t *ooo_seg;
692 u8 *data, *s, *data_buf = 0;
694 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
696 if (unformat (input, "verbose"))
700 test_data_len = fifo_size / sizeof (u32);
701 vec_validate (test_data, test_data_len - 1);
703 for (i = 0; i < vec_len (test_data); i++)
706 f = fifo_prepare (fifo_size);
709 * Enqueue an initial (un-dequeued) chunk
711 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
712 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
713 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
716 * Create 3 chunks in the future. The offsets are relative
717 * to the current fifo tail
719 for (i = 0; i < 3; i++)
721 offset = (2 * i + 1) * sizeof (u32) - f->tail;
722 data = (u8 *) (test_data + (2 * i + 1));
725 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
726 rv = rv > 0 ? 0 : rv;
729 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
731 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
732 offset + sizeof (u32));
735 clib_warning ("enqueue returned %d", rv);
741 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
743 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
744 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
745 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
748 * Try adding a completely overlapped segment
750 offset = 3 * sizeof (u32) - f->tail;
751 data = (u8 *) (test_data + 3);
752 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
755 clib_warning ("enqueue returned %d", rv);
760 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
762 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
763 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
766 * Make sure format functions are not buggy
768 s = format (0, "%U", format_svm_fifo, f, 2);
772 * Paint some of missing data backwards
774 for (i = 3; i > 1; i--)
776 offset = (2 * i + 0) * sizeof (u32) - f->tail;
777 data = (u8 *) (test_data + (2 * i + 0));
778 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
780 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
781 offset + sizeof (u32));
784 clib_warning ("enqueue returned %d", rv);
790 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
792 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
793 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
794 ooo_seg = svm_fifo_first_ooo_segment (f);
795 TCP_TEST ((ooo_seg->start == 12),
796 "first ooo seg position %u", ooo_seg->start);
797 TCP_TEST ((ooo_seg->length == 16),
798 "first ooo seg length %u", ooo_seg->length);
801 * Enqueue the missing u32
803 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
805 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
807 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
808 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
809 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
814 for (i = 0; i < 7; i++)
816 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
817 if (rv != sizeof (u32))
819 clib_warning ("bytes dequeues %u", rv);
822 if (data_word != test_data[i])
824 clib_warning ("recovered [%d] %d not %d", i, data_word,
831 * Test segment overlaps: last ooo segment overlaps all
834 f = fifo_prepare (fifo_size);
836 for (i = 0; i < 4; i++)
838 offset = (2 * i + 1) * sizeof (u32) - f->tail;
839 data = (u8 *) (test_data + (2 * i + 1));
840 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
842 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
843 offset + sizeof (u32));
846 clib_warning ("enqueue returned %d", rv);
851 rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data);
852 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
853 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
854 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
856 vec_validate (data_buf, vec_len (data));
857 svm_fifo_peek (f, 0, vec_len (data), data_buf);
858 if (compare_data (data_buf, data, 8, vec_len (data), &j))
860 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
862 vec_reset_length (data_buf);
865 * Test segment overlaps: enqueue and overlap ooo segments
868 f = fifo_prepare (fifo_size);
870 for (i = 0; i < 4; i++)
872 offset = (2 * i + 1) * sizeof (u32) - f->tail;
873 data = (u8 *) (test_data + (2 * i + 1));
874 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
876 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
877 offset + sizeof (u32));
880 clib_warning ("enqueue returned %d", rv);
886 vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
888 rv = svm_fifo_enqueue_nowait (f, 29, data);
890 vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
892 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
893 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
894 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
896 vec_validate (data_buf, vec_len (data));
897 svm_fifo_peek (f, 0, vec_len (data), data_buf);
898 if (compare_data (data_buf, data, 0, vec_len (data), &j))
900 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
903 /* Try to peek beyond the data */
904 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
905 TCP_TEST ((rv == 0), "peeked %u expected 0", rv);
909 vec_free (test_data);
915 vec_free (test_data);
920 tcp_test_fifo2 (vlib_main_t * vm)
923 u32 fifo_size = 1 << 20;
924 int i, rv, test_data_len;
926 test_pattern_t *tp, *vp, *test_data;
927 ooo_segment_t *ooo_seg;
929 test_data = test_pattern;
930 test_data_len = ARRAY_LEN (test_pattern);
932 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
935 f = fifo_prepare (fifo_size);
938 * Try with sorted data
940 for (i = 0; i < test_data_len; i++)
944 svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
948 /* Expected result: one big fat chunk at offset 4 */
949 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
950 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
951 ooo_seg = svm_fifo_first_ooo_segment (f);
952 TCP_TEST ((ooo_seg->start == 4),
953 "first ooo seg position %u", ooo_seg->start);
954 TCP_TEST ((ooo_seg->length == 2996),
955 "first ooo seg length %u", ooo_seg->length);
958 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
959 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
965 * Now try it again w/ unsorted data...
968 f = fifo_prepare (fifo_size);
970 for (i = 0; i < test_data_len; i++)
974 rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
978 clib_warning ("enqueue returned %d", rv);
982 /* Expecting the same result: one big fat chunk at offset 4 */
983 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
984 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
985 ooo_seg = svm_fifo_first_ooo_segment (f);
986 TCP_TEST ((ooo_seg->start == 4),
987 "first ooo seg position %u", ooo_seg->start);
988 TCP_TEST ((ooo_seg->length == 2996),
989 "first ooo seg length %u", ooo_seg->length);
992 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
994 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
1002 tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
1005 u32 fifo_size = 4 << 10;
1006 u32 fifo_initial_offset = 0;
1007 u32 total_size = 2 << 10;
1008 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
1009 u8 *data_pattern = 0, *data_buf = 0;
1010 test_pattern_t *tp, *generate = 0;
1011 u32 nsegs = 2, seg_size, length_so_far;
1012 u32 current_offset, offset_increment, len_this_chunk;
1013 u32 seed = 0xdeaddabe, j;
1016 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1018 if (unformat (input, "fifo-size %d", &fifo_size))
1020 else if (unformat (input, "total-size %d", &total_size))
1022 else if (unformat (input, "verbose"))
1024 else if (unformat (input, "overlap"))
1026 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
1028 else if (unformat (input, "seed %d", &seed))
1030 else if (unformat (input, "nsegs %d", &nsegs))
1032 else if (unformat (input, "no-randomize"))
1034 else if (unformat (input, "in-seq-all"))
1036 else if (unformat (input, "drop"))
1040 clib_error_t *e = clib_error_return
1041 (0, "unknown input `%U'", format_unformat_error, input);
1042 clib_error_report (e);
1047 if (total_size > fifo_size)
1049 clib_warning ("total_size %d greater than fifo size %d", total_size,
1053 if (overlap && randomize == 0)
1055 clib_warning ("Can't enqueue in-order with overlap");
1062 vec_validate (data_pattern, total_size - 1);
1063 for (i = 0; i < vec_len (data_pattern); i++)
1064 data_pattern[i] = i & 0xff;
1069 seg_size = total_size / nsegs;
1071 current_offset = randomize;
1072 while (length_so_far < total_size)
1074 vec_add2 (generate, tp, 1);
1075 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
1076 tp->offset = current_offset;
1077 tp->len = len_this_chunk;
1079 if (overlap && (len_this_chunk == seg_size))
1082 offset_increment = len_this_chunk
1083 % (1 + (random_u32 (&seed) % len_this_chunk));
1085 while (offset_increment == 0);
1087 offset_increment = len_this_chunk;
1089 current_offset += offset_increment;
1090 length_so_far = tp->offset + tp->len;
1094 * Validate segment list. Only valid for non-overlap cases.
1097 fifo_validate_pattern (vm, generate, vec_len (generate));
1101 vlib_cli_output (vm, "raw data pattern:");
1102 for (i = 0; i < vec_len (generate); i++)
1104 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1105 generate[i].offset, generate[i].len);
1109 /* Randomize data pattern */
1112 for (i = 0; i < vec_len (generate) / 2; i++)
1114 u32 src_index, dst_index;
1115 test_pattern_t _tmp, *tmp = &_tmp;
1117 src_index = random_u32 (&seed) % vec_len (generate);
1118 dst_index = random_u32 (&seed) % vec_len (generate);
1120 tmp[0] = generate[dst_index];
1121 generate[dst_index] = generate[src_index];
1122 generate[src_index] = tmp[0];
1126 vlib_cli_output (vm, "randomized data pattern:");
1127 for (i = 0; i < vec_len (generate); i++)
1129 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1130 generate[i].offset, generate[i].len);
1136 * Create a fifo and add segments
1138 f = fifo_prepare (fifo_size);
1140 /* manually set head and tail pointers to validate modular arithmetic */
1141 fifo_initial_offset = fifo_initial_offset % fifo_size;
1142 f->head = fifo_initial_offset;
1143 f->tail = fifo_initial_offset;
1145 for (i = !randomize; i < vec_len (generate); i++)
1148 svm_fifo_enqueue_with_offset (f,
1149 fifo_initial_offset + tp->offset -
1151 (u8 *) data_pattern + tp->offset);
1154 /* Add the first segment in order for non random data */
1156 svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
1159 * Expected result: one big fat chunk at offset 1 if randomize == 1
1163 vlib_cli_output (vm, "fifo before missing link: %U",
1164 format_svm_fifo, f, 1 /* verbose */ );
1167 * Add the missing byte if segments were randomized
1171 u32 bytes_to_enq = 1;
1173 bytes_to_enq = total_size;
1174 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
1177 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
1179 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
1184 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
1185 svm_fifo_number_ooo_segments (f));
1188 * Test if peeked data is the same as original data
1190 vec_validate (data_buf, vec_len (data_pattern));
1191 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
1192 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1194 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1197 vec_reset_length (data_buf);
1200 * Dequeue or drop all data
1204 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
1208 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
1210 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1212 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1217 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
1218 svm_fifo_max_dequeue (f));
1221 vec_free (data_pattern);
1222 vec_free (data_buf);
1228 tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
1231 u32 fifo_size = 6 << 10;
1232 u32 fifo_initial_offset = 1000000000;
1233 u32 test_n_bytes = 5000, j;
1234 u8 *test_data = 0, *data_buf = 0;
1235 int i, rv, verbose = 0;
1237 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1239 if (unformat (input, "verbose"))
1243 clib_error_t *e = clib_error_return
1244 (0, "unknown input `%U'", format_unformat_error, input);
1245 clib_error_report (e);
1251 * Create a fifo and add segments
1253 f = fifo_prepare (fifo_size);
1255 /* Set head and tail pointers */
1256 fifo_initial_offset = fifo_initial_offset % fifo_size;
1257 svm_fifo_init_pointers (f, fifo_initial_offset);
1259 vec_validate (test_data, test_n_bytes - 1);
1260 for (i = 0; i < vec_len (test_data); i++)
1263 for (i = test_n_bytes - 1; i > 0; i--)
1265 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
1266 sizeof (u8), &test_data[i]);
1268 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1271 clib_warning ("enqueue returned %d", rv);
1273 vec_free (test_data);
1278 svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1280 vec_validate (data_buf, vec_len (test_data));
1282 svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1283 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1285 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1287 TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1290 vec_free (test_data);
1295 fifo_pos (svm_fifo_t * f, u32 pos)
1297 return pos % f->nitems;
1301 tcp_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
1304 u32 fifo_size = 400, j = 0, offset = 200;
1305 int i, rv, verbose = 0;
1306 u8 *test_data = 0, *data_buf = 0;
1307 ooo_segment_t *ooo_seg;
1309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1311 if (unformat (input, "verbose"))
1315 clib_error_t *e = clib_error_return
1316 (0, "unknown input `%U'", format_unformat_error, input);
1317 clib_error_report (e);
1322 f = fifo_prepare (fifo_size);
1323 svm_fifo_init_pointers (f, offset);
1325 vec_validate (test_data, 399);
1326 for (i = 0; i < vec_len (test_data); i++)
1327 test_data[i] = i % 0xff;
1330 * Start with [100, 200] and [300, 400]
1332 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1333 svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
1335 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
1336 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1337 TCP_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
1339 vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
1340 format_svm_fifo, f, 2 /* verbose */ );
1346 rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[200]);
1348 vlib_cli_output (vm, "fifo after [225, 275] : %U",
1349 format_svm_fifo, f, 2 /* verbose */ );
1350 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 3),
1351 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1352 ooo_seg = svm_fifo_first_ooo_segment (f);
1353 TCP_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
1354 "first seg start %u expected %u", ooo_seg->start,
1355 fifo_pos (f, 100 + offset));
1356 TCP_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
1357 ooo_seg->length, 100);
1358 ooo_seg = ooo_segment_next (f, ooo_seg);
1359 TCP_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
1360 "second seg start %u expected %u",
1361 ooo_seg->start, fifo_pos (f, 225 + offset));
1362 TCP_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
1363 ooo_seg->length, 50);
1364 ooo_seg = ooo_segment_next (f, ooo_seg);
1365 TCP_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
1366 "third seg start %u expected %u",
1367 ooo_seg->start, fifo_pos (f, 300 + offset));
1368 TCP_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
1369 ooo_seg->length, 100);
1370 TCP_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
1374 rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
1376 vlib_cli_output (vm, "fifo after [190, 310] : %U",
1377 format_svm_fifo, f, 1 /* verbose */ );
1378 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1379 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1380 ooo_seg = svm_fifo_first_ooo_segment (f);
1381 TCP_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
1382 "first seg start %u expected %u",
1383 ooo_seg->start, fifo_pos (f, offset + 100));
1384 TCP_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
1385 ooo_seg->length, 300);
1390 rv = svm_fifo_enqueue_nowait (f, 150, test_data);
1393 vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
1396 TCP_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
1397 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
1398 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1400 vec_validate (data_buf, 399);
1401 svm_fifo_peek (f, 0, 400, data_buf);
1402 if (compare_data (data_buf, test_data, 0, 400, &j))
1404 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1409 * Add [100 200] and overlap it with [50 250]
1412 f = fifo_prepare (fifo_size);
1414 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1415 svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
1416 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1417 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1418 ooo_seg = svm_fifo_first_ooo_segment (f);
1419 TCP_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
1420 ooo_seg->start, 50);
1421 TCP_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
1422 ooo_seg->length, 200);
1425 vec_free (test_data);
1430 svm_fifo_trace_elem_t fifo_trace[] = {};
1434 tcp_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
1438 u8 no_read = 0, *str = 0;
1440 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1442 if (unformat (input, "verbose"))
1444 else if (unformat (input, "no-read"))
1448 clib_error_t *e = clib_error_return
1449 (0, "unknown input `%U'", format_unformat_error, input);
1450 clib_error_report (e);
1456 f.trace = fifo_trace;
1459 str = svm_fifo_replay (str, &f, no_read, verbose);
1460 vlib_cli_output (vm, "%v", str);
1465 tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1471 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1473 res = tcp_test_fifo1 (vm, input);
1477 res = tcp_test_fifo2 (vm);
1482 * Run a number of fifo3 configs
1484 str = "nsegs 10 overlap seed 123";
1485 unformat_init_cstring (input, str);
1486 if (tcp_test_fifo3 (vm, input))
1488 unformat_free (input);
1490 str = "nsegs 10 overlap seed 123 in-seq-all";
1491 unformat_init_cstring (input, str);
1492 if (tcp_test_fifo3 (vm, input))
1494 unformat_free (input);
1496 str = "nsegs 10 overlap seed 123 initial-offset 3917";
1497 unformat_init_cstring (input, str);
1498 if (tcp_test_fifo3 (vm, input))
1500 unformat_free (input);
1502 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1503 unformat_init_cstring (input, str);
1504 if (tcp_test_fifo3 (vm, input))
1506 unformat_free (input);
1508 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1509 unformat_init_cstring (input, str);
1510 if (tcp_test_fifo3 (vm, input))
1512 unformat_free (input);
1514 res = tcp_test_fifo4 (vm, input);
1518 res = tcp_test_fifo5 (vm, input);
1524 if (unformat (input, "fifo3"))
1526 res = tcp_test_fifo3 (vm, input);
1528 else if (unformat (input, "fifo2"))
1530 res = tcp_test_fifo2 (vm);
1532 else if (unformat (input, "fifo1"))
1534 res = tcp_test_fifo1 (vm, input);
1536 else if (unformat (input, "fifo4"))
1538 res = tcp_test_fifo4 (vm, input);
1540 else if (unformat (input, "fifo5"))
1542 res = tcp_test_fifo5 (vm, input);
1544 else if (unformat (input, "replay"))
1546 res = tcp_test_fifo_replay (vm, input);
1554 tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
1556 session_manager_main_t *smm = &session_manager_main;
1557 tcp_main_t *tm = &tcp_main;
1558 transport_connection_t _tc1, *tc1 = &_tc1, _tc2, *tc2 = &_tc2, *tconn;
1559 tcp_connection_t *tc;
1560 stream_session_t *s, *s1;
1561 u8 cmp = 0, is_filtered = 0;
1564 * Allocate fake session and connection 1
1566 pool_get (smm->sessions[0], s);
1567 memset (s, 0, sizeof (*s));
1568 s->session_index = s - smm->sessions[0];
1570 pool_get (tm->connections[0], tc);
1571 memset (tc, 0, sizeof (*tc));
1572 tc->connection.c_index = tc - tm->connections[0];
1573 tc->connection.s_index = s->session_index;
1574 s->connection_index = tc->connection.c_index;
1576 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1577 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000103);
1578 tc->connection.lcl_port = 35051;
1579 tc->connection.rmt_port = 53764;
1580 tc->connection.proto = TRANSPORT_PROTO_TCP;
1581 tc->connection.is_ip4 = 1;
1582 clib_memcpy (tc1, &tc->connection, sizeof (*tc1));
1586 * Allocate fake session and connection 2
1588 pool_get (session_manager_main.sessions[0], s);
1589 memset (s, 0, sizeof (*s));
1590 s->session_index = s - smm->sessions[0];
1592 pool_get (tm->connections[0], tc);
1593 memset (tc, 0, sizeof (*tc));
1594 tc->connection.c_index = tc - tm->connections[0];
1595 tc->connection.s_index = s->session_index;
1596 s->connection_index = tc->connection.c_index;
1598 tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1599 tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000102);
1600 tc->connection.lcl_port = 38225;
1601 tc->connection.rmt_port = 53764;
1602 tc->connection.proto = TRANSPORT_PROTO_TCP;
1603 tc->connection.is_ip4 = 1;
1604 clib_memcpy (tc2, &tc->connection, sizeof (*tc2));
1607 * Confirm that connection lookup works
1610 session_lookup_add_connection (tc1, session_handle (s1));
1611 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1613 tc1->lcl_port, tc1->rmt_port,
1614 tc1->proto, 0, &is_filtered);
1616 TCP_TEST ((tconn != 0), "connection exists");
1617 cmp = (memcmp (&tconn->rmt_ip, &tc1->rmt_ip, sizeof (tc1->rmt_ip)) == 0);
1618 TCP_TEST ((cmp), "rmt ip is identical %d", cmp);
1619 TCP_TEST ((tconn->lcl_port == tc1->lcl_port),
1620 "rmt port is identical %d", tconn->lcl_port == tc1->lcl_port);
1623 * Non-existing connection lookup should not work
1626 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1628 tc2->lcl_port, tc2->rmt_port,
1629 tc2->proto, 0, &is_filtered);
1630 TCP_TEST ((tconn == 0), "lookup result should be null");
1633 * Delete and lookup again
1635 session_lookup_del_connection (tc1);
1636 tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1638 tc1->lcl_port, tc1->rmt_port,
1639 tc1->proto, 0, &is_filtered);
1640 TCP_TEST ((tconn == 0), "lookup result should be null");
1641 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1643 tc2->lcl_port, tc2->rmt_port,
1644 tc2->proto, 0, &is_filtered);
1645 TCP_TEST ((tconn == 0), "lookup result should be null");
1648 * Re-add and lookup tc2
1650 session_lookup_add_connection (tc1, tc1->s_index);
1651 tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1653 tc2->lcl_port, tc2->rmt_port,
1654 tc2->proto, 0, &is_filtered);
1655 TCP_TEST ((tconn == 0), "lookup result should be null");
1661 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1664 tcp_connection_t *tc0;
1665 ip4_address_t local, remote;
1666 u16 local_port, remote_port;
1667 tcp_main_t *tm = vnet_get_tcp_main ();
1671 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1673 if (unformat (input, "del"))
1675 else if (unformat (input, "add"))
1683 local.as_u32 = clib_host_to_net_u32 (0x06000101);
1684 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1685 local_port = clib_host_to_net_u16 (1234);
1686 remote_port = clib_host_to_net_u16 (11234);
1688 pool_get (tm->connections[0], tc0);
1689 memset (tc0, 0, sizeof (*tc0));
1691 tc0->state = TCP_STATE_ESTABLISHED;
1693 tc0->c_c_index = tc0 - tm->connections[0];
1694 tc0->c_lcl_port = local_port;
1695 tc0->c_rmt_port = remote_port;
1697 tc0->c_thread_index = 0;
1698 tc0->c_lcl_ip4.as_u32 = local.as_u32;
1699 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
1700 tc0->rcv_opts.mss = 1450;
1701 tcp_connection_init_vars (tc0);
1703 TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1705 if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1707 clib_warning ("stream_session_accept failed");
1709 stream_session_accept_notify (&tc0->connection);
1713 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1714 tc0->state = TCP_STATE_CLOSED;
1715 stream_session_disconnect_notify (&tc0->connection);
1721 static clib_error_t *
1722 tcp_test (vlib_main_t * vm,
1723 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1727 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1729 if (unformat (input, "sack"))
1731 res = tcp_test_sack (vm, input);
1733 else if (unformat (input, "fifo"))
1735 res = tcp_test_fifo (vm, input);
1737 else if (unformat (input, "session"))
1739 res = tcp_test_session (vm, input);
1741 else if (unformat (input, "lookup"))
1743 res = tcp_test_lookup (vm, input);
1745 else if (unformat (input, "all"))
1747 if ((res = tcp_test_sack (vm, input)))
1749 if ((res = tcp_test_fifo (vm, input)))
1751 if ((res = tcp_test_lookup (vm, input)))
1760 return clib_error_return (0, "TCP unit test failed");
1765 VLIB_CLI_COMMAND (tcp_test_command, static) =
1768 .short_help = "internal tcp unit tests",
1769 .function = tcp_test,
1774 * fd.io coding-style-patch-verification: ON
1777 * eval: (c-set-style "gnu")