blob: 510deb4fec053b272cc5424be4d46bb9e3fe6803 [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 Coras06d11012017-05-17 14:21:51 -070038tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
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;
Florin Coras06d11012017-05-17 14:21:51 -070044 int i, verbose = 0;
45
46 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
47 {
48 if (unformat (input, "verbose"))
49 verbose = 1;
50 }
Florin Coras6792ec02017-03-13 03:49:51 -070051
52 memset (tc, 0, sizeof (*tc));
53
54 tc->snd_una = 0;
55 tc->snd_una_max = 1000;
56 tc->snd_nxt = 1000;
Florin Coras93992a92017-05-24 18:03:56 -070057 tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
Florin Coras6792ec02017-03-13 03:49:51 -070058 scoreboard_init (&tc->sack_sb);
59
60 for (i = 0; i < 1000 / 100; i++)
61 {
62 block.start = i * 100;
63 block.end = (i + 1) * 100;
64 vec_add1 (sacks, block);
65 }
66
67 /*
68 * Inject even blocks
69 */
70
71 for (i = 0; i < 1000 / 200; i++)
72 {
Florin Coras93992a92017-05-24 18:03:56 -070073 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
Florin Coras6792ec02017-03-13 03:49:51 -070074 }
Florin Coras93992a92017-05-24 18:03:56 -070075 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
Florin Coras6792ec02017-03-13 03:49:51 -070076 tcp_rcv_sacks (tc, 0);
77
Florin Coras06d11012017-05-17 14:21:51 -070078 if (verbose)
79 vlib_cli_output (vm, "sb after even blocks:\n%U", format_tcp_scoreboard,
80 sb);
81
Florin Coras6792ec02017-03-13 03:49:51 -070082 TCP_TEST ((pool_elts (sb->holes) == 5),
83 "scoreboard has %d elements", pool_elts (sb->holes));
84
85 /* First SACK block should be rejected */
86 hole = scoreboard_first_hole (sb);
87 TCP_TEST ((hole->start == 0 && hole->end == 200),
88 "first hole start %u end %u", hole->start, hole->end);
89 hole = scoreboard_last_hole (sb);
90 TCP_TEST ((hole->start == 900 && hole->end == 1000),
91 "last hole start %u end %u", hole->start, hole->end);
92 TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
93 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
94 TCP_TEST ((sb->last_sacked_bytes == 400),
95 "last sacked bytes %d", sb->last_sacked_bytes);
Florin Coras93992a92017-05-24 18:03:56 -070096 TCP_TEST ((sb->high_sacked == 900), "max byte sacked %u", sb->high_sacked);
Florin Coras6792ec02017-03-13 03:49:51 -070097 /*
98 * Inject odd blocks
99 */
100
Florin Coras93992a92017-05-24 18:03:56 -0700101 vec_reset_length (tc->rcv_opts.sacks);
Florin Coras6792ec02017-03-13 03:49:51 -0700102 for (i = 0; i < 1000 / 200; i++)
103 {
Florin Coras93992a92017-05-24 18:03:56 -0700104 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
Florin Coras6792ec02017-03-13 03:49:51 -0700105 }
Florin Coras93992a92017-05-24 18:03:56 -0700106 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
Florin Coras6792ec02017-03-13 03:49:51 -0700107 tcp_rcv_sacks (tc, 0);
108
Florin Coras06d11012017-05-17 14:21:51 -0700109 if (verbose)
110 vlib_cli_output (vm, "sb after odd blocks:\n%U", format_tcp_scoreboard,
111 sb);
112
Florin Coras6792ec02017-03-13 03:49:51 -0700113 hole = scoreboard_first_hole (sb);
114 TCP_TEST ((pool_elts (sb->holes) == 1),
115 "scoreboard has %d holes", pool_elts (sb->holes));
116 TCP_TEST ((hole->start == 0 && hole->end == 100),
117 "first hole start %u end %u", hole->start, hole->end);
118 TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
119 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
Florin Coras93992a92017-05-24 18:03:56 -0700120 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
Florin Coras6792ec02017-03-13 03:49:51 -0700121 TCP_TEST ((sb->last_sacked_bytes == 500),
122 "last sacked bytes %d", sb->last_sacked_bytes);
123
124 /*
125 * Ack until byte 100, all bytes are now acked + sacked
126 */
127 tcp_rcv_sacks (tc, 100);
Florin Coras06d11012017-05-17 14:21:51 -0700128 if (verbose)
129 vlib_cli_output (vm, "ack until byte 100:\n%U", format_tcp_scoreboard,
130 sb);
Florin Coras6792ec02017-03-13 03:49:51 -0700131
132 TCP_TEST ((pool_elts (sb->holes) == 0),
133 "scoreboard has %d elements", pool_elts (sb->holes));
134 TCP_TEST ((sb->snd_una_adv == 900),
135 "snd_una_adv after ack %u", sb->snd_una_adv);
Florin Coras93992a92017-05-24 18:03:56 -0700136 TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
Florin Coras6792ec02017-03-13 03:49:51 -0700137 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
138 TCP_TEST ((sb->last_sacked_bytes == 0),
139 "last sacked bytes %d", sb->last_sacked_bytes);
140
141 /*
142 * Add new block
143 */
144
Florin Coras93992a92017-05-24 18:03:56 -0700145 vec_reset_length (tc->rcv_opts.sacks);
Florin Coras6792ec02017-03-13 03:49:51 -0700146
147 block.start = 1200;
148 block.end = 1300;
Florin Coras93992a92017-05-24 18:03:56 -0700149 vec_add1 (tc->rcv_opts.sacks, block);
Florin Coras6792ec02017-03-13 03:49:51 -0700150
Florin Coras06d11012017-05-17 14:21:51 -0700151 if (verbose)
152 vlib_cli_output (vm, "add [1200, 1300]:\n%U", format_tcp_scoreboard, sb);
Florin Coras6792ec02017-03-13 03:49:51 -0700153 tc->snd_una_max = 1500;
154 tc->snd_una = 1000;
155 tc->snd_nxt = 1500;
156 tcp_rcv_sacks (tc, 1000);
157
Florin Coras06d11012017-05-17 14:21:51 -0700158 if (verbose)
159 vlib_cli_output (vm, "sb snd_una_max 1500, snd_una 1000:\n%U",
160 format_tcp_scoreboard, sb);
161
Florin Coras6792ec02017-03-13 03:49:51 -0700162 TCP_TEST ((sb->snd_una_adv == 0),
163 "snd_una_adv after ack %u", sb->snd_una_adv);
164 TCP_TEST ((pool_elts (sb->holes) == 2),
165 "scoreboard has %d holes", pool_elts (sb->holes));
166 hole = scoreboard_first_hole (sb);
167 TCP_TEST ((hole->start == 1000 && hole->end == 1200),
168 "first hole start %u end %u", hole->start, hole->end);
Florin Coras06d11012017-05-17 14:21:51 -0700169 TCP_TEST ((sb->snd_una_adv == 0),
170 "snd_una_adv after ack %u", sb->snd_una_adv);
Florin Coras93992a92017-05-24 18:03:56 -0700171 TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
Florin Coras6792ec02017-03-13 03:49:51 -0700172 hole = scoreboard_last_hole (sb);
173 TCP_TEST ((hole->start == 1300 && hole->end == 1500),
174 "last hole start %u end %u", hole->start, hole->end);
175 TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
176
177 /*
178 * Ack first hole
179 */
180
Florin Coras93992a92017-05-24 18:03:56 -0700181 vec_reset_length (tc->rcv_opts.sacks);
Florin Coras6792ec02017-03-13 03:49:51 -0700182 tcp_rcv_sacks (tc, 1200);
183
Florin Coras06d11012017-05-17 14:21:51 -0700184 if (verbose)
185 vlib_cli_output (vm, "sb ack up to byte 1200:\n%U", format_tcp_scoreboard,
186 sb);
187
Florin Coras6792ec02017-03-13 03:49:51 -0700188 TCP_TEST ((sb->snd_una_adv == 100),
189 "snd_una_adv after ack %u", sb->snd_una_adv);
190 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
191 TCP_TEST ((pool_elts (sb->holes) == 1),
192 "scoreboard has %d elements", pool_elts (sb->holes));
Florin Corasf03a59a2017-06-09 21:07:32 -0700193 hole = scoreboard_first_hole (sb);
194 TCP_TEST ((hole->prev == TCP_INVALID_SACK_HOLE_INDEX
195 && hole->next == TCP_INVALID_SACK_HOLE_INDEX), "hole is valid");
196 TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
197 sb->last_bytes_delivered);
Florin Coras6792ec02017-03-13 03:49:51 -0700198
199 /*
Florin Coras93992a92017-05-24 18:03:56 -0700200 * Add some more blocks and then remove all
Florin Coras6792ec02017-03-13 03:49:51 -0700201 */
Florin Coras93992a92017-05-24 18:03:56 -0700202 vec_reset_length (tc->rcv_opts.sacks);
Florin Corasf03a59a2017-06-09 21:07:32 -0700203 tc->snd_una += sb->snd_una_adv;
204 tc->snd_una_max = 1900;
Florin Coras93992a92017-05-24 18:03:56 -0700205 for (i = 0; i < 5; i++)
206 {
207 block.start = i * 100 + 1200;
208 block.end = (i + 1) * 100 + 1200;
209 vec_add1 (tc->rcv_opts.sacks, block);
210 }
211 tcp_rcv_sacks (tc, 1900);
Florin Coras6792ec02017-03-13 03:49:51 -0700212
213 scoreboard_clear (sb);
Florin Coras06d11012017-05-17 14:21:51 -0700214 if (verbose)
215 vlib_cli_output (vm, "sb cleared all:\n%U", format_tcp_scoreboard, sb);
216
Florin Coras6792ec02017-03-13 03:49:51 -0700217 TCP_TEST ((pool_elts (sb->holes) == 0),
218 "number of holes %d", pool_elts (sb->holes));
Florin Coras93992a92017-05-24 18:03:56 -0700219 TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
220 TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
221
Florin Coras06d11012017-05-17 14:21:51 -0700222 /*
223 * Re-inject odd blocks and ack them all
224 */
225
226 tc->snd_una = 0;
227 tc->snd_una_max = 1000;
228 tc->snd_nxt = 1000;
229 for (i = 0; i < 5; i++)
230 {
Florin Coras93992a92017-05-24 18:03:56 -0700231 vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
Florin Coras06d11012017-05-17 14:21:51 -0700232 }
Florin Coras93992a92017-05-24 18:03:56 -0700233 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
Florin Coras06d11012017-05-17 14:21:51 -0700234 tcp_rcv_sacks (tc, 0);
235 if (verbose)
236 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
237 format_tcp_scoreboard, sb);
238
239 tcp_rcv_sacks (tc, 950);
240
241 if (verbose)
242 vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
243 format_tcp_scoreboard, sb);
244
245 TCP_TEST ((pool_elts (sb->holes) == 0),
246 "scoreboard has %d elements", pool_elts (sb->holes));
247 TCP_TEST ((sb->snd_una_adv == 50), "snd_una_adv %u", sb->snd_una_adv);
248 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
249 TCP_TEST ((sb->last_sacked_bytes == 0),
250 "last sacked bytes %d", sb->last_sacked_bytes);
251
Florin Corasf03a59a2017-06-09 21:07:32 -0700252 /*
253 * Inject one block, ack it and overlap hole
254 */
255
256 tc->snd_una = 0;
257 tc->snd_una_max = 1000;
258 tc->snd_nxt = 1000;
259
260 block.start = 100;
261 block.end = 500;
262 vec_add1 (tc->rcv_opts.sacks, block);
263 tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
264
265 tcp_rcv_sacks (tc, 0);
266
267 if (verbose)
268 vlib_cli_output (vm, "sb added [100, 500]:\n%U",
269 format_tcp_scoreboard, sb);
270
271 tcp_rcv_sacks (tc, 800);
272
273 if (verbose)
274 vlib_cli_output (vm, "sb ack [0, 800]:\n%U", format_tcp_scoreboard, sb);
275
276 TCP_TEST ((pool_elts (sb->holes) == 1),
277 "scoreboard has %d elements", pool_elts (sb->holes));
278 TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
279 TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
280 TCP_TEST ((sb->last_sacked_bytes == 0),
281 "last sacked bytes %d", sb->last_sacked_bytes);
282 TCP_TEST ((sb->last_bytes_delivered == 400),
283 "last bytes delivered %d", sb->last_bytes_delivered);
284
Florin Coras6792ec02017-03-13 03:49:51 -0700285 return 0;
286}
287
Florin Coras45d34962017-04-25 00:05:27 -0700288static int
289tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
290{
291 tcp_connection_t _tc, *tc = &_tc;
292 sack_block_t *sacks;
Dave Barach2c25a622017-06-26 11:35:07 -0400293 int i, verbose = 0, expected;
Florin Coras45d34962017-04-25 00:05:27 -0700294
295 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
296 {
297 if (unformat (input, "verbose"))
298 verbose = 1;
299 else
300 {
301 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
302 input);
303 return -1;
304 }
305 }
306
307 memset (tc, 0, sizeof (*tc));
308
309 /*
310 * Add odd sack block pairs
311 */
312 for (i = 1; i < 10; i += 2)
313 {
314 tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
315 }
316
317 TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
318 vec_len (tc->snd_sacks), 5);
319 TCP_TEST ((tc->snd_sacks[0].start = 900),
320 "first sack block start %u expected %u", tc->snd_sacks[0].start,
321 900);
322
323 /*
324 * Try to add one extra
325 */
326 sacks = vec_dup (tc->snd_sacks);
327
328 tcp_update_sack_list (tc, 1100, 1200);
Dave Barach2c25a622017-06-26 11:35:07 -0400329 if (verbose)
330 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
331 format_tcp_sacks, tc);
332 expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
333 TCP_TEST ((vec_len (tc->snd_sacks) == expected),
334 "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
Florin Coras45d34962017-04-25 00:05:27 -0700335 TCP_TEST ((tc->snd_sacks[0].start == 1100),
336 "first sack block start %u expected %u", tc->snd_sacks[0].start,
337 1100);
338
339 /* restore */
340 vec_free (tc->snd_sacks);
341 tc->snd_sacks = sacks;
342
343 /*
344 * Overlap first 2 segment
345 */
346 tc->rcv_nxt = 300;
347 tcp_update_sack_list (tc, 300, 300);
348 if (verbose)
349 vlib_cli_output (vm, "overlap first 2 segments:\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700350 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700351 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
352 vec_len (tc->snd_sacks), 3);
353 TCP_TEST ((tc->snd_sacks[0].start == 900),
354 "first sack block start %u expected %u", tc->snd_sacks[0].start,
355 500);
356
357 /*
358 * Add a new segment
359 */
360 tcp_update_sack_list (tc, 1100, 1200);
361 if (verbose)
362 vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700363 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700364 TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
365 vec_len (tc->snd_sacks), 4);
366 TCP_TEST ((tc->snd_sacks[0].start == 1100),
367 "first sack block start %u expected %u", tc->snd_sacks[0].start,
368 1100);
369
370 /*
371 * Join middle segments
372 */
373 tcp_update_sack_list (tc, 800, 900);
374 if (verbose)
375 vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
Florin Corasc28764f2017-04-26 00:08:42 -0700376 format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700377
378 TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
379 vec_len (tc->snd_sacks), 3);
380 TCP_TEST ((tc->snd_sacks[0].start == 700),
381 "first sack block start %u expected %u", tc->snd_sacks[0].start,
382 1100);
383
384 /*
385 * Advance rcv_nxt to overlap all
386 */
387 tc->rcv_nxt = 1200;
388 tcp_update_sack_list (tc, 1200, 1200);
389 if (verbose)
Florin Corasc28764f2017-04-26 00:08:42 -0700390 vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
Florin Coras45d34962017-04-25 00:05:27 -0700391 TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
392 vec_len (tc->snd_sacks), 0);
393 return 0;
394}
395
396static int
397tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
398{
399 int res = 0;
400
401 /* Run all tests */
402 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
403 {
404 if (tcp_test_sack_tx (vm, input))
405 {
406 return -1;
407 }
408
Florin Coras06d11012017-05-17 14:21:51 -0700409 if (tcp_test_sack_rx (vm, input))
Florin Coras45d34962017-04-25 00:05:27 -0700410 {
411 return -1;
412 }
413 }
414 else
415 {
416 if (unformat (input, "tx"))
417 {
418 res = tcp_test_sack_tx (vm, input);
419 }
420 else if (unformat (input, "rx"))
421 {
Florin Coras06d11012017-05-17 14:21:51 -0700422 res = tcp_test_sack_rx (vm, input);
Florin Coras45d34962017-04-25 00:05:27 -0700423 }
424 }
425
426 return res;
427}
428
429
Dave Barach1f75cfd2017-04-14 16:46:44 -0400430typedef struct
431{
432 u32 offset;
433 u32 len;
434} test_pattern_t;
435
436/* *INDENT-OFF* */
437test_pattern_t test_pattern[] = {
438 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
439 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
440 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
441 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
442 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
443 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
444 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
445 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
446 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
447 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
448 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
449 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
450 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
451 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
452 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
453 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
454 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
455 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
456 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
457 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
458 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
459 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
460 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
461 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
462 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
463 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
464 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
465 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
466 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
467 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
468 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
469 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
470 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
471 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
472 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
473 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
474 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
475 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
476 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
477 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
478 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
479 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
480 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
481 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
482 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
483 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
484 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
485 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
486 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
487 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
488 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
489 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
490 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
491 {2332, 8},
492 /* missing from original data set */
493 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
494};
495/* *INDENT-ON* */
496
497int
498pattern_cmp (const void *arg1, const void *arg2)
499{
500 test_pattern_t *a1 = (test_pattern_t *) arg1;
501 test_pattern_t *a2 = (test_pattern_t *) arg2;
502
503 if (a1->offset < a2->offset)
504 return -1;
505 else if (a1->offset > a2->offset)
506 return 1;
507 return 0;
508}
509
510static u8
511fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
512 u32 pattern_length)
513{
514 test_pattern_t *tp = pattern;
515 int i;
516
517 /* Go through the pattern and make 100% sure it's sane */
518 for (i = 0; i < pattern_length - 1; i++)
519 {
520 if (tp->offset + tp->len != (tp + 1)->offset)
521 {
522 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
523 (tp->offset + tp->len),
524 (tp + 1)->offset - (tp->offset + tp->len));
525 return 0;
526 }
527 tp++;
528 }
529 return 1;
530}
531
532static test_pattern_t *
533fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
534 u32 test_data_len)
535{
536 test_pattern_t *validate_pattern = 0;
537
538 /* Validate, and try segments in order... */
539 vec_validate (validate_pattern, test_data_len - 1);
540 memcpy (validate_pattern, test_data,
541 test_data_len * sizeof (test_pattern_t));
542 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
543 pattern_cmp);
544
545 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
546 return 0;
547
548 return validate_pattern;
549}
550
Florin Corasb59a7052017-04-18 22:07:29 -0700551static svm_fifo_t *
552fifo_prepare (u32 fifo_size)
553{
554 svm_fifo_t *f;
555 f = svm_fifo_create (fifo_size);
556
557 /* Paint fifo data vector with -1's */
558 memset (f->data, 0xFF, fifo_size);
559
560 return f;
561}
562
563static int
564compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
565{
566 int i;
567
568 for (i = start; i < len; i++)
569 {
570 if (data1[i] != data2[i])
571 {
572 *index = i;
573 return 1;
574 }
575 }
576 return 0;
577}
578
Dave Barach1f75cfd2017-04-14 16:46:44 -0400579int
580tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700581{
582 svm_fifo_t *f;
583 u32 fifo_size = 1 << 20;
584 u32 *test_data = 0;
585 u32 offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400586 int i, rv, verbose = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700587 u32 data_word, test_data_len, j;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400588 ooo_segment_t *ooo_seg;
Florin Corasb59a7052017-04-18 22:07:29 -0700589 u8 *data, *s, *data_buf = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700590
Dave Barach1f75cfd2017-04-14 16:46:44 -0400591 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
592 {
593 if (unformat (input, "verbose"))
594 verbose = 1;
595 }
596
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 test_data_len = fifo_size / sizeof (u32);
598 vec_validate (test_data, test_data_len - 1);
599
600 for (i = 0; i < vec_len (test_data); i++)
601 test_data[i] = i;
602
Florin Corasb59a7052017-04-18 22:07:29 -0700603 f = fifo_prepare (fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700604
Florin Corasb59a7052017-04-18 22:07:29 -0700605 /*
606 * Enqueue an initial (un-dequeued) chunk
607 */
Florin Corasa5464812017-04-19 13:00:05 -0700608 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400609 TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
610 TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700611
612 /*
613 * Create 3 chunks in the future. The offsets are relative
614 * to the current fifo tail
615 */
616 for (i = 0; i < 3; i++)
617 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700618 offset = (2 * i + 1) * sizeof (u32) - f->tail;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400619 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasc28764f2017-04-26 00:08:42 -0700620 if (i == 0)
621 {
622 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
623 rv = rv > 0 ? 0 : rv;
624 }
625 else
626 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400627 if (verbose)
628 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
629 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700630 if (rv)
631 {
632 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400633 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700634 }
635 }
636
Dave Barach1f75cfd2017-04-14 16:46:44 -0400637 if (verbose)
638 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
Florin Corasb59a7052017-04-18 22:07:29 -0700639
Dave Barach1f75cfd2017-04-14 16:46:44 -0400640 TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
Florin Corasc28764f2017-04-26 00:08:42 -0700641 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
642 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
643
644 /*
645 * Try adding a completely overlapped segment
646 */
Florin Corasf03a59a2017-06-09 21:07:32 -0700647 offset = 3 * sizeof (u32) - f->tail;
Florin Corasc28764f2017-04-26 00:08:42 -0700648 data = (u8 *) (test_data + 3);
649 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
650 if (rv)
651 {
652 clib_warning ("enqueue returned %d", rv);
653 goto err;
654 }
655
656 if (verbose)
657 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
658
659 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
660 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Dave Barach1f75cfd2017-04-14 16:46:44 -0400661
Florin Corasb59a7052017-04-18 22:07:29 -0700662 /*
663 * Make sure format functions are not buggy
664 */
665 s = format (0, "%U", format_svm_fifo, f, 2);
666 vec_free (s);
667
668 /*
669 * Paint some of missing data backwards
670 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400671 for (i = 3; i > 1; i--)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700672 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700673 offset = (2 * i + 0) * sizeof (u32) - f->tail;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400674 data = (u8 *) (test_data + (2 * i + 0));
Florin Corasa5464812017-04-19 13:00:05 -0700675 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400676 if (verbose)
677 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
678 offset + sizeof (u32));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700679 if (rv)
680 {
681 clib_warning ("enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400682 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700683 }
684 }
685
Dave Barach1f75cfd2017-04-14 16:46:44 -0400686 if (verbose)
687 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
688 1);
689 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
690 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
691 ooo_seg = svm_fifo_first_ooo_segment (f);
692 TCP_TEST ((ooo_seg->start == 12),
693 "first ooo seg position %u", ooo_seg->start);
694 TCP_TEST ((ooo_seg->length == 16),
695 "first ooo seg length %u", ooo_seg->length);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700696
Florin Corasb59a7052017-04-18 22:07:29 -0700697 /*
698 * Enqueue the missing u32
699 */
Florin Corasa5464812017-04-19 13:00:05 -0700700 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
Dave Barach1f75cfd2017-04-14 16:46:44 -0400701 if (verbose)
702 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
703 1);
704 TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
705 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
706 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700707
Florin Corasb59a7052017-04-18 22:07:29 -0700708 /*
709 * Collect results
710 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700711 for (i = 0; i < 7; i++)
712 {
Florin Corasa5464812017-04-19 13:00:05 -0700713 rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700714 if (rv != sizeof (u32))
715 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400716 clib_warning ("bytes dequeues %u", rv);
717 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700718 }
719 if (data_word != test_data[i])
720 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400721 clib_warning ("recovered [%d] %d not %d", i, data_word,
722 test_data[i]);
723 goto err;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700724 }
725 }
726
Florin Corasb59a7052017-04-18 22:07:29 -0700727 /*
728 * Test segment overlaps: last ooo segment overlaps all
729 */
730 svm_fifo_free (f);
731 f = fifo_prepare (fifo_size);
732
733 for (i = 0; i < 4; i++)
734 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700735 offset = (2 * i + 1) * sizeof (u32) - f->tail;
Florin Corasb59a7052017-04-18 22:07:29 -0700736 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700737 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700738 if (verbose)
739 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
740 offset + sizeof (u32));
741 if (rv)
742 {
743 clib_warning ("enqueue returned %d", rv);
744 goto err;
745 }
746 }
747
Florin Corasf03a59a2017-06-09 21:07:32 -0700748 rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data);
Florin Corasb59a7052017-04-18 22:07:29 -0700749 TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
750 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
751 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
752
753 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700754 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700755 if (compare_data (data_buf, data, 8, vec_len (data), &j))
756 {
757 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
758 }
759 vec_reset_length (data_buf);
760
761 /*
762 * Test segment overlaps: enqueue and overlap ooo segments
763 */
764 svm_fifo_free (f);
765 f = fifo_prepare (fifo_size);
766
767 for (i = 0; i < 4; i++)
768 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700769 offset = (2 * i + 1) * sizeof (u32) - f->tail;
Florin Corasb59a7052017-04-18 22:07:29 -0700770 data = (u8 *) (test_data + (2 * i + 1));
Florin Corasa5464812017-04-19 13:00:05 -0700771 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
Florin Corasb59a7052017-04-18 22:07:29 -0700772 if (verbose)
773 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
774 offset + sizeof (u32));
775 if (rv)
776 {
777 clib_warning ("enqueue returned %d", rv);
778 goto err;
779 }
780 }
781
Florin Corasf03a59a2017-06-09 21:07:32 -0700782 if (verbose)
783 vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
784
Florin Corasa5464812017-04-19 13:00:05 -0700785 rv = svm_fifo_enqueue_nowait (f, 29, data);
Florin Corasf03a59a2017-06-09 21:07:32 -0700786 if (verbose)
787 vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
788 1);
Florin Corasb59a7052017-04-18 22:07:29 -0700789 TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
790 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
791 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
792
793 vec_validate (data_buf, vec_len (data));
Florin Corasa5464812017-04-19 13:00:05 -0700794 svm_fifo_peek (f, 0, vec_len (data), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -0700795 if (compare_data (data_buf, data, 0, vec_len (data), &j))
796 {
797 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
798 }
799
Florin Coras93992a92017-05-24 18:03:56 -0700800 /* Try to peek beyond the data */
801 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
802 TCP_TEST ((rv == 0), "peeked %u expected 0", rv);
803
Florin Corasb59a7052017-04-18 22:07:29 -0700804 vec_free (data_buf);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700805 svm_fifo_free (f);
806 vec_free (test_data);
Florin Corasb59a7052017-04-18 22:07:29 -0700807
Florin Coras6cf30ad2017-04-04 23:08:23 -0700808 return 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400809
810err:
811 svm_fifo_free (f);
812 vec_free (test_data);
813 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700814}
815
Dave Barach1f75cfd2017-04-14 16:46:44 -0400816static int
817tcp_test_fifo2 (vlib_main_t * vm)
818{
819 svm_fifo_t *f;
820 u32 fifo_size = 1 << 20;
821 int i, rv, test_data_len;
822 u64 data64;
823 test_pattern_t *tp, *vp, *test_data;
824 ooo_segment_t *ooo_seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700825
Dave Barach1f75cfd2017-04-14 16:46:44 -0400826 test_data = test_pattern;
827 test_data_len = ARRAY_LEN (test_pattern);
828
829 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
830
831 /* Create a fifo */
Florin Corasb59a7052017-04-18 22:07:29 -0700832 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400833
834 /*
835 * Try with sorted data
836 */
837 for (i = 0; i < test_data_len; i++)
838 {
839 tp = vp + i;
840 data64 = tp->offset;
Florin Corasf03a59a2017-06-09 21:07:32 -0700841 svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
842 (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400843 }
844
845 /* Expected result: one big fat chunk at offset 4 */
846 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
847 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
848 ooo_seg = svm_fifo_first_ooo_segment (f);
849 TCP_TEST ((ooo_seg->start == 4),
850 "first ooo seg position %u", ooo_seg->start);
851 TCP_TEST ((ooo_seg->length == 2996),
852 "first ooo seg length %u", ooo_seg->length);
853
854 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700855 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400856 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
857
858 svm_fifo_free (f);
859 vec_free (vp);
860
861 /*
862 * Now try it again w/ unsorted data...
863 */
864
Florin Corasb59a7052017-04-18 22:07:29 -0700865 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400866
867 for (i = 0; i < test_data_len; i++)
868 {
869 tp = &test_data[i];
870 data64 = tp->offset;
Florin Corasf03a59a2017-06-09 21:07:32 -0700871 rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
Dave Barach1f75cfd2017-04-14 16:46:44 -0400872 (u8 *) & data64);
873 if (rv)
874 {
875 clib_warning ("enqueue returned %d", rv);
876 }
877 }
878
879 /* Expecting the same result: one big fat chunk at offset 4 */
880 TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
881 "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
882 ooo_seg = svm_fifo_first_ooo_segment (f);
883 TCP_TEST ((ooo_seg->start == 4),
884 "first ooo seg position %u", ooo_seg->start);
885 TCP_TEST ((ooo_seg->length == 2996),
886 "first ooo seg length %u", ooo_seg->length);
887
888 data64 = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700889 rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400890
891 TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
892
893 svm_fifo_free (f);
894
895 return 0;
896}
897
898static int
899tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
900{
901 svm_fifo_t *f;
902 u32 fifo_size = 4 << 10;
903 u32 fifo_initial_offset = 0;
904 u32 total_size = 2 << 10;
Florin Corasb59a7052017-04-18 22:07:29 -0700905 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
906 u8 *data_pattern = 0, *data_buf = 0;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400907 test_pattern_t *tp, *generate = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700908 u32 nsegs = 2, seg_size, length_so_far;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400909 u32 current_offset, offset_increment, len_this_chunk;
Florin Corasb59a7052017-04-18 22:07:29 -0700910 u32 seed = 0xdeaddabe, j;
911 int i, rv;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400912
913 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
914 {
915 if (unformat (input, "fifo-size %d", &fifo_size))
916 ;
917 else if (unformat (input, "total-size %d", &total_size))
918 ;
919 else if (unformat (input, "verbose"))
920 verbose = 1;
921 else if (unformat (input, "overlap"))
922 overlap = 1;
923 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
924 ;
925 else if (unformat (input, "seed %d", &seed))
926 ;
927 else if (unformat (input, "nsegs %d", &nsegs))
928 ;
929 else if (unformat (input, "no-randomize"))
930 randomize = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700931 else if (unformat (input, "in-seq-all"))
932 in_seq_all = 1;
933 else if (unformat (input, "drop"))
934 drop = 1;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400935 else
936 {
937 clib_error_t *e = clib_error_return
938 (0, "unknown input `%U'", format_unformat_error, input);
939 clib_error_report (e);
940 return -1;
941 }
942 }
943
Florin Corasb59a7052017-04-18 22:07:29 -0700944 if (total_size > fifo_size)
945 {
946 clib_warning ("total_size %d greater than fifo size %d", total_size,
947 fifo_size);
948 return -1;
949 }
950 if (overlap && randomize == 0)
951 {
952 clib_warning ("Can't enqueue in-order with overlap");
953 return -1;
954 }
955
Dave Barach1f75cfd2017-04-14 16:46:44 -0400956 /*
957 * Generate data
958 */
959 vec_validate (data_pattern, total_size - 1);
960 for (i = 0; i < vec_len (data_pattern); i++)
961 data_pattern[i] = i & 0xff;
962
Florin Corasb59a7052017-04-18 22:07:29 -0700963 /*
964 * Generate segments
965 */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400966 seg_size = total_size / nsegs;
967 length_so_far = 0;
Florin Corasb59a7052017-04-18 22:07:29 -0700968 current_offset = randomize;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400969 while (length_so_far < total_size)
970 {
971 vec_add2 (generate, tp, 1);
972 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
973 tp->offset = current_offset;
974 tp->len = len_this_chunk;
975
976 if (overlap && (len_this_chunk == seg_size))
977 do
978 {
979 offset_increment = len_this_chunk
980 % (1 + (random_u32 (&seed) % len_this_chunk));
981 }
982 while (offset_increment == 0);
983 else
984 offset_increment = len_this_chunk;
985
986 current_offset += offset_increment;
987 length_so_far = tp->offset + tp->len;
988 }
989
990 /*
991 * Validate segment list. Only valid for non-overlap cases.
992 */
993 if (overlap == 0)
994 fifo_validate_pattern (vm, generate, vec_len (generate));
995
996 if (verbose)
997 {
998 vlib_cli_output (vm, "raw data pattern:");
999 for (i = 0; i < vec_len (generate); i++)
1000 {
1001 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1002 generate[i].offset, generate[i].len);
1003 }
1004 }
1005
1006 /* Randomize data pattern */
1007 if (randomize)
1008 {
1009 for (i = 0; i < vec_len (generate) / 2; i++)
1010 {
1011 u32 src_index, dst_index;
1012 test_pattern_t _tmp, *tmp = &_tmp;
1013
1014 src_index = random_u32 (&seed) % vec_len (generate);
1015 dst_index = random_u32 (&seed) % vec_len (generate);
1016
1017 tmp[0] = generate[dst_index];
1018 generate[dst_index] = generate[src_index];
1019 generate[src_index] = tmp[0];
1020 }
Florin Corasb59a7052017-04-18 22:07:29 -07001021 if (verbose)
Dave Barach1f75cfd2017-04-14 16:46:44 -04001022 {
Florin Corasb59a7052017-04-18 22:07:29 -07001023 vlib_cli_output (vm, "randomized data pattern:");
1024 for (i = 0; i < vec_len (generate); i++)
1025 {
1026 vlib_cli_output (vm, "[%d] offset %u len %u", i,
1027 generate[i].offset, generate[i].len);
1028 }
Dave Barach1f75cfd2017-04-14 16:46:44 -04001029 }
1030 }
1031
Florin Corasb59a7052017-04-18 22:07:29 -07001032 /*
1033 * Create a fifo and add segments
1034 */
1035 f = fifo_prepare (fifo_size);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001036
1037 /* manually set head and tail pointers to validate modular arithmetic */
Florin Corasb59a7052017-04-18 22:07:29 -07001038 fifo_initial_offset = fifo_initial_offset % fifo_size;
1039 f->head = fifo_initial_offset;
1040 f->tail = fifo_initial_offset;
Dave Barach1f75cfd2017-04-14 16:46:44 -04001041
Florin Corasc28764f2017-04-26 00:08:42 -07001042 for (i = !randomize; i < vec_len (generate); i++)
Dave Barach1f75cfd2017-04-14 16:46:44 -04001043 {
1044 tp = generate + i;
Florin Corasf03a59a2017-06-09 21:07:32 -07001045 svm_fifo_enqueue_with_offset (f,
1046 fifo_initial_offset + tp->offset -
1047 f->tail, tp->len,
Florin Coras82b13a82017-04-25 11:58:06 -07001048 (u8 *) data_pattern + tp->offset);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001049 }
1050
Florin Corasc28764f2017-04-26 00:08:42 -07001051 /* Add the first segment in order for non random data */
1052 if (!randomize)
1053 svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
1054
Florin Corasb59a7052017-04-18 22:07:29 -07001055 /*
1056 * Expected result: one big fat chunk at offset 1 if randomize == 1
1057 */
Dave Barach1f75cfd2017-04-14 16:46:44 -04001058
1059 if (verbose)
1060 vlib_cli_output (vm, "fifo before missing link: %U",
1061 format_svm_fifo, f, 1 /* verbose */ );
1062
Florin Corasb59a7052017-04-18 22:07:29 -07001063 /*
1064 * Add the missing byte if segments were randomized
1065 */
1066 if (randomize)
1067 {
1068 u32 bytes_to_enq = 1;
1069 if (in_seq_all)
1070 bytes_to_enq = total_size;
Florin Corasa5464812017-04-19 13:00:05 -07001071 rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001072
Florin Corasb59a7052017-04-18 22:07:29 -07001073 if (verbose)
1074 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001075
Florin Corasb59a7052017-04-18 22:07:29 -07001076 TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
1077 total_size);
1078
1079 }
1080
1081 TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
1082 svm_fifo_number_ooo_segments (f));
1083
1084 /*
1085 * Test if peeked data is the same as original data
1086 */
1087 vec_validate (data_buf, vec_len (data_pattern));
Florin Corasa5464812017-04-19 13:00:05 -07001088 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -07001089 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1090 {
1091 TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1092 data_pattern[j]);
1093 }
1094 vec_reset_length (data_buf);
1095
1096 /*
1097 * Dequeue or drop all data
1098 */
1099 if (drop)
1100 {
Florin Corasa5464812017-04-19 13:00:05 -07001101 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
Florin Corasb59a7052017-04-18 22:07:29 -07001102 }
1103 else
1104 {
Florin Corasa5464812017-04-19 13:00:05 -07001105 svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
Florin Corasb59a7052017-04-18 22:07:29 -07001106 if (compare_data
1107 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1108 {
1109 TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1110 data_pattern[j]);
1111 }
1112 }
1113
1114 TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
1115 svm_fifo_max_dequeue (f));
1116
Dave Barach1f75cfd2017-04-14 16:46:44 -04001117 svm_fifo_free (f);
1118 vec_free (data_pattern);
Florin Corasb59a7052017-04-18 22:07:29 -07001119 vec_free (data_buf);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001120
1121 return 0;
1122}
1123
1124static int
Florin Corasc28764f2017-04-26 00:08:42 -07001125tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
1126{
1127 svm_fifo_t *f;
1128 u32 fifo_size = 6 << 10;
1129 u32 fifo_initial_offset = 1000000000;
1130 u32 test_n_bytes = 5000, j;
1131 u8 *test_data = 0, *data_buf = 0;
1132 int i, rv, verbose = 0;
1133
1134 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1135 {
1136 if (unformat (input, "verbose"))
1137 verbose = 1;
1138 else
1139 {
1140 clib_error_t *e = clib_error_return
1141 (0, "unknown input `%U'", format_unformat_error, input);
1142 clib_error_report (e);
1143 return -1;
1144 }
1145 }
1146
1147 /*
1148 * Create a fifo and add segments
1149 */
1150 f = fifo_prepare (fifo_size);
1151
1152 /* Set head and tail pointers */
1153 fifo_initial_offset = fifo_initial_offset % fifo_size;
1154 svm_fifo_init_pointers (f, fifo_initial_offset);
1155
1156 vec_validate (test_data, test_n_bytes - 1);
1157 for (i = 0; i < vec_len (test_data); i++)
1158 test_data[i] = i;
1159
1160 for (i = test_n_bytes - 1; i > 0; i--)
1161 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001162 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
Florin Corasc28764f2017-04-26 00:08:42 -07001163 sizeof (u8), &test_data[i]);
1164 if (verbose)
1165 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1166 if (rv)
1167 {
1168 clib_warning ("enqueue returned %d", rv);
1169 svm_fifo_free (f);
1170 vec_free (test_data);
1171 return -1;
1172 }
1173 }
1174
1175 svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1176
1177 vec_validate (data_buf, vec_len (test_data));
1178
1179 svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1180 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1181 if (rv)
1182 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1183 test_data[j]);
1184 TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1185
1186 svm_fifo_free (f);
1187 vec_free (test_data);
1188 return 0;
1189}
1190
1191static int
Dave Barach1f75cfd2017-04-14 16:46:44 -04001192tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1193{
1194 int res = 0;
Florin Corasb59a7052017-04-18 22:07:29 -07001195 char *str;
Dave Barach1f75cfd2017-04-14 16:46:44 -04001196
1197 /* Run all tests */
1198 if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1199 {
1200 res = tcp_test_fifo1 (vm, input);
1201 if (res)
1202 return res;
1203
1204 res = tcp_test_fifo2 (vm);
1205 if (res)
1206 return res;
1207
Florin Corasb59a7052017-04-18 22:07:29 -07001208 /*
1209 * Run a number of fifo3 configs
1210 */
1211 str = "nsegs 10 overlap seed 123";
1212 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001213 if (tcp_test_fifo3 (vm, input))
1214 return -1;
1215 unformat_free (input);
1216
Florin Corasb59a7052017-04-18 22:07:29 -07001217 str = "nsegs 10 overlap seed 123 in-seq-all";
1218 unformat_init_cstring (input, str);
1219 if (tcp_test_fifo3 (vm, input))
1220 return -1;
1221 unformat_free (input);
1222
1223 str = "nsegs 10 overlap seed 123 initial-offset 3917";
1224 unformat_init_cstring (input, str);
1225 if (tcp_test_fifo3 (vm, input))
1226 return -1;
1227 unformat_free (input);
1228
1229 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1230 unformat_init_cstring (input, str);
1231 if (tcp_test_fifo3 (vm, input))
1232 return -1;
1233 unformat_free (input);
1234
1235 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1236 unformat_init_cstring (input, str);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001237 if (tcp_test_fifo3 (vm, input))
1238 return -1;
1239 unformat_free (input);
1240 }
1241 else
1242 {
1243 if (unformat (input, "fifo3"))
1244 {
1245 res = tcp_test_fifo3 (vm, input);
1246 }
1247 else if (unformat (input, "fifo2"))
1248 {
1249 res = tcp_test_fifo2 (vm);
1250 }
1251 else if (unformat (input, "fifo1"))
1252 {
1253 res = tcp_test_fifo1 (vm, input);
1254 }
Florin Corasc28764f2017-04-26 00:08:42 -07001255 else if (unformat (input, "fifo4"))
1256 {
1257 res = tcp_test_fifo4 (vm, input);
1258 }
Dave Barach1f75cfd2017-04-14 16:46:44 -04001259 }
1260
1261 return res;
1262}
Florin Coras6cf30ad2017-04-04 23:08:23 -07001263
Dave Barach63681512017-04-20 17:50:39 -04001264static int
1265tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1266{
1267 int rv = 0;
1268 tcp_connection_t *tc0;
1269 u8 sst = SESSION_TYPE_IP4_TCP;
1270 ip4_address_t local, remote;
1271 u16 local_port, remote_port;
1272 tcp_main_t *tm = vnet_get_tcp_main ();
1273 int is_add = 1;
1274
1275
1276 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1277 {
1278 if (unformat (input, "del"))
1279 is_add = 0;
1280 else if (unformat (input, "add"))
1281 is_add = 1;
1282 else
1283 break;
1284 }
1285
1286 if (is_add)
1287 {
1288 local.as_u32 = clib_host_to_net_u32 (0x06000101);
1289 remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1290 local_port = clib_host_to_net_u16 (1234);
1291 remote_port = clib_host_to_net_u16 (11234);
1292
1293 pool_get (tm->connections[0], tc0);
1294 memset (tc0, 0, sizeof (*tc0));
1295
1296 tc0->state = TCP_STATE_ESTABLISHED;
1297 tc0->rcv_las = 1;
1298 tc0->c_c_index = tc0 - tm->connections[0];
1299 tc0->c_lcl_port = local_port;
1300 tc0->c_rmt_port = remote_port;
1301 tc0->c_is_ip4 = 1;
1302 tc0->c_thread_index = 0;
1303 tc0->c_lcl_ip4.as_u32 = local.as_u32;
1304 tc0->c_rmt_ip4.as_u32 = remote.as_u32;
Florin Coras93992a92017-05-24 18:03:56 -07001305 tc0->rcv_opts.mss = 1450;
Dave Barach63681512017-04-20 17:50:39 -04001306 tcp_connection_init_vars (tc0);
1307
1308 TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1309
1310 if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1311 sst, 0 /* notify */ ))
1312 clib_warning ("stream_session_accept failed");
1313
1314 stream_session_accept_notify (&tc0->connection);
1315 }
1316 else
1317 {
1318 tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1319 tc0->state = TCP_STATE_CLOSED;
1320 stream_session_disconnect_notify (&tc0->connection);
1321 }
1322
1323 return rv;
1324}
1325
Florin Coras6792ec02017-03-13 03:49:51 -07001326static clib_error_t *
1327tcp_test (vlib_main_t * vm,
1328 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1329{
1330 int res = 0;
1331
1332 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1333 {
1334 if (unformat (input, "sack"))
1335 {
Florin Coras45d34962017-04-25 00:05:27 -07001336 res = tcp_test_sack (vm, input);
Florin Coras6792ec02017-03-13 03:49:51 -07001337 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001338 else if (unformat (input, "fifo"))
1339 {
1340 res = tcp_test_fifo (vm, input);
1341 }
Dave Barach63681512017-04-20 17:50:39 -04001342 else if (unformat (input, "session"))
Florin Coras6792ec02017-03-13 03:49:51 -07001343 {
Dave Barach63681512017-04-20 17:50:39 -04001344 res = tcp_test_session (vm, input);
Florin Coras6792ec02017-03-13 03:49:51 -07001345 }
Dave Barach63681512017-04-20 17:50:39 -04001346 else
1347 break;
Florin Coras6792ec02017-03-13 03:49:51 -07001348 }
1349
1350 if (res)
1351 {
1352 return clib_error_return (0, "TCP unit test failed");
1353 }
1354 else
1355 {
1356 return 0;
1357 }
1358}
1359
Florin Coras6cf30ad2017-04-04 23:08:23 -07001360/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -07001361VLIB_CLI_COMMAND (tcp_test_command, static) =
1362{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001363 .path = "test tcp",
1364 .short_help = "internal tcp unit tests",
1365 .function = tcp_test,
1366};
1367/* *INDENT-ON* */
1368
1369
Florin Coras6792ec02017-03-13 03:49:51 -07001370/*
1371 * fd.io coding-style-patch-verification: ON
1372 *
1373 * Local Variables:
1374 * eval: (c-set-style "gnu")
1375 * End:
1376 */