blob: a457ac8f7faf5e2d1caf285d034affd615485c64 [file] [log] [blame]
Florin Coras6792ec02017-03-13 03:49:51 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
Florin Coras6792ec02017-03-13 03:49:51 -070015#include <vnet/tcp/tcp.h>
16
17#define TCP_TEST_I(_cond, _comment, _args...) \
18({ \
19 int _evald = (_cond); \
20 if (!(_evald)) { \
21 fformat(stderr, "FAIL:%d: " _comment "\n", \
22 __LINE__, ##_args); \
23 } else { \
24 fformat(stderr, "PASS:%d: " _comment "\n", \
25 __LINE__, ##_args); \
26 } \
27 _evald; \
28})
29
30#define TCP_TEST(_cond, _comment, _args...) \
31{ \
32 if (!TCP_TEST_I(_cond, _comment, ##_args)) { \
33 return 1; \
34 } \
35}
36
37static int
Florin Coras45d34962017-04-25 00:05:27 -070038tcp_test_sack_rx ()
Florin Coras6792ec02017-03-13 03:49:51 -070039{
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;
44 int i;
45
46 memset (tc, 0, sizeof (*tc));
47
48 tc->snd_una = 0;
49 tc->snd_una_max = 1000;
50 tc->snd_nxt = 1000;
51 tc->opt.flags |= TCP_OPTS_FLAG_SACK;
52 scoreboard_init (&tc->sack_sb);
53
54 for (i = 0; i < 1000 / 100; i++)
55 {
56 block.start = i * 100;
57 block.end = (i + 1) * 100;
58 vec_add1 (sacks, block);
59 }
60
61 /*
62 * Inject even blocks
63 */
64
65 for (i = 0; i < 1000 / 200; i++)
66 {
67 vec_add1 (tc->opt.sacks, sacks[i * 2]);
68 }
69 tc->opt.n_sack_blocks = vec_len (tc->opt.sacks);
70 tcp_rcv_sacks (tc, 0);
71
72 TCP_TEST ((pool_elts (sb->holes) == 5),
73 "scoreboard has %d elements", pool_elts (sb->holes));
74
75 /* First SACK block should be rejected */
76 hole = scoreboard_first_hole (sb);
77 TCP_TEST ((hole->start == 0 && hole->end == 200),
78 "first hole start %u end %u", hole->start, hole->end);
79 hole = scoreboard_last_hole (sb);
80 TCP_TEST ((hole->start == 900 && hole->end == 1000),
81 "last hole start %u end %u", hole->start, hole->end);
82 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
83 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
84 TCP_TEST ((sb->last_sacked_bytes == 400),
85 "last sacked bytes %d", sb->last_sacked_bytes);
86
87 /*
88 * Inject odd blocks
89 */
90
91 vec_reset_length (tc->opt.sacks);
92 for (i = 0; i < 1000 / 200; i++)
93 {
94 vec_add1 (tc->opt.sacks, sacks[i * 2 + 1]);
95 }
96 tc->opt.n_sack_blocks = vec_len (tc->opt.sacks);
97 tcp_rcv_sacks (tc, 0);
98
99 hole = scoreboard_first_hole (sb);
100 TCP_TEST ((pool_elts (sb->holes) == 1),
101 "scoreboard has %d holes", pool_elts (sb->holes));
102 TCP_TEST ((hole->start == 0 && hole->end == 100),
103 "first hole start %u end %u", hole->start, hole->end);
104 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
105 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
106 TCP_TEST ((sb->max_byte_sacked == 1000),
107 "max sacked byte %u", sb->max_byte_sacked);
108 TCP_TEST ((sb->last_sacked_bytes == 500),
109 "last sacked bytes %d", sb->last_sacked_bytes);
110
111 /*
112 * Ack until byte 100, all bytes are now acked + sacked
113 */
114 tcp_rcv_sacks (tc, 100);
115
116 TCP_TEST ((pool_elts (sb->holes) == 0),
117 "scoreboard has %d elements", pool_elts (sb->holes));
118 TCP_TEST ((sb->snd_una_adv == 900),
119 "snd_una_adv after ack %u", sb->snd_una_adv);
120 TCP_TEST ((sb->max_byte_sacked == 1000),
121 "max sacked byte %u", sb->max_byte_sacked);
122 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
123 TCP_TEST ((sb->last_sacked_bytes == 0),
124 "last sacked bytes %d", sb->last_sacked_bytes);
125
126 /*
127 * Add new block
128 */
129
130 vec_reset_length (tc->opt.sacks);
131
132 block.start = 1200;
133 block.end = 1300;
134 vec_add1 (tc->opt.sacks, block);
135
136 tc->snd_una_max = 1500;
137 tc->snd_una = 1000;
138 tc->snd_nxt = 1500;
139 tcp_rcv_sacks (tc, 1000);
140
141 TCP_TEST ((sb->snd_una_adv == 0),
142 "snd_una_adv after ack %u", sb->snd_una_adv);
143 TCP_TEST ((pool_elts (sb->holes) == 2),
144 "scoreboard has %d holes", pool_elts (sb->holes));
145 hole = scoreboard_first_hole (sb);
146 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
147 "first hole start %u end %u", hole->start, hole->end);
148 hole = scoreboard_last_hole (sb);
149 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
150 "last hole start %u end %u", hole->start, hole->end);
151 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
152
153 /*
154 * Ack first hole
155 */
156
157 vec_reset_length (tc->opt.sacks);
158 tcp_rcv_sacks (tc, 1200);
159
160 TCP_TEST ((sb->snd_una_adv == 100),
161 "snd_una_adv after ack %u", sb->snd_una_adv);
162 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
163 TCP_TEST ((pool_elts (sb->holes) == 1),
164 "scoreboard has %d elements", pool_elts (sb->holes));
165
166 /*
167 * Remove all
168 */
169
170 scoreboard_clear (sb);
171 TCP_TEST ((pool_elts (sb->holes) == 0),
172 "number of holes %d", pool_elts (sb->holes));
173 return 0;
174}
175
Florin Coras45d34962017-04-25 00:05:27 -0700176static int
177tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
178{
179 tcp_connection_t _tc, *tc = &_tc;
180 sack_block_t *sacks;
181 int i, verbose = 0;
182
183 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
184 {
185 if (unformat (input, "verbose"))
186 verbose = 1;
187 else
188 {
189 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
190 input);
191 return -1;
192 }
193 }
194
195 memset (tc, 0, sizeof (*tc));
196
197 /*
198 * Add odd sack block pairs
199 */
200 for (i = 1; i < 10; i += 2)
201 {
202 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
203 }
204
205 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
206 vec_len (tc->snd_sacks), 5);
207 TCP_TEST ((tc->snd_sacks[0].start = 900),
208 "first sack block start %u expected %u", tc->snd_sacks[0].start,
209 900);
210
211 /*
212 * Try to add one extra
213 */
214 sacks = vec_dup (tc->snd_sacks);
215
216 tcp_update_sack_list (tc, 1100, 1200);
217 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
218 vec_len (tc->snd_sacks), 5);
219 TCP_TEST ((tc->snd_sacks[0].start == 1100),
220 "first sack block start %u expected %u", tc->snd_sacks[0].start,
221 1100);
222
223 /* restore */
224 vec_free (tc->snd_sacks);
225 tc->snd_sacks = sacks;
226
227 /*
228 * Overlap first 2 segment
229 */
230 tc->rcv_nxt = 300;
231 tcp_update_sack_list (tc, 300, 300);
232 if (verbose)
233 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700234 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700235 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
236 vec_len (tc->snd_sacks), 3);
237 TCP_TEST ((tc->snd_sacks[0].start == 900),
238 "first sack block start %u expected %u", tc->snd_sacks[0].start,
239 500);
240
241 /*
242 * Add a new segment
243 */
244 tcp_update_sack_list (tc, 1100, 1200);
245 if (verbose)
246 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700247 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700248 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
249 vec_len (tc->snd_sacks), 4);
250 TCP_TEST ((tc->snd_sacks[0].start == 1100),
251 "first sack block start %u expected %u", tc->snd_sacks[0].start,
252 1100);
253
254 /*
255 * Join middle segments
256 */
257 tcp_update_sack_list (tc, 800, 900);
258 if (verbose)
259 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700260 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700261
262 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
263 vec_len (tc->snd_sacks), 3);
264 TCP_TEST ((tc->snd_sacks[0].start == 700),
265 "first sack block start %u expected %u", tc->snd_sacks[0].start,
266 1100);
267
268 /*
269 * Advance rcv_nxt to overlap all
270 */
271 tc->rcv_nxt = 1200;
272 tcp_update_sack_list (tc, 1200, 1200);
273 if (verbose)
Florin Corasc28764f2017-04-26 00:08:42 -0700274 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700275 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
276 vec_len (tc->snd_sacks), 0);
277 return 0;
278}
279
280static int
281tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
282{
283 int res = 0;
284
285 /* Run all tests */
286 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
287 {
288 if (tcp_test_sack_tx (vm, input))
289 {
290 return -1;
291 }
292
293 if (tcp_test_sack_rx ())
294 {
295 return -1;
296 }
297 }
298 else
299 {
300 if (unformat (input, "tx"))
301 {
302 res = tcp_test_sack_tx (vm, input);
303 }
304 else if (unformat (input, "rx"))
305 {
306 res = tcp_test_sack_rx ();
307 }
308 }
309
310 return res;
311}
312
313
Dave Barach1f75cfd2017-04-14 16:46:44 -0400314typedef struct
315{
316 u32 offset;
317 u32 len;
318} test_pattern_t;
319
320/* *INDENT-OFF* */
321test_pattern_t test_pattern[] = {
322 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
323 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
324 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
325 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
326 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
327 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
328 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
329 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
330 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
331 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
332 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
333 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
334 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
335 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
336 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
337 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
338 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
339 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
340 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
341 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
342 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
343 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
344 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
345 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
346 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
347 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
348 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
349 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
350 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
351 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
352 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
353 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
354 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
355 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
356 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
357 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
358 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
359 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
360 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
361 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
362 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
363 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
364 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
365 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
366 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
367 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
368 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
369 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
370 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
371 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
372 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
373 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
374 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
375 {2332, 8},
376 /* missing from original data set */
377 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
378};
379/* *INDENT-ON* */
380
381int
382pattern_cmp (const void *arg1, const void *arg2)
383{
384 test_pattern_t *a1 = (test_pattern_t *) arg1;
385 test_pattern_t *a2 = (test_pattern_t *) arg2;
386
387 if (a1->offset < a2->offset)
388 return -1;
389 else if (a1->offset > a2->offset)
390 return 1;
391 return 0;
392}
393
394static u8
395fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
396 u32 pattern_length)
397{
398 test_pattern_t *tp = pattern;
399 int i;
400
401 /* Go through the pattern and make 100% sure it's sane */
402 for (i = 0; i < pattern_length - 1; i++)
403 {
404 if (tp->offset + tp->len != (tp + 1)->offset)
405 {
406 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
407 (tp->offset + tp->len),
408 (tp + 1)->offset - (tp->offset + tp->len));
409 return 0;
410 }
411 tp++;
412 }
413 return 1;
414}
415
416static test_pattern_t *
417fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
418 u32 test_data_len)
419{
420 test_pattern_t *validate_pattern = 0;
421
422 /* Validate, and try segments in order... */
423 vec_validate (validate_pattern, test_data_len - 1);
424 memcpy (validate_pattern, test_data,
425 test_data_len * sizeof (test_pattern_t));
426 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
427 pattern_cmp);
428
429 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
430 return 0;
431
432 return validate_pattern;
433}
434
Florin Corasb59a7052017-04-18 22:07:29 -0700435static svm_fifo_t *
436fifo_prepare (u32 fifo_size)
437{
438 svm_fifo_t *f;
439 f = svm_fifo_create (fifo_size);
440
441 /* Paint fifo data vector with -1's */
442 memset (f->data, 0xFF, fifo_size);
443
444 return f;
445}
446
447static int
448compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
449{
450 int i;
451
452 for (i = start; i < len; i++)
453 {
454 if (data1[i] != data2[i])
455 {
456 *index = i;
457 return 1;
458 }
459 }
460 return 0;
461}
462
Dave Barach1f75cfd2017-04-14 16:46:44 -0400463int
464tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700465{
466 svm_fifo_t *f;
467 u32 fifo_size = 1 << 20;
468 u32 *test_data = 0;
469 u32 offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400470 int i, rv, verbose = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700471 u32 data_word, test_data_len, j;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400472 ooo_segment_t *ooo_seg;
Florin Corasb59a7052017-04-18 22:07:29 -0700473 u8 *data, *s, *data_buf = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700474
Dave Barach1f75cfd2017-04-14 16:46:44 -0400475 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
476 {
477 if (unformat (input, "verbose"))
478 verbose = 1;
479 }
480
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 test_data_len = fifo_size / sizeof (u32);
482 vec_validate (test_data, test_data_len - 1);
483
484 for (i = 0; i < vec_len (test_data); i++)
485 test_data[i] = i;
486
Florin Corasb59a7052017-04-18 22:07:29 -0700487 f = fifo_prepare (fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700488
Florin Corasb59a7052017-04-18 22:07:29 -0700489 /*
490 * Enqueue an initial (un-dequeued) chunk
491 */
Florin Corasa5464812017-04-19 13:00:05 -0700492 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400493 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
494 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700495
496 /*
497 * Create 3 chunks in the future. The offsets are relative
498 * to the current fifo tail
499 */
500 for (i = 0; i < 3; i++)
501 {
502 offset = (2 * i + 1) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400503 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasc28764f2017-04-26 00:08:42 -0700504 if (i == 0)
505 {
506 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
507 rv = rv > 0 ? 0 : rv;
508 }
509 else
510 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400511 if (verbose)
512 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
513 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700514 if (rv)
515 {
516 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400517 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700518 }
519 }
520
Dave Barach1f75cfd2017-04-14 16:46:44 -0400521 if (verbose)
522 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
Florin Corasb59a7052017-04-18 22:07:29 -0700523
Dave Barach1f75cfd2017-04-14 16:46:44 -0400524 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
Florin Corasc28764f2017-04-26 00:08:42 -0700525 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
526 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
527
528 /*
529 * Try adding a completely overlapped segment
530 */
531 offset = 3 * sizeof (u32);
532 data = (u8 *) (test_data + 3);
533 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
534 if (rv)
535 {
536 clib_warning ("enqueue returned %d", rv);
537 goto err;
538 }
539
540 if (verbose)
541 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
542
543 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
544 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Dave Barach1f75cfd2017-04-14 16:46:44 -0400545
Florin Corasb59a7052017-04-18 22:07:29 -0700546 /*
547 * Make sure format functions are not buggy
548 */
549 s = format (0, "%U", format_svm_fifo, f, 2);
550 vec_free (s);
551
552 /*
553 * Paint some of missing data backwards
554 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400555 for (i = 3; i > 1; i--)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700556 {
557 offset = (2 * i + 0) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400558 data = (u8 *) (test_data + (2 * i + 0));
Florin Corasa5464812017-04-19 13:00:05 -0700559 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400560 if (verbose)
561 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
562 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563 if (rv)
564 {
565 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400566 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700567 }
568 }
569
Dave Barach1f75cfd2017-04-14 16:46:44 -0400570 if (verbose)
571 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
572 1);
573 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
574 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
575 ooo_seg = svm_fifo_first_ooo_segment (f);
576 TCP_TEST ((ooo_seg->start == 12),
577 "first ooo seg position %u", ooo_seg->start);
578 TCP_TEST ((ooo_seg->length == 16),
579 "first ooo seg length %u", ooo_seg->length);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700580
Florin Corasb59a7052017-04-18 22:07:29 -0700581 /*
582 * Enqueue the missing u32
583 */
Florin Corasa5464812017-04-19 13:00:05 -0700584 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
Dave Barach1f75cfd2017-04-14 16:46:44 -0400585 if (verbose)
586 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
587 1);
588 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
589 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
590 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700591
Florin Corasb59a7052017-04-18 22:07:29 -0700592 /*
593 * Collect results
594 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700595 for (i = 0; i < 7; i++)
596 {
Florin Corasa5464812017-04-19 13:00:05 -0700597 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700598 if (rv != sizeof (u32))
599 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400600 clib_warning ("bytes dequeues %u", rv);
601 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700602 }
603 if (data_word != test_data[i])
604 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400605 clib_warning ("recovered [%d] %d not %d", i, data_word,
606 test_data[i]);
607 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700608 }
609 }
610
Florin Corasb59a7052017-04-18 22:07:29 -0700611 /*
612 * Test segment overlaps: last ooo segment overlaps all
613 */
614 svm_fifo_free (f);
615 f = fifo_prepare (fifo_size);
616
617 for (i = 0; i < 4; i++)
618 {
619 offset = (2 * i + 1) * sizeof (u32);
620 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700621 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700622 if (verbose)
623 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
624 offset + sizeof (u32));
625 if (rv)
626 {
627 clib_warning ("enqueue returned %d", rv);
628 goto err;
629 }
630 }
631
Florin Corasa5464812017-04-19 13:00:05 -0700632 rv = svm_fifo_enqueue_with_offset (f, 8, 21, data);
Florin Corasb59a7052017-04-18 22:07:29 -0700633 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
634 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
635 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
636
637 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700638 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700639 if (compare_data (data_buf, data, 8, vec_len (data), &j))
640 {
641 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
642 }
643 vec_reset_length (data_buf);
644
645 /*
646 * Test segment overlaps: enqueue and overlap ooo segments
647 */
648 svm_fifo_free (f);
649 f = fifo_prepare (fifo_size);
650
651 for (i = 0; i < 4; i++)
652 {
653 offset = (2 * i + 1) * sizeof (u32);
654 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700655 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700656 if (verbose)
657 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
658 offset + sizeof (u32));
659 if (rv)
660 {
661 clib_warning ("enqueue returned %d", rv);
662 goto err;
663 }
664 }
665
Florin Corasa5464812017-04-19 13:00:05 -0700666 rv = svm_fifo_enqueue_nowait (f, 29, data);
Florin Corasb59a7052017-04-18 22:07:29 -0700667 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
668 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
669 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
670
671 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700672 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700673 if (compare_data (data_buf, data, 0, vec_len (data), &j))
674 {
675 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
676 }
677
678 vec_free (data_buf);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700679 svm_fifo_free (f);
680 vec_free (test_data);
Florin Corasb59a7052017-04-18 22:07:29 -0700681
Florin Coras6cf30ad2017-04-04 23:08:23 -0700682 return 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400683
684err:
685 svm_fifo_free (f);
686 vec_free (test_data);
687 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700688}
689
Dave Barach1f75cfd2017-04-14 16:46:44 -0400690static int
691tcp_test_fifo2 (vlib_main_t * vm)
692{
693 svm_fifo_t *f;
694 u32 fifo_size = 1 << 20;
695 int i, rv, test_data_len;
696 u64 data64;
697 test_pattern_t *tp, *vp, *test_data;
698 ooo_segment_t *ooo_seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700699
Dave Barach1f75cfd2017-04-14 16:46:44 -0400700 test_data = test_pattern;
701 test_data_len = ARRAY_LEN (test_pattern);
702
703 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
704
705 /* Create a fifo */
Florin Corasb59a7052017-04-18 22:07:29 -0700706 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400707
708 /*
709 * Try with sorted data
710 */
711 for (i = 0; i < test_data_len; i++)
712 {
713 tp = vp + i;
714 data64 = tp->offset;
Florin Coras82b13a82017-04-25 11:58:06 -0700715 svm_fifo_enqueue_with_offset (f, tp->offset, tp->len, (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400716 }
717
718 /* Expected result: one big fat chunk at offset 4 */
719 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
720 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
721 ooo_seg = svm_fifo_first_ooo_segment (f);
722 TCP_TEST ((ooo_seg->start == 4),
723 "first ooo seg position %u", ooo_seg->start);
724 TCP_TEST ((ooo_seg->length == 2996),
725 "first ooo seg length %u", ooo_seg->length);
726
727 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700728 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400729 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
730
731 svm_fifo_free (f);
732 vec_free (vp);
733
734 /*
735 * Now try it again w/ unsorted data...
736 */
737
Florin Corasb59a7052017-04-18 22:07:29 -0700738 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400739
740 for (i = 0; i < test_data_len; i++)
741 {
742 tp = &test_data[i];
743 data64 = tp->offset;
Florin Corasa5464812017-04-19 13:00:05 -0700744 rv = svm_fifo_enqueue_with_offset (f, tp->offset, tp->len,
Dave Barach1f75cfd2017-04-14 16:46:44 -0400745 (u8 *) & data64);
746 if (rv)
747 {
748 clib_warning ("enqueue returned %d", rv);
749 }
750 }
751
752 /* Expecting the same result: one big fat chunk at offset 4 */
753 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
754 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
755 ooo_seg = svm_fifo_first_ooo_segment (f);
756 TCP_TEST ((ooo_seg->start == 4),
757 "first ooo seg position %u", ooo_seg->start);
758 TCP_TEST ((ooo_seg->length == 2996),
759 "first ooo seg length %u", ooo_seg->length);
760
761 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700762 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400763
764 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
765
766 svm_fifo_free (f);
767
768 return 0;
769}
770
771static int
772tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
773{
774 svm_fifo_t *f;
775 u32 fifo_size = 4 << 10;
776 u32 fifo_initial_offset = 0;
777 u32 total_size = 2 << 10;
Florin Corasb59a7052017-04-18 22:07:29 -0700778 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
779 u8 *data_pattern = 0, *data_buf = 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400780 test_pattern_t *tp, *generate = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700781 u32 nsegs = 2, seg_size, length_so_far;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400782 u32 current_offset, offset_increment, len_this_chunk;
Florin Corasb59a7052017-04-18 22:07:29 -0700783 u32 seed = 0xdeaddabe, j;
784 int i, rv;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400785
786 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
787 {
788 if (unformat (input, "fifo-size %d", &fifo_size))
789 ;
790 else if (unformat (input, "total-size %d", &total_size))
791 ;
792 else if (unformat (input, "verbose"))
793 verbose = 1;
794 else if (unformat (input, "overlap"))
795 overlap = 1;
796 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
797 ;
798 else if (unformat (input, "seed %d", &seed))
799 ;
800 else if (unformat (input, "nsegs %d", &nsegs))
801 ;
802 else if (unformat (input, "no-randomize"))
803 randomize = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700804 else if (unformat (input, "in-seq-all"))
805 in_seq_all = 1;
806 else if (unformat (input, "drop"))
807 drop = 1;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400808 else
809 {
810 clib_error_t *e = clib_error_return
811 (0, "unknown input `%U'", format_unformat_error, input);
812 clib_error_report (e);
813 return -1;
814 }
815 }
816
Florin Corasb59a7052017-04-18 22:07:29 -0700817 if (total_size > fifo_size)
818 {
819 clib_warning ("total_size %d greater than fifo size %d", total_size,
820 fifo_size);
821 return -1;
822 }
823 if (overlap && randomize == 0)
824 {
825 clib_warning ("Can't enqueue in-order with overlap");
826 return -1;
827 }
828
Dave Barach1f75cfd2017-04-14 16:46:44 -0400829 /*
830 * Generate data
831 */
832 vec_validate (data_pattern, total_size - 1);
833 for (i = 0; i < vec_len (data_pattern); i++)
834 data_pattern[i] = i & 0xff;
835
Florin Corasb59a7052017-04-18 22:07:29 -0700836 /*
837 * Generate segments
838 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400839 seg_size = total_size / nsegs;
840 length_so_far = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700841 current_offset = randomize;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400842 while (length_so_far < total_size)
843 {
844 vec_add2 (generate, tp, 1);
845 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
846 tp->offset = current_offset;
847 tp->len = len_this_chunk;
848
849 if (overlap && (len_this_chunk == seg_size))
850 do
851 {
852 offset_increment = len_this_chunk
853 % (1 + (random_u32 (&seed) % len_this_chunk));
854 }
855 while (offset_increment == 0);
856 else
857 offset_increment = len_this_chunk;
858
859 current_offset += offset_increment;
860 length_so_far = tp->offset + tp->len;
861 }
862
863 /*
864 * Validate segment list. Only valid for non-overlap cases.
865 */
866 if (overlap == 0)
867 fifo_validate_pattern (vm, generate, vec_len (generate));
868
869 if (verbose)
870 {
871 vlib_cli_output (vm, "raw data pattern:");
872 for (i = 0; i < vec_len (generate); i++)
873 {
874 vlib_cli_output (vm, "[%d] offset %u len %u", i,
875 generate[i].offset, generate[i].len);
876 }
877 }
878
879 /* Randomize data pattern */
880 if (randomize)
881 {
882 for (i = 0; i < vec_len (generate) / 2; i++)
883 {
884 u32 src_index, dst_index;
885 test_pattern_t _tmp, *tmp = &_tmp;
886
887 src_index = random_u32 (&seed) % vec_len (generate);
888 dst_index = random_u32 (&seed) % vec_len (generate);
889
890 tmp[0] = generate[dst_index];
891 generate[dst_index] = generate[src_index];
892 generate[src_index] = tmp[0];
893 }
Florin Corasb59a7052017-04-18 22:07:29 -0700894 if (verbose)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400895 {
Florin Corasb59a7052017-04-18 22:07:29 -0700896 vlib_cli_output (vm, "randomized data pattern:");
897 for (i = 0; i < vec_len (generate); i++)
898 {
899 vlib_cli_output (vm, "[%d] offset %u len %u", i,
900 generate[i].offset, generate[i].len);
901 }
Dave Barach1f75cfd2017-04-14 16:46:44 -0400902 }
903 }
904
Florin Corasb59a7052017-04-18 22:07:29 -0700905 /*
906 * Create a fifo and add segments
907 */
908 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400909
910 /* manually set head and tail pointers to validate modular arithmetic */
Florin Corasb59a7052017-04-18 22:07:29 -0700911 fifo_initial_offset = fifo_initial_offset % fifo_size;
912 f->head = fifo_initial_offset;
913 f->tail = fifo_initial_offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400914
Florin Corasc28764f2017-04-26 00:08:42 -0700915 for (i = !randomize; i < vec_len (generate); i++)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400916 {
917 tp = generate + i;
Florin Coras82b13a82017-04-25 11:58:06 -0700918 svm_fifo_enqueue_with_offset (f, fifo_initial_offset + tp->offset,
919 tp->len,
920 (u8 *) data_pattern + tp->offset);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400921 }
922
Florin Corasc28764f2017-04-26 00:08:42 -0700923 /* Add the first segment in order for non random data */
924 if (!randomize)
925 svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
926
Florin Corasb59a7052017-04-18 22:07:29 -0700927 /*
928 * Expected result: one big fat chunk at offset 1 if randomize == 1
929 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400930
931 if (verbose)
932 vlib_cli_output (vm, "fifo before missing link: %U",
933 format_svm_fifo, f, 1 /* verbose */ );
934
Florin Corasb59a7052017-04-18 22:07:29 -0700935 /*
936 * Add the missing byte if segments were randomized
937 */
938 if (randomize)
939 {
940 u32 bytes_to_enq = 1;
941 if (in_seq_all)
942 bytes_to_enq = total_size;
Florin Corasa5464812017-04-19 13:00:05 -0700943 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400944
Florin Corasb59a7052017-04-18 22:07:29 -0700945 if (verbose)
946 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400947
Florin Corasb59a7052017-04-18 22:07:29 -0700948 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
949 total_size);
950
951 }
952
953 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
954 svm_fifo_number_ooo_segments (f));
955
956 /*
957 * Test if peeked data is the same as original data
958 */
959 vec_validate (data_buf, vec_len (data_pattern));
Florin Corasa5464812017-04-19 13:00:05 -0700960 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700961 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
962 {
963 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
964 data_pattern[j]);
965 }
966 vec_reset_length (data_buf);
967
968 /*
969 * Dequeue or drop all data
970 */
971 if (drop)
972 {
Florin Corasa5464812017-04-19 13:00:05 -0700973 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
Florin Corasb59a7052017-04-18 22:07:29 -0700974 }
975 else
976 {
Florin Corasa5464812017-04-19 13:00:05 -0700977 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700978 if (compare_data
979 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
980 {
981 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
982 data_pattern[j]);
983 }
984 }
985
986 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
987 svm_fifo_max_dequeue (f));
988
Dave Barach1f75cfd2017-04-14 16:46:44 -0400989 svm_fifo_free (f);
990 vec_free (data_pattern);
Florin Corasb59a7052017-04-18 22:07:29 -0700991 vec_free (data_buf);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400992
993 return 0;
994}
995
996static int
Florin Corasc28764f2017-04-26 00:08:42 -0700997tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
998{
999 svm_fifo_t *f;
1000 u32 fifo_size = 6 << 10;
1001 u32 fifo_initial_offset = 1000000000;
1002 u32 test_n_bytes = 5000, j;
1003 u8 *test_data = 0, *data_buf = 0;
1004 int i, rv, verbose = 0;
1005
1006 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1007 {
1008 if (unformat (input, "verbose"))
1009 verbose = 1;
1010 else
1011 {
1012 clib_error_t *e = clib_error_return
1013 (0, "unknown input `%U'", format_unformat_error, input);
1014 clib_error_report (e);
1015 return -1;
1016 }
1017 }
1018
1019 /*
1020 * Create a fifo and add segments
1021 */
1022 f = fifo_prepare (fifo_size);
1023
1024 /* Set head and tail pointers */
1025 fifo_initial_offset = fifo_initial_offset % fifo_size;
1026 svm_fifo_init_pointers (f, fifo_initial_offset);
1027
1028 vec_validate (test_data, test_n_bytes - 1);
1029 for (i = 0; i < vec_len (test_data); i++)
1030 test_data[i] = i;
1031
1032 for (i = test_n_bytes - 1; i > 0; i--)
1033 {
1034 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i,
1035 sizeof (u8), &test_data[i]);
1036 if (verbose)
1037 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1038 if (rv)
1039 {
1040 clib_warning ("enqueue returned %d", rv);
1041 svm_fifo_free (f);
1042 vec_free (test_data);
1043 return -1;
1044 }
1045 }
1046
1047 svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1048
1049 vec_validate (data_buf, vec_len (test_data));
1050
1051 svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1052 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1053 if (rv)
1054 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1055 test_data[j]);
1056 TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1057
1058 svm_fifo_free (f);
1059 vec_free (test_data);
1060 return 0;
1061}
1062
1063static int
Dave Barach1f75cfd2017-04-14 16:46:44 -04001064tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1065{
1066 int res = 0;
Florin Corasb59a7052017-04-18 22:07:29 -07001067 char *str;
Dave Barach1f75cfd2017-04-14 16:46:44 -04001068
1069 /* Run all tests */
1070 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1071 {
1072 res = tcp_test_fifo1 (vm, input);
1073 if (res)
1074 return res;
1075
1076 res = tcp_test_fifo2 (vm);
1077 if (res)
1078 return res;
1079
Florin Corasb59a7052017-04-18 22:07:29 -07001080 /*
1081 * Run a number of fifo3 configs
1082 */
1083 str = "nsegs 10 overlap seed 123";
1084 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001085 if (tcp_test_fifo3 (vm, input))
1086 return -1;
1087 unformat_free (input);
1088
Florin Corasb59a7052017-04-18 22:07:29 -07001089 str = "nsegs 10 overlap seed 123 in-seq-all";
1090 unformat_init_cstring (input, str);
1091 if (tcp_test_fifo3 (vm, input))
1092 return -1;
1093 unformat_free (input);
1094
1095 str = "nsegs 10 overlap seed 123 initial-offset 3917";
1096 unformat_init_cstring (input, str);
1097 if (tcp_test_fifo3 (vm, input))
1098 return -1;
1099 unformat_free (input);
1100
1101 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1102 unformat_init_cstring (input, str);
1103 if (tcp_test_fifo3 (vm, input))
1104 return -1;
1105 unformat_free (input);
1106
1107 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1108 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001109 if (tcp_test_fifo3 (vm, input))
1110 return -1;
1111 unformat_free (input);
1112 }
1113 else
1114 {
1115 if (unformat (input, "fifo3"))
1116 {
1117 res = tcp_test_fifo3 (vm, input);
1118 }
1119 else if (unformat (input, "fifo2"))
1120 {
1121 res = tcp_test_fifo2 (vm);
1122 }
1123 else if (unformat (input, "fifo1"))
1124 {
1125 res = tcp_test_fifo1 (vm, input);
1126 }
Florin Corasc28764f2017-04-26 00:08:42 -07001127 else if (unformat (input, "fifo4"))
1128 {
1129 res = tcp_test_fifo4 (vm, input);
1130 }
Dave Barach1f75cfd2017-04-14 16:46:44 -04001131 }
1132
1133 return res;
1134}
Florin Coras6cf30ad2017-04-04 23:08:23 -07001135
Dave Barach63681512017-04-20 17:50:39 -04001136static int
1137tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1138{
1139 int rv = 0;
1140 tcp_connection_t *tc0;
1141 u8 sst = SESSION_TYPE_IP4_TCP;
1142 ip4_address_t local, remote;
1143 u16 local_port, remote_port;
1144 tcp_main_t *tm = vnet_get_tcp_main ();
1145 int is_add = 1;
1146
1147
1148 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1149 {
1150 if (unformat (input, "del"))
1151 is_add = 0;
1152 else if (unformat (input, "add"))
1153 is_add = 1;
1154 else
1155 break;
1156 }
1157
1158 if (is_add)
1159 {
1160 local.as_u32 = clib_host_to_net_u32 (0x06000101);
1161 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1162 local_port = clib_host_to_net_u16 (1234);
1163 remote_port = clib_host_to_net_u16 (11234);
1164
1165 pool_get (tm->connections[0], tc0);
1166 memset (tc0, 0, sizeof (*tc0));
1167
1168 tc0->state = TCP_STATE_ESTABLISHED;
1169 tc0->rcv_las = 1;
1170 tc0->c_c_index = tc0 - tm->connections[0];
1171 tc0->c_lcl_port = local_port;
1172 tc0->c_rmt_port = remote_port;
1173 tc0->c_is_ip4 = 1;
1174 tc0->c_thread_index = 0;
1175 tc0->c_lcl_ip4.as_u32 = local.as_u32;
1176 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
1177 tc0->opt.mss = 1450;
1178 tcp_connection_init_vars (tc0);
1179
1180 TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1181
1182 if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1183 sst, 0 /* notify */ ))
1184 clib_warning ("stream_session_accept failed");
1185
1186 stream_session_accept_notify (&tc0->connection);
1187 }
1188 else
1189 {
1190 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1191 tc0->state = TCP_STATE_CLOSED;
1192 stream_session_disconnect_notify (&tc0->connection);
1193 }
1194
1195 return rv;
1196}
1197
Florin Coras6792ec02017-03-13 03:49:51 -07001198static clib_error_t *
1199tcp_test (vlib_main_t * vm,
1200 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1201{
1202 int res = 0;
1203
1204 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1205 {
1206 if (unformat (input, "sack"))
1207 {
Florin Coras45d34962017-04-25 00:05:27 -07001208 res = tcp_test_sack (vm, input);
Florin Coras6792ec02017-03-13 03:49:51 -07001209 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001210 else if (unformat (input, "fifo"))
1211 {
1212 res = tcp_test_fifo (vm, input);
1213 }
Dave Barach63681512017-04-20 17:50:39 -04001214 else if (unformat (input, "session"))
Florin Coras6792ec02017-03-13 03:49:51 -07001215 {
Dave Barach63681512017-04-20 17:50:39 -04001216 res = tcp_test_session (vm, input);
Florin Coras6792ec02017-03-13 03:49:51 -07001217 }
Dave Barach63681512017-04-20 17:50:39 -04001218 else
1219 break;
Florin Coras6792ec02017-03-13 03:49:51 -07001220 }
1221
1222 if (res)
1223 {
1224 return clib_error_return (0, "TCP unit test failed");
1225 }
1226 else
1227 {
1228 return 0;
1229 }
1230}
1231
Florin Coras6cf30ad2017-04-04 23:08:23 -07001232/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -07001233VLIB_CLI_COMMAND (tcp_test_command, static) =
1234{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001235 .path = "test tcp",
1236 .short_help = "internal tcp unit tests",
1237 .function = tcp_test,
1238};
1239/* *INDENT-ON* */
1240
1241
Florin Coras6792ec02017-03-13 03:49:51 -07001242/*
1243 * fd.io coding-style-patch-verification: ON
1244 *
1245 * Local Variables:
1246 * eval: (c-set-style "gnu")
1247 * End:
1248 */