blob: 0146154b742f1166ea8beba40c8ef6284ee39d33 [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
38tcp_test_sack ()
39{
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
Dave Barach1f75cfd2017-04-14 16:46:44 -0400176typedef struct
177{
178 u32 offset;
179 u32 len;
180} test_pattern_t;
181
182/* *INDENT-OFF* */
183test_pattern_t test_pattern[] = {
184 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
185 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
186 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
187 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
188 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
189 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
190 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
191 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
192 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
193 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
194 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
195 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
196 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
197 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
198 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
199 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
200 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
201 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
202 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
203 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
204 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
205 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
206 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
207 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
208 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
209 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
210 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
211 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
212 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
213 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
214 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
215 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
216 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
217 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
218 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
219 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
220 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
221 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
222 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
223 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
224 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
225 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
226 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
227 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
228 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
229 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
230 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
231 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
232 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
233 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
234 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
235 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
236 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
237 {2332, 8},
238 /* missing from original data set */
239 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
240};
241/* *INDENT-ON* */
242
243int
244pattern_cmp (const void *arg1, const void *arg2)
245{
246 test_pattern_t *a1 = (test_pattern_t *) arg1;
247 test_pattern_t *a2 = (test_pattern_t *) arg2;
248
249 if (a1->offset < a2->offset)
250 return -1;
251 else if (a1->offset > a2->offset)
252 return 1;
253 return 0;
254}
255
256static u8
257fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
258 u32 pattern_length)
259{
260 test_pattern_t *tp = pattern;
261 int i;
262
263 /* Go through the pattern and make 100% sure it's sane */
264 for (i = 0; i < pattern_length - 1; i++)
265 {
266 if (tp->offset + tp->len != (tp + 1)->offset)
267 {
268 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
269 (tp->offset + tp->len),
270 (tp + 1)->offset - (tp->offset + tp->len));
271 return 0;
272 }
273 tp++;
274 }
275 return 1;
276}
277
278static test_pattern_t *
279fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
280 u32 test_data_len)
281{
282 test_pattern_t *validate_pattern = 0;
283
284 /* Validate, and try segments in order... */
285 vec_validate (validate_pattern, test_data_len - 1);
286 memcpy (validate_pattern, test_data,
287 test_data_len * sizeof (test_pattern_t));
288 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
289 pattern_cmp);
290
291 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
292 return 0;
293
294 return validate_pattern;
295}
296
Florin Corasb59a7052017-04-18 22:07:29 -0700297static svm_fifo_t *
298fifo_prepare (u32 fifo_size)
299{
300 svm_fifo_t *f;
301 f = svm_fifo_create (fifo_size);
302
303 /* Paint fifo data vector with -1's */
304 memset (f->data, 0xFF, fifo_size);
305
306 return f;
307}
308
309static int
310compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
311{
312 int i;
313
314 for (i = start; i < len; i++)
315 {
316 if (data1[i] != data2[i])
317 {
318 *index = i;
319 return 1;
320 }
321 }
322 return 0;
323}
324
Dave Barach1f75cfd2017-04-14 16:46:44 -0400325int
326tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700327{
328 svm_fifo_t *f;
329 u32 fifo_size = 1 << 20;
330 u32 *test_data = 0;
331 u32 offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400332 int i, rv, verbose = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700333 u32 data_word, test_data_len, j;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400334 ooo_segment_t *ooo_seg;
Florin Corasb59a7052017-04-18 22:07:29 -0700335 u8 *data, *s, *data_buf = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700336
Dave Barach1f75cfd2017-04-14 16:46:44 -0400337 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
338 {
339 if (unformat (input, "verbose"))
340 verbose = 1;
341 }
342
Florin Coras6cf30ad2017-04-04 23:08:23 -0700343 test_data_len = fifo_size / sizeof (u32);
344 vec_validate (test_data, test_data_len - 1);
345
346 for (i = 0; i < vec_len (test_data); i++)
347 test_data[i] = i;
348
Florin Corasb59a7052017-04-18 22:07:29 -0700349 f = fifo_prepare (fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350
Florin Corasb59a7052017-04-18 22:07:29 -0700351 /*
352 * Enqueue an initial (un-dequeued) chunk
353 */
Florin Corasa5464812017-04-19 13:00:05 -0700354 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400355 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
356 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700357
358 /*
359 * Create 3 chunks in the future. The offsets are relative
360 * to the current fifo tail
361 */
362 for (i = 0; i < 3; i++)
363 {
364 offset = (2 * i + 1) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400365 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700366 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400367 if (verbose)
368 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
369 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700370 if (rv)
371 {
372 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400373 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700374 }
375 }
376
Dave Barach1f75cfd2017-04-14 16:46:44 -0400377 if (verbose)
378 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
Florin Corasb59a7052017-04-18 22:07:29 -0700379
Dave Barach1f75cfd2017-04-14 16:46:44 -0400380 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
381
Florin Corasb59a7052017-04-18 22:07:29 -0700382 /*
383 * Make sure format functions are not buggy
384 */
385 s = format (0, "%U", format_svm_fifo, f, 2);
386 vec_free (s);
387
388 /*
389 * Paint some of missing data backwards
390 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400391 for (i = 3; i > 1; i--)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700392 {
393 offset = (2 * i + 0) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400394 data = (u8 *) (test_data + (2 * i + 0));
Florin Corasa5464812017-04-19 13:00:05 -0700395 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400396 if (verbose)
397 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
398 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700399 if (rv)
400 {
401 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400402 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700403 }
404 }
405
Dave Barach1f75cfd2017-04-14 16:46:44 -0400406 if (verbose)
407 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
408 1);
409 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
410 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
411 ooo_seg = svm_fifo_first_ooo_segment (f);
412 TCP_TEST ((ooo_seg->start == 12),
413 "first ooo seg position %u", ooo_seg->start);
414 TCP_TEST ((ooo_seg->length == 16),
415 "first ooo seg length %u", ooo_seg->length);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416
Florin Corasb59a7052017-04-18 22:07:29 -0700417 /*
418 * Enqueue the missing u32
419 */
Florin Corasa5464812017-04-19 13:00:05 -0700420 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
Dave Barach1f75cfd2017-04-14 16:46:44 -0400421 if (verbose)
422 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
423 1);
424 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
425 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
426 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427
Florin Corasb59a7052017-04-18 22:07:29 -0700428 /*
429 * Collect results
430 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700431 for (i = 0; i < 7; i++)
432 {
Florin Corasa5464812017-04-19 13:00:05 -0700433 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700434 if (rv != sizeof (u32))
435 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400436 clib_warning ("bytes dequeues %u", rv);
437 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700438 }
439 if (data_word != test_data[i])
440 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400441 clib_warning ("recovered [%d] %d not %d", i, data_word,
442 test_data[i]);
443 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700444 }
445 }
446
Florin Corasb59a7052017-04-18 22:07:29 -0700447 /*
448 * Test segment overlaps: last ooo segment overlaps all
449 */
450 svm_fifo_free (f);
451 f = fifo_prepare (fifo_size);
452
453 for (i = 0; i < 4; i++)
454 {
455 offset = (2 * i + 1) * sizeof (u32);
456 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700457 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700458 if (verbose)
459 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
460 offset + sizeof (u32));
461 if (rv)
462 {
463 clib_warning ("enqueue returned %d", rv);
464 goto err;
465 }
466 }
467
Florin Corasa5464812017-04-19 13:00:05 -0700468 rv = svm_fifo_enqueue_with_offset (f, 8, 21, data);
Florin Corasb59a7052017-04-18 22:07:29 -0700469 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
470 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
471 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
472
473 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700474 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700475 if (compare_data (data_buf, data, 8, vec_len (data), &j))
476 {
477 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
478 }
479 vec_reset_length (data_buf);
480
481 /*
482 * Test segment overlaps: enqueue and overlap ooo segments
483 */
484 svm_fifo_free (f);
485 f = fifo_prepare (fifo_size);
486
487 for (i = 0; i < 4; i++)
488 {
489 offset = (2 * i + 1) * sizeof (u32);
490 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700491 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700492 if (verbose)
493 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
494 offset + sizeof (u32));
495 if (rv)
496 {
497 clib_warning ("enqueue returned %d", rv);
498 goto err;
499 }
500 }
501
Florin Corasa5464812017-04-19 13:00:05 -0700502 rv = svm_fifo_enqueue_nowait (f, 29, data);
Florin Corasb59a7052017-04-18 22:07:29 -0700503 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
504 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
505 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
506
507 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700508 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700509 if (compare_data (data_buf, data, 0, vec_len (data), &j))
510 {
511 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
512 }
513
514 vec_free (data_buf);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700515 svm_fifo_free (f);
516 vec_free (test_data);
Florin Corasb59a7052017-04-18 22:07:29 -0700517
Florin Coras6cf30ad2017-04-04 23:08:23 -0700518 return 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400519
520err:
521 svm_fifo_free (f);
522 vec_free (test_data);
523 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700524}
525
Dave Barach1f75cfd2017-04-14 16:46:44 -0400526static int
527tcp_test_fifo2 (vlib_main_t * vm)
528{
529 svm_fifo_t *f;
530 u32 fifo_size = 1 << 20;
531 int i, rv, test_data_len;
532 u64 data64;
533 test_pattern_t *tp, *vp, *test_data;
534 ooo_segment_t *ooo_seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700535
Dave Barach1f75cfd2017-04-14 16:46:44 -0400536 test_data = test_pattern;
537 test_data_len = ARRAY_LEN (test_pattern);
538
539 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
540
541 /* Create a fifo */
Florin Corasb59a7052017-04-18 22:07:29 -0700542 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400543
544 /*
545 * Try with sorted data
546 */
547 for (i = 0; i < test_data_len; i++)
548 {
549 tp = vp + i;
550 data64 = tp->offset;
Florin Corasa5464812017-04-19 13:00:05 -0700551 rv = svm_fifo_enqueue_with_offset (f, tp->offset, tp->len,
Dave Barach1f75cfd2017-04-14 16:46:44 -0400552 (u8 *) & data64);
553 }
554
555 /* Expected result: one big fat chunk at offset 4 */
556 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
557 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
558 ooo_seg = svm_fifo_first_ooo_segment (f);
559 TCP_TEST ((ooo_seg->start == 4),
560 "first ooo seg position %u", ooo_seg->start);
561 TCP_TEST ((ooo_seg->length == 2996),
562 "first ooo seg length %u", ooo_seg->length);
563
564 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700565 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400566 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
567
568 svm_fifo_free (f);
569 vec_free (vp);
570
571 /*
572 * Now try it again w/ unsorted data...
573 */
574
Florin Corasb59a7052017-04-18 22:07:29 -0700575 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400576
577 for (i = 0; i < test_data_len; i++)
578 {
579 tp = &test_data[i];
580 data64 = tp->offset;
Florin Corasa5464812017-04-19 13:00:05 -0700581 rv = svm_fifo_enqueue_with_offset (f, tp->offset, tp->len,
Dave Barach1f75cfd2017-04-14 16:46:44 -0400582 (u8 *) & data64);
583 if (rv)
584 {
585 clib_warning ("enqueue returned %d", rv);
586 }
587 }
588
589 /* Expecting the same result: one big fat chunk at offset 4 */
590 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
591 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
592 ooo_seg = svm_fifo_first_ooo_segment (f);
593 TCP_TEST ((ooo_seg->start == 4),
594 "first ooo seg position %u", ooo_seg->start);
595 TCP_TEST ((ooo_seg->length == 2996),
596 "first ooo seg length %u", ooo_seg->length);
597
598 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700599 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400600
601 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
602
603 svm_fifo_free (f);
604
605 return 0;
606}
607
608static int
609tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
610{
611 svm_fifo_t *f;
612 u32 fifo_size = 4 << 10;
613 u32 fifo_initial_offset = 0;
614 u32 total_size = 2 << 10;
Florin Corasb59a7052017-04-18 22:07:29 -0700615 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
616 u8 *data_pattern = 0, *data_buf = 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400617 test_pattern_t *tp, *generate = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700618 u32 nsegs = 2, seg_size, length_so_far;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400619 u32 current_offset, offset_increment, len_this_chunk;
Florin Corasb59a7052017-04-18 22:07:29 -0700620 u32 seed = 0xdeaddabe, j;
621 int i, rv;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400622
623 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
624 {
625 if (unformat (input, "fifo-size %d", &fifo_size))
626 ;
627 else if (unformat (input, "total-size %d", &total_size))
628 ;
629 else if (unformat (input, "verbose"))
630 verbose = 1;
631 else if (unformat (input, "overlap"))
632 overlap = 1;
633 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
634 ;
635 else if (unformat (input, "seed %d", &seed))
636 ;
637 else if (unformat (input, "nsegs %d", &nsegs))
638 ;
639 else if (unformat (input, "no-randomize"))
640 randomize = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700641 else if (unformat (input, "in-seq-all"))
642 in_seq_all = 1;
643 else if (unformat (input, "drop"))
644 drop = 1;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400645 else
646 {
647 clib_error_t *e = clib_error_return
648 (0, "unknown input `%U'", format_unformat_error, input);
649 clib_error_report (e);
650 return -1;
651 }
652 }
653
Florin Corasb59a7052017-04-18 22:07:29 -0700654 if (total_size > fifo_size)
655 {
656 clib_warning ("total_size %d greater than fifo size %d", total_size,
657 fifo_size);
658 return -1;
659 }
660 if (overlap && randomize == 0)
661 {
662 clib_warning ("Can't enqueue in-order with overlap");
663 return -1;
664 }
665
Dave Barach1f75cfd2017-04-14 16:46:44 -0400666 /*
667 * Generate data
668 */
669 vec_validate (data_pattern, total_size - 1);
670 for (i = 0; i < vec_len (data_pattern); i++)
671 data_pattern[i] = i & 0xff;
672
Florin Corasb59a7052017-04-18 22:07:29 -0700673 /*
674 * Generate segments
675 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400676 seg_size = total_size / nsegs;
677 length_so_far = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700678 current_offset = randomize;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400679 while (length_so_far < total_size)
680 {
681 vec_add2 (generate, tp, 1);
682 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
683 tp->offset = current_offset;
684 tp->len = len_this_chunk;
685
686 if (overlap && (len_this_chunk == seg_size))
687 do
688 {
689 offset_increment = len_this_chunk
690 % (1 + (random_u32 (&seed) % len_this_chunk));
691 }
692 while (offset_increment == 0);
693 else
694 offset_increment = len_this_chunk;
695
696 current_offset += offset_increment;
697 length_so_far = tp->offset + tp->len;
698 }
699
700 /*
701 * Validate segment list. Only valid for non-overlap cases.
702 */
703 if (overlap == 0)
704 fifo_validate_pattern (vm, generate, vec_len (generate));
705
706 if (verbose)
707 {
708 vlib_cli_output (vm, "raw data pattern:");
709 for (i = 0; i < vec_len (generate); i++)
710 {
711 vlib_cli_output (vm, "[%d] offset %u len %u", i,
712 generate[i].offset, generate[i].len);
713 }
714 }
715
716 /* Randomize data pattern */
717 if (randomize)
718 {
719 for (i = 0; i < vec_len (generate) / 2; i++)
720 {
721 u32 src_index, dst_index;
722 test_pattern_t _tmp, *tmp = &_tmp;
723
724 src_index = random_u32 (&seed) % vec_len (generate);
725 dst_index = random_u32 (&seed) % vec_len (generate);
726
727 tmp[0] = generate[dst_index];
728 generate[dst_index] = generate[src_index];
729 generate[src_index] = tmp[0];
730 }
Florin Corasb59a7052017-04-18 22:07:29 -0700731 if (verbose)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400732 {
Florin Corasb59a7052017-04-18 22:07:29 -0700733 vlib_cli_output (vm, "randomized data pattern:");
734 for (i = 0; i < vec_len (generate); i++)
735 {
736 vlib_cli_output (vm, "[%d] offset %u len %u", i,
737 generate[i].offset, generate[i].len);
738 }
Dave Barach1f75cfd2017-04-14 16:46:44 -0400739 }
740 }
741
Florin Corasb59a7052017-04-18 22:07:29 -0700742 /*
743 * Create a fifo and add segments
744 */
745 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400746
747 /* manually set head and tail pointers to validate modular arithmetic */
Florin Corasb59a7052017-04-18 22:07:29 -0700748 fifo_initial_offset = fifo_initial_offset % fifo_size;
749 f->head = fifo_initial_offset;
750 f->tail = fifo_initial_offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400751
752 for (i = 0; i < vec_len (generate); i++)
753 {
754 tp = generate + i;
Florin Corasa5464812017-04-19 13:00:05 -0700755 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset
Florin Corasb59a7052017-04-18 22:07:29 -0700756 + tp->offset, tp->len,
Dave Barach1f75cfd2017-04-14 16:46:44 -0400757 (u8 *) data_pattern + tp->offset);
758 }
759
Florin Corasb59a7052017-04-18 22:07:29 -0700760 /*
761 * Expected result: one big fat chunk at offset 1 if randomize == 1
762 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400763
764 if (verbose)
765 vlib_cli_output (vm, "fifo before missing link: %U",
766 format_svm_fifo, f, 1 /* verbose */ );
767
Florin Corasb59a7052017-04-18 22:07:29 -0700768 /*
769 * Add the missing byte if segments were randomized
770 */
771 if (randomize)
772 {
773 u32 bytes_to_enq = 1;
774 if (in_seq_all)
775 bytes_to_enq = total_size;
Florin Corasa5464812017-04-19 13:00:05 -0700776 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400777
Florin Corasb59a7052017-04-18 22:07:29 -0700778 if (verbose)
779 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400780
Florin Corasb59a7052017-04-18 22:07:29 -0700781 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
782 total_size);
783
784 }
785
786 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
787 svm_fifo_number_ooo_segments (f));
788
789 /*
790 * Test if peeked data is the same as original data
791 */
792 vec_validate (data_buf, vec_len (data_pattern));
Florin Corasa5464812017-04-19 13:00:05 -0700793 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700794 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
795 {
796 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
797 data_pattern[j]);
798 }
799 vec_reset_length (data_buf);
800
801 /*
802 * Dequeue or drop all data
803 */
804 if (drop)
805 {
Florin Corasa5464812017-04-19 13:00:05 -0700806 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
Florin Corasb59a7052017-04-18 22:07:29 -0700807 }
808 else
809 {
Florin Corasa5464812017-04-19 13:00:05 -0700810 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700811 if (compare_data
812 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
813 {
814 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
815 data_pattern[j]);
816 }
817 }
818
819 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
820 svm_fifo_max_dequeue (f));
821
Dave Barach1f75cfd2017-04-14 16:46:44 -0400822 svm_fifo_free (f);
823 vec_free (data_pattern);
Florin Corasb59a7052017-04-18 22:07:29 -0700824 vec_free (data_buf);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400825
826 return 0;
827}
828
829static int
830tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
831{
832 int res = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700833 char *str;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400834
835 /* Run all tests */
836 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
837 {
838 res = tcp_test_fifo1 (vm, input);
839 if (res)
840 return res;
841
842 res = tcp_test_fifo2 (vm);
843 if (res)
844 return res;
845
Florin Corasb59a7052017-04-18 22:07:29 -0700846 /*
847 * Run a number of fifo3 configs
848 */
849 str = "nsegs 10 overlap seed 123";
850 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400851 if (tcp_test_fifo3 (vm, input))
852 return -1;
853 unformat_free (input);
854
Florin Corasb59a7052017-04-18 22:07:29 -0700855 str = "nsegs 10 overlap seed 123 in-seq-all";
856 unformat_init_cstring (input, str);
857 if (tcp_test_fifo3 (vm, input))
858 return -1;
859 unformat_free (input);
860
861 str = "nsegs 10 overlap seed 123 initial-offset 3917";
862 unformat_init_cstring (input, str);
863 if (tcp_test_fifo3 (vm, input))
864 return -1;
865 unformat_free (input);
866
867 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
868 unformat_init_cstring (input, str);
869 if (tcp_test_fifo3 (vm, input))
870 return -1;
871 unformat_free (input);
872
873 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
874 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400875 if (tcp_test_fifo3 (vm, input))
876 return -1;
877 unformat_free (input);
878 }
879 else
880 {
881 if (unformat (input, "fifo3"))
882 {
883 res = tcp_test_fifo3 (vm, input);
884 }
885 else if (unformat (input, "fifo2"))
886 {
887 res = tcp_test_fifo2 (vm);
888 }
889 else if (unformat (input, "fifo1"))
890 {
891 res = tcp_test_fifo1 (vm, input);
892 }
893 }
894
895 return res;
896}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700897
Florin Coras6792ec02017-03-13 03:49:51 -0700898static clib_error_t *
899tcp_test (vlib_main_t * vm,
900 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
901{
902 int res = 0;
903
904 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
905 {
906 if (unformat (input, "sack"))
907 {
908 res = tcp_test_sack ();
909 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700910 else if (unformat (input, "fifo"))
911 {
912 res = tcp_test_fifo (vm, input);
913 }
Florin Coras6792ec02017-03-13 03:49:51 -0700914 else
915 {
916 return clib_error_return (0, "unknown input `%U'",
917 format_unformat_error, input);
918 }
919 }
920
921 if (res)
922 {
923 return clib_error_return (0, "TCP unit test failed");
924 }
925 else
926 {
927 return 0;
928 }
929}
930
Florin Coras6cf30ad2017-04-04 23:08:23 -0700931/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700932VLIB_CLI_COMMAND (tcp_test_command, static) =
933{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700934 .path = "test tcp",
935 .short_help = "internal tcp unit tests",
936 .function = tcp_test,
937};
938/* *INDENT-ON* */
939
940
Florin Coras6792ec02017-03-13 03:49:51 -0700941/*
942 * fd.io coding-style-patch-verification: ON
943 *
944 * Local Variables:
945 * eval: (c-set-style "gnu")
946 * End:
947 */