blob: 12579632e3d7ac5b45cb9533965cbaaea75d12fc [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
297int
298tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299{
300 svm_fifo_t *f;
301 u32 fifo_size = 1 << 20;
302 u32 *test_data = 0;
303 u32 offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400304 int i, rv, verbose = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305 u32 data_word, test_data_len;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400306 ooo_segment_t *ooo_seg;
307 u8 *data;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308
Dave Barach1f75cfd2017-04-14 16:46:44 -0400309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
310 {
311 if (unformat (input, "verbose"))
312 verbose = 1;
313 }
314
Florin Coras6cf30ad2017-04-04 23:08:23 -0700315 test_data_len = fifo_size / sizeof (u32);
316 vec_validate (test_data, test_data_len - 1);
317
318 for (i = 0; i < vec_len (test_data); i++)
319 test_data[i] = i;
320
321 f = svm_fifo_create (fifo_size);
322
323 /* Paint fifo data vector with -1's */
324 memset (f->data, 0xFF, test_data_len);
325
326 /* Enqueue an initial (un-dequeued) chunk */
327 rv = svm_fifo_enqueue_nowait (f, 0 /* pid */ ,
328 sizeof (u32), (u8 *) test_data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400329 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
330 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700331
332 /*
333 * Create 3 chunks in the future. The offsets are relative
334 * to the current fifo tail
335 */
336 for (i = 0; i < 3; i++)
337 {
338 offset = (2 * i + 1) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400339 data = (u8 *) (test_data + (2 * i + 1));
340 rv =
341 svm_fifo_enqueue_with_offset (f, 0 /* pid */ , offset, sizeof (u32),
342 data);
343 if (verbose)
344 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
345 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700346 if (rv)
347 {
348 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400349 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350 }
351 }
352
Dave Barach1f75cfd2017-04-14 16:46:44 -0400353 if (verbose)
354 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
355 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
356
357 /* Paint some of missing data backwards */
358 for (i = 3; i > 1; i--)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700359 {
360 offset = (2 * i + 0) * sizeof (u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400361 data = (u8 *) (test_data + (2 * i + 0));
362 rv =
363 svm_fifo_enqueue_with_offset (f, 0 /* pid */ , offset, sizeof (u32),
364 data);
365 if (verbose)
366 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
367 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700368 if (rv)
369 {
370 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400371 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700372 }
373 }
374
Dave Barach1f75cfd2017-04-14 16:46:44 -0400375 if (verbose)
376 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
377 1);
378 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
379 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
380 ooo_seg = svm_fifo_first_ooo_segment (f);
381 TCP_TEST ((ooo_seg->start == 12),
382 "first ooo seg position %u", ooo_seg->start);
383 TCP_TEST ((ooo_seg->length == 16),
384 "first ooo seg length %u", ooo_seg->length);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700385
386 /* Enqueue the missing u32 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400387 rv = svm_fifo_enqueue_nowait (f, 0 /* pid */ , sizeof (u32),
388 (u8 *) (test_data + 2));
389 if (verbose)
390 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
391 1);
392 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
393 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
394 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700395
396 /* Collect results */
397 for (i = 0; i < 7; i++)
398 {
399 rv = svm_fifo_dequeue_nowait (f, 0 /* pid */ , sizeof (u32),
400 (u8 *) & data_word);
401 if (rv != sizeof (u32))
402 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400403 clib_warning ("bytes dequeues %u", rv);
404 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700405 }
406 if (data_word != test_data[i])
407 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400408 clib_warning ("recovered [%d] %d not %d", i, data_word,
409 test_data[i]);
410 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700411 }
412 }
413
Florin Coras6cf30ad2017-04-04 23:08:23 -0700414 svm_fifo_free (f);
415 vec_free (test_data);
416 return 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400417
418err:
419 svm_fifo_free (f);
420 vec_free (test_data);
421 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700422}
423
Dave Barach1f75cfd2017-04-14 16:46:44 -0400424static int
425tcp_test_fifo2 (vlib_main_t * vm)
426{
427 svm_fifo_t *f;
428 u32 fifo_size = 1 << 20;
429 int i, rv, test_data_len;
430 u64 data64;
431 test_pattern_t *tp, *vp, *test_data;
432 ooo_segment_t *ooo_seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700433
Dave Barach1f75cfd2017-04-14 16:46:44 -0400434 test_data = test_pattern;
435 test_data_len = ARRAY_LEN (test_pattern);
436
437 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
438
439 /* Create a fifo */
440 f = svm_fifo_create (fifo_size);
441
442 /* Paint the fifo data vector with -1's */
443 memset (f->data, 0xFF, 1 << 20);
444
445 /*
446 * Try with sorted data
447 */
448 for (i = 0; i < test_data_len; i++)
449 {
450 tp = vp + i;
451 data64 = tp->offset;
452 rv = svm_fifo_enqueue_with_offset (f, 0, tp->offset, tp->len,
453 (u8 *) & data64);
454 }
455
456 /* Expected result: one big fat chunk at offset 4 */
457 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
458 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
459 ooo_seg = svm_fifo_first_ooo_segment (f);
460 TCP_TEST ((ooo_seg->start == 4),
461 "first ooo seg position %u", ooo_seg->start);
462 TCP_TEST ((ooo_seg->length == 2996),
463 "first ooo seg length %u", ooo_seg->length);
464
465 data64 = 0;
466 rv = svm_fifo_enqueue_nowait (f, 0, sizeof (u32), (u8 *) & data64);
467 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
468
469 svm_fifo_free (f);
470 vec_free (vp);
471
472 /*
473 * Now try it again w/ unsorted data...
474 */
475
476 f = svm_fifo_create (fifo_size);
477
478 /* Paint fifo data vector with -1's */
479 memset (f->data, 0xFF, 1 << 20);
480
481 for (i = 0; i < test_data_len; i++)
482 {
483 tp = &test_data[i];
484 data64 = tp->offset;
485 rv = svm_fifo_enqueue_with_offset (f, 0, tp->offset, tp->len,
486 (u8 *) & data64);
487 if (rv)
488 {
489 clib_warning ("enqueue returned %d", rv);
490 }
491 }
492
493 /* Expecting the same result: one big fat chunk at offset 4 */
494 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
495 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
496 ooo_seg = svm_fifo_first_ooo_segment (f);
497 TCP_TEST ((ooo_seg->start == 4),
498 "first ooo seg position %u", ooo_seg->start);
499 TCP_TEST ((ooo_seg->length == 2996),
500 "first ooo seg length %u", ooo_seg->length);
501
502 data64 = 0;
503 rv = svm_fifo_enqueue_nowait (f, 0, sizeof (u32), (u8 *) & data64);
504
505 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
506
507 svm_fifo_free (f);
508
509 return 0;
510}
511
512static int
513tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
514{
515 svm_fifo_t *f;
516 u32 fifo_size = 4 << 10;
517 u32 fifo_initial_offset = 0;
518 u32 total_size = 2 << 10;
519 int overlap = 0;
520 int i, rv;
521 u8 *data_pattern = 0;
522 test_pattern_t *tp, *generate = 0;
523 u32 nsegs = 2;
524 u32 seg_size, length_so_far;
525 u32 current_offset, offset_increment, len_this_chunk;
526 u32 seed = 0xdeaddabe;
527 int verbose = 0;
528 int randomize = 1;
529
530 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
531 {
532 if (unformat (input, "fifo-size %d", &fifo_size))
533 ;
534 else if (unformat (input, "total-size %d", &total_size))
535 ;
536 else if (unformat (input, "verbose"))
537 verbose = 1;
538 else if (unformat (input, "overlap"))
539 overlap = 1;
540 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
541 ;
542 else if (unformat (input, "seed %d", &seed))
543 ;
544 else if (unformat (input, "nsegs %d", &nsegs))
545 ;
546 else if (unformat (input, "no-randomize"))
547 randomize = 0;
548 else
549 {
550 clib_error_t *e = clib_error_return
551 (0, "unknown input `%U'", format_unformat_error, input);
552 clib_error_report (e);
553 return -1;
554 }
555 }
556
557 /*
558 * Generate data
559 */
560 vec_validate (data_pattern, total_size - 1);
561 for (i = 0; i < vec_len (data_pattern); i++)
562 data_pattern[i] = i & 0xff;
563
564 seg_size = total_size / nsegs;
565 length_so_far = 0;
566 current_offset = 1;
567 while (length_so_far < total_size)
568 {
569 vec_add2 (generate, tp, 1);
570 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
571 tp->offset = current_offset;
572 tp->len = len_this_chunk;
573
574 if (overlap && (len_this_chunk == seg_size))
575 do
576 {
577 offset_increment = len_this_chunk
578 % (1 + (random_u32 (&seed) % len_this_chunk));
579 }
580 while (offset_increment == 0);
581 else
582 offset_increment = len_this_chunk;
583
584 current_offset += offset_increment;
585 length_so_far = tp->offset + tp->len;
586 }
587
588 /*
589 * Validate segment list. Only valid for non-overlap cases.
590 */
591 if (overlap == 0)
592 fifo_validate_pattern (vm, generate, vec_len (generate));
593
594 if (verbose)
595 {
596 vlib_cli_output (vm, "raw data pattern:");
597 for (i = 0; i < vec_len (generate); i++)
598 {
599 vlib_cli_output (vm, "[%d] offset %u len %u", i,
600 generate[i].offset, generate[i].len);
601 }
602 }
603
604 /* Randomize data pattern */
605 if (randomize)
606 {
607 for (i = 0; i < vec_len (generate) / 2; i++)
608 {
609 u32 src_index, dst_index;
610 test_pattern_t _tmp, *tmp = &_tmp;
611
612 src_index = random_u32 (&seed) % vec_len (generate);
613 dst_index = random_u32 (&seed) % vec_len (generate);
614
615 tmp[0] = generate[dst_index];
616 generate[dst_index] = generate[src_index];
617 generate[src_index] = tmp[0];
618 }
619 }
620
621 if (verbose)
622 {
623 vlib_cli_output (vm, "randomized data pattern:");
624 for (i = 0; i < vec_len (generate); i++)
625 {
626 vlib_cli_output (vm, "[%d] offset %u len %u", i,
627 generate[i].offset, generate[i].len);
628 }
629 }
630
631 /* Create a fifo */
632 f = svm_fifo_create (fifo_size);
633
634 /* Paint the fifo data vector with -1's */
635 memset (f->data, 0xFF, fifo_size);
636
637 /* manually set head and tail pointers to validate modular arithmetic */
638 f->head = fifo_initial_offset % fifo_size;
639 f->tail = fifo_initial_offset % fifo_size;
640
641 for (i = 0; i < vec_len (generate); i++)
642 {
643 tp = generate + i;
644 rv = svm_fifo_enqueue_with_offset (f, 0, tp->offset, tp->len,
645 (u8 *) data_pattern + tp->offset);
646 }
647
648 /* Expected result: one big fat chunk at offset 1 */
649
650 if (verbose)
651 vlib_cli_output (vm, "fifo before missing link: %U",
652 format_svm_fifo, f, 1 /* verbose */ );
653
654 rv = svm_fifo_enqueue_nowait (f, 0, 1 /* count */ , data_pattern + 0);
655
656 if (verbose)
657 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
658
659 TCP_TEST ((rv == total_size), "retrieved %u expected %u", rv, total_size);
660 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
661 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
662 svm_fifo_free (f);
663 vec_free (data_pattern);
664
665 return 0;
666}
667
668static int
669tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
670{
671 int res = 0;
672
673 /* Run all tests */
674 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
675 {
676 res = tcp_test_fifo1 (vm, input);
677 if (res)
678 return res;
679
680 res = tcp_test_fifo2 (vm);
681 if (res)
682 return res;
683
684 /* Run a number of fifo3 configs */
685 unformat_init_cstring (input, "nsegs 3 overlap seed 123");
686 if (tcp_test_fifo3 (vm, input))
687 return -1;
688 unformat_free (input);
689
690 unformat_init_cstring (input, "nsegs 10");
691 if (tcp_test_fifo3 (vm, input))
692 return -1;
693 unformat_free (input);
694 }
695 else
696 {
697 if (unformat (input, "fifo3"))
698 {
699 res = tcp_test_fifo3 (vm, input);
700 }
701 else if (unformat (input, "fifo2"))
702 {
703 res = tcp_test_fifo2 (vm);
704 }
705 else if (unformat (input, "fifo1"))
706 {
707 res = tcp_test_fifo1 (vm, input);
708 }
709 }
710
711 return res;
712}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700713
Florin Coras6792ec02017-03-13 03:49:51 -0700714static clib_error_t *
715tcp_test (vlib_main_t * vm,
716 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
717{
718 int res = 0;
719
720 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
721 {
722 if (unformat (input, "sack"))
723 {
724 res = tcp_test_sack ();
725 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700726 else if (unformat (input, "fifo"))
727 {
728 res = tcp_test_fifo (vm, input);
729 }
Florin Coras6792ec02017-03-13 03:49:51 -0700730 else
731 {
732 return clib_error_return (0, "unknown input `%U'",
733 format_unformat_error, input);
734 }
735 }
736
737 if (res)
738 {
739 return clib_error_return (0, "TCP unit test failed");
740 }
741 else
742 {
743 return 0;
744 }
745}
746
Florin Coras6cf30ad2017-04-04 23:08:23 -0700747/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700748VLIB_CLI_COMMAND (tcp_test_command, static) =
749{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700750 .path = "test tcp",
751 .short_help = "internal tcp unit tests",
752 .function = tcp_test,
753};
754/* *INDENT-ON* */
755
756
Florin Coras6792ec02017-03-13 03:49:51 -0700757/*
758 * fd.io coding-style-patch-verification: ON
759 *
760 * Local Variables:
761 * eval: (c-set-style "gnu")
762 * End:
763 */