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 tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
40 tcp_connection_t _tc, *tc = &_tc;
41 sack_scoreboard_t *sb = &tc->sack_sb;
42 sack_block_t *sacks = 0, block;
43 sack_scoreboard_hole_t *hole;
46 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
48 if (unformat (input, "verbose"))
52 memset (tc, 0, sizeof (*tc));
55 tc->snd_una_max = 1000;
57 tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
58 scoreboard_init (&tc->sack_sb);
60 for (i = 0; i < 1000 / 100; i++)
62 block.start = i * 100;
63 block.end = (i + 1) * 100;
64 vec_add1 (sacks, block);
71 for (i = 0; i < 1000 / 200; i++)
73 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
75 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
76 tcp_rcv_sacks (tc, 0);
79 vlib_cli_output (vm, "sb after even blocks:\n%U", format_tcp_scoreboard,
82 TCP_TEST ((pool_elts (sb->holes) == 5),
83 "scoreboard has %d elements", pool_elts (sb->holes));
85 /* First SACK block should be rejected */
86 hole = scoreboard_first_hole (sb);
87 TCP_TEST ((hole->start == 0 && hole->end == 200),
88 "first hole start %u end %u", hole->start, hole->end);
89 hole = scoreboard_last_hole (sb);
90 TCP_TEST ((hole->start == 900 && hole->end == 1000),
91 "last hole start %u end %u", hole->start, hole->end);
92 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
93 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
94 TCP_TEST ((sb->last_sacked_bytes == 400),
95 "last sacked bytes %d", sb->last_sacked_bytes);
96 TCP_TEST ((sb->high_sacked == 900), "max byte sacked %u", sb->high_sacked);
101 vec_reset_length (tc->rcv_opts.sacks);
102 for (i = 0; i < 1000 / 200; i++)
104 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
106 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
107 tcp_rcv_sacks (tc, 0);
110 vlib_cli_output (vm, "sb after odd blocks:\n%U", format_tcp_scoreboard,
113 hole = scoreboard_first_hole (sb);
114 TCP_TEST ((pool_elts (sb->holes) == 1),
115 "scoreboard has %d holes", pool_elts (sb->holes));
116 TCP_TEST ((hole->start == 0 && hole->end == 100),
117 "first hole start %u end %u", hole->start, hole->end);
118 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
119 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
120 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
121 TCP_TEST ((sb->last_sacked_bytes == 500),
122 "last sacked bytes %d", sb->last_sacked_bytes);
125 * Ack until byte 100, all bytes are now acked + sacked
127 tcp_rcv_sacks (tc, 100);
129 vlib_cli_output (vm, "ack until byte 100:\n%U", format_tcp_scoreboard,
132 TCP_TEST ((pool_elts (sb->holes) == 0),
133 "scoreboard has %d elements", pool_elts (sb->holes));
134 TCP_TEST ((sb->snd_una_adv == 900),
135 "snd_una_adv after ack %u", sb->snd_una_adv);
136 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
137 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
138 TCP_TEST ((sb->last_sacked_bytes == 0),
139 "last sacked bytes %d", sb->last_sacked_bytes);
145 vec_reset_length (tc->rcv_opts.sacks);
149 vec_add1 (tc->rcv_opts.sacks, block);
152 vlib_cli_output (vm, "add [1200, 1300]:\n%U", format_tcp_scoreboard, sb);
153 tc->snd_una_max = 1500;
156 tcp_rcv_sacks (tc, 1000);
159 vlib_cli_output (vm, "sb snd_una_max 1500, snd_una 1000:\n%U",
160 format_tcp_scoreboard, sb);
162 TCP_TEST ((sb->snd_una_adv == 0),
163 "snd_una_adv after ack %u", sb->snd_una_adv);
164 TCP_TEST ((pool_elts (sb->holes) == 2),
165 "scoreboard has %d holes", pool_elts (sb->holes));
166 hole = scoreboard_first_hole (sb);
167 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
168 "first hole start %u end %u", hole->start, hole->end);
169 TCP_TEST ((sb->snd_una_adv == 0),
170 "snd_una_adv after ack %u", sb->snd_una_adv);
171 TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
172 hole = scoreboard_last_hole (sb);
173 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
174 "last hole start %u end %u", hole->start, hole->end);
175 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
181 vec_reset_length (tc->rcv_opts.sacks);
182 tcp_rcv_sacks (tc, 1200);
185 vlib_cli_output (vm, "sb ack up to byte 1200:\n%U", format_tcp_scoreboard,
188 TCP_TEST ((sb->snd_una_adv == 100),
189 "snd_una_adv after ack %u", sb->snd_una_adv);
190 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
191 TCP_TEST ((pool_elts (sb->holes) == 1),
192 "scoreboard has %d elements", pool_elts (sb->holes));
195 * Add some more blocks and then remove all
197 vec_reset_length (tc->rcv_opts.sacks);
198 for (i = 0; i < 5; i++)
200 block.start = i * 100 + 1200;
201 block.end = (i + 1) * 100 + 1200;
202 vec_add1 (tc->rcv_opts.sacks, block);
204 tcp_rcv_sacks (tc, 1900);
206 scoreboard_clear (sb);
208 vlib_cli_output (vm, "sb cleared all:\n%U", format_tcp_scoreboard, sb);
210 TCP_TEST ((pool_elts (sb->holes) == 0),
211 "number of holes %d", pool_elts (sb->holes));
212 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
213 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
216 * Re-inject odd blocks and ack them all
220 tc->snd_una_max = 1000;
222 for (i = 0; i < 5; i++)
224 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
226 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
227 tcp_rcv_sacks (tc, 0);
229 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
230 format_tcp_scoreboard, sb);
232 tcp_rcv_sacks (tc, 950);
235 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
236 format_tcp_scoreboard, sb);
238 TCP_TEST ((pool_elts (sb->holes) == 0),
239 "scoreboard has %d elements", pool_elts (sb->holes));
240 TCP_TEST ((sb->snd_una_adv == 50), "snd_una_adv %u", sb->snd_una_adv);
241 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
242 TCP_TEST ((sb->last_sacked_bytes == 0),
243 "last sacked bytes %d", sb->last_sacked_bytes);
249 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
251 tcp_connection_t _tc, *tc = &_tc;
255 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
257 if (unformat (input, "verbose"))
261 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
267 memset (tc, 0, sizeof (*tc));
270 * Add odd sack block pairs
272 for (i = 1; i < 10; i += 2)
274 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
277 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
278 vec_len (tc->snd_sacks), 5);
279 TCP_TEST ((tc->snd_sacks[0].start = 900),
280 "first sack block start %u expected %u", tc->snd_sacks[0].start,
284 * Try to add one extra
286 sacks = vec_dup (tc->snd_sacks);
288 tcp_update_sack_list (tc, 1100, 1200);
289 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
290 vec_len (tc->snd_sacks), 5);
291 TCP_TEST ((tc->snd_sacks[0].start == 1100),
292 "first sack block start %u expected %u", tc->snd_sacks[0].start,
296 vec_free (tc->snd_sacks);
297 tc->snd_sacks = sacks;
300 * Overlap first 2 segment
303 tcp_update_sack_list (tc, 300, 300);
305 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
306 format_tcp_sacks, tc);
307 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
308 vec_len (tc->snd_sacks), 3);
309 TCP_TEST ((tc->snd_sacks[0].start == 900),
310 "first sack block start %u expected %u", tc->snd_sacks[0].start,
316 tcp_update_sack_list (tc, 1100, 1200);
318 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
319 format_tcp_sacks, tc);
320 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
321 vec_len (tc->snd_sacks), 4);
322 TCP_TEST ((tc->snd_sacks[0].start == 1100),
323 "first sack block start %u expected %u", tc->snd_sacks[0].start,
327 * Join middle segments
329 tcp_update_sack_list (tc, 800, 900);
331 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
332 format_tcp_sacks, tc);
334 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
335 vec_len (tc->snd_sacks), 3);
336 TCP_TEST ((tc->snd_sacks[0].start == 700),
337 "first sack block start %u expected %u", tc->snd_sacks[0].start,
341 * Advance rcv_nxt to overlap all
344 tcp_update_sack_list (tc, 1200, 1200);
346 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
347 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
348 vec_len (tc->snd_sacks), 0);
353 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
358 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
360 if (tcp_test_sack_tx (vm, input))
365 if (tcp_test_sack_rx (vm, input))
372 if (unformat (input, "tx"))
374 res = tcp_test_sack_tx (vm, input);
376 else if (unformat (input, "rx"))
378 res = tcp_test_sack_rx (vm, input);
393 test_pattern_t test_pattern[] = {
394 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
395 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
396 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
397 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
398 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
399 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
400 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
401 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
402 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
403 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
404 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
405 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
406 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
407 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
408 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
409 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
410 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
411 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
412 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
413 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
414 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
415 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
416 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
417 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
418 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
419 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
420 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
421 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
422 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
423 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
424 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
425 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
426 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
427 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
428 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
429 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
430 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
431 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
432 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
433 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
434 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
435 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
436 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
437 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
438 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
439 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
440 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
441 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
442 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
443 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
444 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
445 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
446 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
448 /* missing from original data set */
449 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
454 pattern_cmp (const void *arg1, const void *arg2)
456 test_pattern_t *a1 = (test_pattern_t *) arg1;
457 test_pattern_t *a2 = (test_pattern_t *) arg2;
459 if (a1->offset < a2->offset)
461 else if (a1->offset > a2->offset)
467 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
470 test_pattern_t *tp = pattern;
473 /* Go through the pattern and make 100% sure it's sane */
474 for (i = 0; i < pattern_length - 1; i++)
476 if (tp->offset + tp->len != (tp + 1)->offset)
478 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
479 (tp->offset + tp->len),
480 (tp + 1)->offset - (tp->offset + tp->len));
488 static test_pattern_t *
489 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
492 test_pattern_t *validate_pattern = 0;
494 /* Validate, and try segments in order... */
495 vec_validate (validate_pattern, test_data_len - 1);
496 memcpy (validate_pattern, test_data,
497 test_data_len * sizeof (test_pattern_t));
498 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
501 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
504 return validate_pattern;
508 fifo_prepare (u32 fifo_size)
511 f = svm_fifo_create (fifo_size);
513 /* Paint fifo data vector with -1's */
514 memset (f->data, 0xFF, fifo_size);
520 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
524 for (i = start; i < len; i++)
526 if (data1[i] != data2[i])
536 tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
539 u32 fifo_size = 1 << 20;
542 int i, rv, verbose = 0;
543 u32 data_word, test_data_len, j;
544 ooo_segment_t *ooo_seg;
545 u8 *data, *s, *data_buf = 0;
547 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
549 if (unformat (input, "verbose"))
553 test_data_len = fifo_size / sizeof (u32);
554 vec_validate (test_data, test_data_len - 1);
556 for (i = 0; i < vec_len (test_data); i++)
559 f = fifo_prepare (fifo_size);
562 * Enqueue an initial (un-dequeued) chunk
564 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
565 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
566 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
569 * Create 3 chunks in the future. The offsets are relative
570 * to the current fifo tail
572 for (i = 0; i < 3; i++)
574 offset = (2 * i + 1) * sizeof (u32);
575 data = (u8 *) (test_data + (2 * i + 1));
578 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
579 rv = rv > 0 ? 0 : rv;
582 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
584 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
585 offset + sizeof (u32));
588 clib_warning ("enqueue returned %d", rv);
594 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
596 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
597 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
598 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
601 * Try adding a completely overlapped segment
603 offset = 3 * sizeof (u32);
604 data = (u8 *) (test_data + 3);
605 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
608 clib_warning ("enqueue returned %d", rv);
613 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
615 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
616 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
619 * Make sure format functions are not buggy
621 s = format (0, "%U", format_svm_fifo, f, 2);
625 * Paint some of missing data backwards
627 for (i = 3; i > 1; i--)
629 offset = (2 * i + 0) * sizeof (u32);
630 data = (u8 *) (test_data + (2 * i + 0));
631 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
633 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
634 offset + sizeof (u32));
637 clib_warning ("enqueue returned %d", rv);
643 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
645 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
646 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
647 ooo_seg = svm_fifo_first_ooo_segment (f);
648 TCP_TEST ((ooo_seg->start == 12),
649 "first ooo seg position %u", ooo_seg->start);
650 TCP_TEST ((ooo_seg->length == 16),
651 "first ooo seg length %u", ooo_seg->length);
654 * Enqueue the missing u32
656 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
658 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
660 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
661 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
662 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
667 for (i = 0; i < 7; i++)
669 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
670 if (rv != sizeof (u32))
672 clib_warning ("bytes dequeues %u", rv);
675 if (data_word != test_data[i])
677 clib_warning ("recovered [%d] %d not %d", i, data_word,
684 * Test segment overlaps: last ooo segment overlaps all
687 f = fifo_prepare (fifo_size);
689 for (i = 0; i < 4; i++)
691 offset = (2 * i + 1) * sizeof (u32);
692 data = (u8 *) (test_data + (2 * i + 1));
693 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
695 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
696 offset + sizeof (u32));
699 clib_warning ("enqueue returned %d", rv);
704 rv = svm_fifo_enqueue_with_offset (f, 8, 21, data);
705 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
706 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
707 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
709 vec_validate (data_buf, vec_len (data));
710 svm_fifo_peek (f, 0, vec_len (data), data_buf);
711 if (compare_data (data_buf, data, 8, vec_len (data), &j))
713 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
715 vec_reset_length (data_buf);
718 * Test segment overlaps: enqueue and overlap ooo segments
721 f = fifo_prepare (fifo_size);
723 for (i = 0; i < 4; i++)
725 offset = (2 * i + 1) * sizeof (u32);
726 data = (u8 *) (test_data + (2 * i + 1));
727 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
729 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
730 offset + sizeof (u32));
733 clib_warning ("enqueue returned %d", rv);
738 rv = svm_fifo_enqueue_nowait (f, 29, data);
739 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
740 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
741 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
743 vec_validate (data_buf, vec_len (data));
744 svm_fifo_peek (f, 0, vec_len (data), data_buf);
745 if (compare_data (data_buf, data, 0, vec_len (data), &j))
747 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
750 /* Try to peek beyond the data */
751 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
752 TCP_TEST ((rv == 0), "peeked %u expected 0", rv);
756 vec_free (test_data);
762 vec_free (test_data);
767 tcp_test_fifo2 (vlib_main_t * vm)
770 u32 fifo_size = 1 << 20;
771 int i, rv, test_data_len;
773 test_pattern_t *tp, *vp, *test_data;
774 ooo_segment_t *ooo_seg;
776 test_data = test_pattern;
777 test_data_len = ARRAY_LEN (test_pattern);
779 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
782 f = fifo_prepare (fifo_size);
785 * Try with sorted data
787 for (i = 0; i < test_data_len; i++)
791 svm_fifo_enqueue_with_offset (f, tp->offset, tp->len, (u8 *) & data64);
794 /* Expected result: one big fat chunk at offset 4 */
795 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
796 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
797 ooo_seg = svm_fifo_first_ooo_segment (f);
798 TCP_TEST ((ooo_seg->start == 4),
799 "first ooo seg position %u", ooo_seg->start);
800 TCP_TEST ((ooo_seg->length == 2996),
801 "first ooo seg length %u", ooo_seg->length);
804 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
805 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
811 * Now try it again w/ unsorted data...
814 f = fifo_prepare (fifo_size);
816 for (i = 0; i < test_data_len; i++)
820 rv = svm_fifo_enqueue_with_offset (f, tp->offset, tp->len,
824 clib_warning ("enqueue returned %d", rv);
828 /* Expecting the same result: one big fat chunk at offset 4 */
829 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
830 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
831 ooo_seg = svm_fifo_first_ooo_segment (f);
832 TCP_TEST ((ooo_seg->start == 4),
833 "first ooo seg position %u", ooo_seg->start);
834 TCP_TEST ((ooo_seg->length == 2996),
835 "first ooo seg length %u", ooo_seg->length);
838 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
840 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
848 tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
851 u32 fifo_size = 4 << 10;
852 u32 fifo_initial_offset = 0;
853 u32 total_size = 2 << 10;
854 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
855 u8 *data_pattern = 0, *data_buf = 0;
856 test_pattern_t *tp, *generate = 0;
857 u32 nsegs = 2, seg_size, length_so_far;
858 u32 current_offset, offset_increment, len_this_chunk;
859 u32 seed = 0xdeaddabe, j;
862 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
864 if (unformat (input, "fifo-size %d", &fifo_size))
866 else if (unformat (input, "total-size %d", &total_size))
868 else if (unformat (input, "verbose"))
870 else if (unformat (input, "overlap"))
872 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
874 else if (unformat (input, "seed %d", &seed))
876 else if (unformat (input, "nsegs %d", &nsegs))
878 else if (unformat (input, "no-randomize"))
880 else if (unformat (input, "in-seq-all"))
882 else if (unformat (input, "drop"))
886 clib_error_t *e = clib_error_return
887 (0, "unknown input `%U'", format_unformat_error, input);
888 clib_error_report (e);
893 if (total_size > fifo_size)
895 clib_warning ("total_size %d greater than fifo size %d", total_size,
899 if (overlap && randomize == 0)
901 clib_warning ("Can't enqueue in-order with overlap");
908 vec_validate (data_pattern, total_size - 1);
909 for (i = 0; i < vec_len (data_pattern); i++)
910 data_pattern[i] = i & 0xff;
915 seg_size = total_size / nsegs;
917 current_offset = randomize;
918 while (length_so_far < total_size)
920 vec_add2 (generate, tp, 1);
921 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
922 tp->offset = current_offset;
923 tp->len = len_this_chunk;
925 if (overlap && (len_this_chunk == seg_size))
928 offset_increment = len_this_chunk
929 % (1 + (random_u32 (&seed) % len_this_chunk));
931 while (offset_increment == 0);
933 offset_increment = len_this_chunk;
935 current_offset += offset_increment;
936 length_so_far = tp->offset + tp->len;
940 * Validate segment list. Only valid for non-overlap cases.
943 fifo_validate_pattern (vm, generate, vec_len (generate));
947 vlib_cli_output (vm, "raw data pattern:");
948 for (i = 0; i < vec_len (generate); i++)
950 vlib_cli_output (vm, "[%d] offset %u len %u", i,
951 generate[i].offset, generate[i].len);
955 /* Randomize data pattern */
958 for (i = 0; i < vec_len (generate) / 2; i++)
960 u32 src_index, dst_index;
961 test_pattern_t _tmp, *tmp = &_tmp;
963 src_index = random_u32 (&seed) % vec_len (generate);
964 dst_index = random_u32 (&seed) % vec_len (generate);
966 tmp[0] = generate[dst_index];
967 generate[dst_index] = generate[src_index];
968 generate[src_index] = tmp[0];
972 vlib_cli_output (vm, "randomized data pattern:");
973 for (i = 0; i < vec_len (generate); i++)
975 vlib_cli_output (vm, "[%d] offset %u len %u", i,
976 generate[i].offset, generate[i].len);
982 * Create a fifo and add segments
984 f = fifo_prepare (fifo_size);
986 /* manually set head and tail pointers to validate modular arithmetic */
987 fifo_initial_offset = fifo_initial_offset % fifo_size;
988 f->head = fifo_initial_offset;
989 f->tail = fifo_initial_offset;
991 for (i = !randomize; i < vec_len (generate); i++)
994 svm_fifo_enqueue_with_offset (f, fifo_initial_offset + tp->offset,
996 (u8 *) data_pattern + tp->offset);
999 /* Add the first segment in order for non random data */
1001 svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
1004 * Expected result: one big fat chunk at offset 1 if randomize == 1
1008 vlib_cli_output (vm, "fifo before missing link: %U",
1009 format_svm_fifo, f, 1 /* verbose */ );
1012 * Add the missing byte if segments were randomized
1016 u32 bytes_to_enq = 1;
1018 bytes_to_enq = total_size;
1019 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
1022 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
1024 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
1029 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
1030 svm_fifo_number_ooo_segments (f));
1033 * Test if peeked data is the same as original data
1035 vec_validate (data_buf, vec_len (data_pattern));
1036 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
1037 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1039 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1042 vec_reset_length (data_buf);
1045 * Dequeue or drop all data
1049 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
1053 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
1055 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1057 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1062 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
1063 svm_fifo_max_dequeue (f));
1066 vec_free (data_pattern);
1067 vec_free (data_buf);
1073 tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
1076 u32 fifo_size = 6 << 10;
1077 u32 fifo_initial_offset = 1000000000;
1078 u32 test_n_bytes = 5000, j;
1079 u8 *test_data = 0, *data_buf = 0;
1080 int i, rv, verbose = 0;
1082 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1084 if (unformat (input, "verbose"))
1088 clib_error_t *e = clib_error_return
1089 (0, "unknown input `%U'", format_unformat_error, input);
1090 clib_error_report (e);
1096 * Create a fifo and add segments
1098 f = fifo_prepare (fifo_size);
1100 /* Set head and tail pointers */
1101 fifo_initial_offset = fifo_initial_offset % fifo_size;
1102 svm_fifo_init_pointers (f, fifo_initial_offset);
1104 vec_validate (test_data, test_n_bytes - 1);
1105 for (i = 0; i < vec_len (test_data); i++)
1108 for (i = test_n_bytes - 1; i > 0; i--)
1110 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i,
1111 sizeof (u8), &test_data[i]);
1113 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1116 clib_warning ("enqueue returned %d", rv);
1118 vec_free (test_data);
1123 svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1125 vec_validate (data_buf, vec_len (test_data));
1127 svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1128 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1130 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1132 TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1135 vec_free (test_data);
1140 tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1146 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1148 res = tcp_test_fifo1 (vm, input);
1152 res = tcp_test_fifo2 (vm);
1157 * Run a number of fifo3 configs
1159 str = "nsegs 10 overlap seed 123";
1160 unformat_init_cstring (input, str);
1161 if (tcp_test_fifo3 (vm, input))
1163 unformat_free (input);
1165 str = "nsegs 10 overlap seed 123 in-seq-all";
1166 unformat_init_cstring (input, str);
1167 if (tcp_test_fifo3 (vm, input))
1169 unformat_free (input);
1171 str = "nsegs 10 overlap seed 123 initial-offset 3917";
1172 unformat_init_cstring (input, str);
1173 if (tcp_test_fifo3 (vm, input))
1175 unformat_free (input);
1177 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1178 unformat_init_cstring (input, str);
1179 if (tcp_test_fifo3 (vm, input))
1181 unformat_free (input);
1183 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1184 unformat_init_cstring (input, str);
1185 if (tcp_test_fifo3 (vm, input))
1187 unformat_free (input);
1191 if (unformat (input, "fifo3"))
1193 res = tcp_test_fifo3 (vm, input);
1195 else if (unformat (input, "fifo2"))
1197 res = tcp_test_fifo2 (vm);
1199 else if (unformat (input, "fifo1"))
1201 res = tcp_test_fifo1 (vm, input);
1203 else if (unformat (input, "fifo4"))
1205 res = tcp_test_fifo4 (vm, input);
1213 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1216 tcp_connection_t *tc0;
1217 u8 sst = SESSION_TYPE_IP4_TCP;
1218 ip4_address_t local, remote;
1219 u16 local_port, remote_port;
1220 tcp_main_t *tm = vnet_get_tcp_main ();
1224 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1226 if (unformat (input, "del"))
1228 else if (unformat (input, "add"))
1236 local.as_u32 = clib_host_to_net_u32 (0x06000101);
1237 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1238 local_port = clib_host_to_net_u16 (1234);
1239 remote_port = clib_host_to_net_u16 (11234);
1241 pool_get (tm->connections[0], tc0);
1242 memset (tc0, 0, sizeof (*tc0));
1244 tc0->state = TCP_STATE_ESTABLISHED;
1246 tc0->c_c_index = tc0 - tm->connections[0];
1247 tc0->c_lcl_port = local_port;
1248 tc0->c_rmt_port = remote_port;
1250 tc0->c_thread_index = 0;
1251 tc0->c_lcl_ip4.as_u32 = local.as_u32;
1252 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
1253 tc0->rcv_opts.mss = 1450;
1254 tcp_connection_init_vars (tc0);
1256 TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1258 if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1259 sst, 0 /* notify */ ))
1260 clib_warning ("stream_session_accept failed");
1262 stream_session_accept_notify (&tc0->connection);
1266 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1267 tc0->state = TCP_STATE_CLOSED;
1268 stream_session_disconnect_notify (&tc0->connection);
1274 static clib_error_t *
1275 tcp_test (vlib_main_t * vm,
1276 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1280 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1282 if (unformat (input, "sack"))
1284 res = tcp_test_sack (vm, input);
1286 else if (unformat (input, "fifo"))
1288 res = tcp_test_fifo (vm, input);
1290 else if (unformat (input, "session"))
1292 res = tcp_test_session (vm, input);
1300 return clib_error_return (0, "TCP unit test failed");
1309 VLIB_CLI_COMMAND (tcp_test_command, static) =
1312 .short_help = "internal tcp unit tests",
1313 .function = tcp_test,
1319 * fd.io coding-style-patch-verification: ON
1322 * eval: (c-set-style "gnu")