test frame work pump thread exit: set flag then wake
[vpp.git] / src / vnet / tcp / tcp_test.c
1 /*
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  */
15 #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
37 /* *INDENT-OFF* */
38 scoreboard_trace_elt_t sb_trace[] = {};
39 /* *INDENT-ON* */
40
41 static int
42 tcp_test_scoreboard_replay (vlib_main_t * vm, unformat_input_t * input)
43 {
44   int verbose = 0;
45   tcp_connection_t _tc, *tc = &_tc;
46   u8 *s = 0;
47
48   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
49     {
50       if (unformat (input, "detail"))
51         verbose = 1;
52       else
53         {
54           clib_error_t *e = clib_error_return
55             (0, "unknown input `%U'", format_unformat_error, input);
56           clib_error_report (e);
57           return -1;
58         }
59     }
60
61 #if TCP_SCOREBOARD_TRACE
62   tc->sack_sb.trace = sb_trace;
63 #endif
64   s = tcp_scoreboard_replay (s, tc, verbose);
65   vlib_cli_output (vm, "%v", s);
66   return 0;
67 }
68
69 static int
70 tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
71 {
72   tcp_connection_t _tc, *tc = &_tc;
73   sack_scoreboard_t *sb = &tc->sack_sb;
74   sack_block_t *sacks = 0, block;
75   sack_scoreboard_hole_t *hole;
76   int i, verbose = 0;
77
78   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
79     {
80       if (unformat (input, "verbose"))
81         verbose = 1;
82       else if (unformat (input, "replay"))
83         return tcp_test_scoreboard_replay (vm, input);
84     }
85
86   memset (tc, 0, sizeof (*tc));
87
88   tc->snd_una = 0;
89   tc->snd_una_max = 1000;
90   tc->snd_nxt = 1000;
91   tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
92   scoreboard_init (&tc->sack_sb);
93
94   for (i = 0; i < 1000 / 100; i++)
95     {
96       block.start = i * 100;
97       block.end = (i + 1) * 100;
98       vec_add1 (sacks, block);
99     }
100
101   /*
102    * Inject even blocks
103    */
104
105   for (i = 0; i < 1000 / 200; i++)
106     {
107       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
108     }
109   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
110   tcp_rcv_sacks (tc, 0);
111
112   if (verbose)
113     vlib_cli_output (vm, "sb after even blocks:\n%U", format_tcp_scoreboard,
114                      sb);
115
116   TCP_TEST ((pool_elts (sb->holes) == 5),
117             "scoreboard has %d elements", pool_elts (sb->holes));
118
119   /* First SACK block should be rejected */
120   hole = scoreboard_first_hole (sb);
121   TCP_TEST ((hole->start == 0 && hole->end == 200),
122             "first hole start %u end %u", hole->start, hole->end);
123   hole = scoreboard_last_hole (sb);
124   TCP_TEST ((hole->start == 900 && hole->end == 1000),
125             "last hole start %u end %u", hole->start, hole->end);
126   TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
127   TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
128   TCP_TEST ((sb->last_sacked_bytes == 400),
129             "last sacked bytes %d", sb->last_sacked_bytes);
130   TCP_TEST ((sb->high_sacked == 900), "max byte sacked %u", sb->high_sacked);
131   /*
132    * Inject odd blocks
133    */
134
135   vec_reset_length (tc->rcv_opts.sacks);
136   for (i = 0; i < 1000 / 200; i++)
137     {
138       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
139     }
140   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
141   tcp_rcv_sacks (tc, 0);
142
143   if (verbose)
144     vlib_cli_output (vm, "sb after odd blocks:\n%U", format_tcp_scoreboard,
145                      sb);
146
147   hole = scoreboard_first_hole (sb);
148   TCP_TEST ((pool_elts (sb->holes) == 1),
149             "scoreboard has %d holes", pool_elts (sb->holes));
150   TCP_TEST ((hole->start == 0 && hole->end == 100),
151             "first hole start %u end %u", hole->start, hole->end);
152   TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
153   TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
154   TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
155   TCP_TEST ((sb->last_sacked_bytes == 500),
156             "last sacked bytes %d", sb->last_sacked_bytes);
157
158   /*
159    *  Ack until byte 100, all bytes are now acked + sacked
160    */
161   tcp_rcv_sacks (tc, 100);
162   if (verbose)
163     vlib_cli_output (vm, "ack until byte 100:\n%U", format_tcp_scoreboard,
164                      sb);
165
166   TCP_TEST ((pool_elts (sb->holes) == 0),
167             "scoreboard has %d elements", pool_elts (sb->holes));
168   TCP_TEST ((sb->snd_una_adv == 900),
169             "snd_una_adv after ack %u", sb->snd_una_adv);
170   TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
171   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
172   TCP_TEST ((sb->last_sacked_bytes == 0),
173             "last sacked bytes %d", sb->last_sacked_bytes);
174
175   /*
176    * Add new block
177    */
178
179   vec_reset_length (tc->rcv_opts.sacks);
180
181   block.start = 1200;
182   block.end = 1300;
183   vec_add1 (tc->rcv_opts.sacks, block);
184
185   if (verbose)
186     vlib_cli_output (vm, "add [1200, 1300]:\n%U", format_tcp_scoreboard, sb);
187   tc->snd_una_max = 1500;
188   tc->snd_una = 1000;
189   tc->snd_nxt = 1500;
190   tcp_rcv_sacks (tc, 1000);
191
192   if (verbose)
193     vlib_cli_output (vm, "sb snd_una_max 1500, snd_una 1000:\n%U",
194                      format_tcp_scoreboard, sb);
195
196   TCP_TEST ((sb->snd_una_adv == 0),
197             "snd_una_adv after ack %u", sb->snd_una_adv);
198   TCP_TEST ((pool_elts (sb->holes) == 2),
199             "scoreboard has %d holes", pool_elts (sb->holes));
200   hole = scoreboard_first_hole (sb);
201   TCP_TEST ((hole->start == 1000 && hole->end == 1200),
202             "first hole start %u end %u", hole->start, hole->end);
203   TCP_TEST ((sb->snd_una_adv == 0),
204             "snd_una_adv after ack %u", sb->snd_una_adv);
205   TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
206   hole = scoreboard_last_hole (sb);
207   TCP_TEST ((hole->start == 1300 && hole->end == 1500),
208             "last hole start %u end %u", hole->start, hole->end);
209   TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
210
211   /*
212    * Ack first hole
213    */
214
215   vec_reset_length (tc->rcv_opts.sacks);
216   tcp_rcv_sacks (tc, 1200);
217
218   if (verbose)
219     vlib_cli_output (vm, "sb ack up to byte 1200:\n%U", format_tcp_scoreboard,
220                      sb);
221
222   TCP_TEST ((sb->snd_una_adv == 100),
223             "snd_una_adv after ack %u", sb->snd_una_adv);
224   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
225   TCP_TEST ((pool_elts (sb->holes) == 1),
226             "scoreboard has %d elements", pool_elts (sb->holes));
227   hole = scoreboard_first_hole (sb);
228   TCP_TEST ((hole->prev == TCP_INVALID_SACK_HOLE_INDEX
229              && hole->next == TCP_INVALID_SACK_HOLE_INDEX), "hole is valid");
230   TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
231             sb->last_bytes_delivered);
232
233   /*
234    * Add some more blocks and then remove all
235    */
236   vec_reset_length (tc->rcv_opts.sacks);
237   tc->snd_una += sb->snd_una_adv;
238   tc->snd_una_max = 1900;
239   for (i = 0; i < 5; i++)
240     {
241       block.start = i * 100 + 1200;
242       block.end = (i + 1) * 100 + 1200;
243       vec_add1 (tc->rcv_opts.sacks, block);
244     }
245   tcp_rcv_sacks (tc, 1900);
246
247   scoreboard_clear (sb);
248   if (verbose)
249     vlib_cli_output (vm, "sb cleared all:\n%U", format_tcp_scoreboard, sb);
250
251   TCP_TEST ((pool_elts (sb->holes) == 0),
252             "number of holes %d", pool_elts (sb->holes));
253   TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
254   TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
255
256   /*
257    * Re-inject odd blocks and ack them all
258    */
259
260   tc->snd_una = 0;
261   tc->snd_una_max = 1000;
262   tc->snd_nxt = 1000;
263   for (i = 0; i < 5; i++)
264     {
265       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
266     }
267   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
268   tcp_rcv_sacks (tc, 0);
269   if (verbose)
270     vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
271                      format_tcp_scoreboard, sb);
272
273   tcp_rcv_sacks (tc, 950);
274
275   if (verbose)
276     vlib_cli_output (vm, "sb added odd blocks and ack [0, 950]:\n%U",
277                      format_tcp_scoreboard, sb);
278
279   TCP_TEST ((pool_elts (sb->holes) == 0),
280             "scoreboard has %d elements", pool_elts (sb->holes));
281   TCP_TEST ((sb->snd_una_adv == 50), "snd_una_adv %u", sb->snd_una_adv);
282   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
283   TCP_TEST ((sb->last_sacked_bytes == 0),
284             "last sacked bytes %d", sb->last_sacked_bytes);
285
286   /*
287    * Inject one block, ack it and overlap hole
288    */
289
290   tc->snd_una = 0;
291   tc->snd_una_max = 1000;
292   tc->snd_nxt = 1000;
293
294   block.start = 100;
295   block.end = 500;
296   vec_add1 (tc->rcv_opts.sacks, block);
297   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
298
299   tcp_rcv_sacks (tc, 0);
300
301   if (verbose)
302     vlib_cli_output (vm, "sb added [100, 500]:\n%U",
303                      format_tcp_scoreboard, sb);
304
305   tcp_rcv_sacks (tc, 800);
306
307   if (verbose)
308     vlib_cli_output (vm, "sb ack [0, 800]:\n%U", format_tcp_scoreboard, sb);
309
310   TCP_TEST ((pool_elts (sb->holes) == 1),
311             "scoreboard has %d elements", pool_elts (sb->holes));
312   TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
313   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
314   TCP_TEST ((sb->last_sacked_bytes == 0),
315             "last sacked bytes %d", sb->last_sacked_bytes);
316   TCP_TEST ((sb->last_bytes_delivered == 400),
317             "last bytes delivered %d", sb->last_bytes_delivered);
318
319   /*
320    * One hole close to head, patch head, split in two and start acking
321    * the lowest part
322    */
323   scoreboard_clear (sb);
324   tc->snd_una = 0;
325   tc->snd_una_max = 1000;
326   tc->snd_nxt = 1000;
327
328   block.start = 500;
329   block.end = 1000;
330   vec_add1 (tc->rcv_opts.sacks, block);
331   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
332
333   tcp_rcv_sacks (tc, 0);
334   if (verbose)
335     vlib_cli_output (vm, "sb added [500, 1000]:\n%U",
336                      format_tcp_scoreboard, sb);
337
338   vec_reset_length (tc->rcv_opts.sacks);
339   block.start = 300;
340   block.end = 400;
341   vec_add1 (tc->rcv_opts.sacks, block);
342   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
343   tcp_rcv_sacks (tc, 100);
344   if (verbose)
345     vlib_cli_output (vm, "sb added [0, 100] [300, 400]:\n%U",
346                      format_tcp_scoreboard, sb);
347   TCP_TEST ((pool_elts (sb->holes) == 2),
348             "scoreboard has %d elements", pool_elts (sb->holes));
349
350   tc->snd_una = 100;
351   tcp_rcv_sacks (tc, 200);
352   tcp_rcv_sacks (tc, 300);
353   if (verbose)
354     vlib_cli_output (vm, "sb added [0, 300]:\n%U", format_tcp_scoreboard, sb);
355   TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
356
357   return 0;
358 }
359
360 static int
361 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
362 {
363   tcp_connection_t _tc, *tc = &_tc;
364   sack_block_t *sacks;
365   int i, verbose = 0, expected;
366
367   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
368     {
369       if (unformat (input, "verbose"))
370         verbose = 1;
371       else
372         {
373           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
374                            input);
375           return -1;
376         }
377     }
378
379   memset (tc, 0, sizeof (*tc));
380
381   /*
382    * Add odd sack block pairs
383    */
384   for (i = 1; i < 10; i += 2)
385     {
386       tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
387     }
388
389   TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
390             vec_len (tc->snd_sacks), 5);
391   TCP_TEST ((tc->snd_sacks[0].start = 900),
392             "first sack block start %u expected %u", tc->snd_sacks[0].start,
393             900);
394
395   /*
396    * Try to add one extra
397    */
398   sacks = vec_dup (tc->snd_sacks);
399
400   tcp_update_sack_list (tc, 1100, 1200);
401   if (verbose)
402     vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
403                      format_tcp_sacks, tc);
404   expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
405   TCP_TEST ((vec_len (tc->snd_sacks) == expected),
406             "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
407   TCP_TEST ((tc->snd_sacks[0].start == 1100),
408             "first sack block start %u expected %u", tc->snd_sacks[0].start,
409             1100);
410
411   /* restore */
412   vec_free (tc->snd_sacks);
413   tc->snd_sacks = sacks;
414
415   /*
416    * Overlap first 2 segment
417    */
418   tc->rcv_nxt = 300;
419   tcp_update_sack_list (tc, 300, 300);
420   if (verbose)
421     vlib_cli_output (vm, "overlap first 2 segments:\n%U",
422                      format_tcp_sacks, tc);
423   TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
424             vec_len (tc->snd_sacks), 3);
425   TCP_TEST ((tc->snd_sacks[0].start == 900),
426             "first sack block start %u expected %u", tc->snd_sacks[0].start,
427             500);
428
429   /*
430    * Add a new segment
431    */
432   tcp_update_sack_list (tc, 1100, 1200);
433   if (verbose)
434     vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
435                      format_tcp_sacks, tc);
436   TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
437             vec_len (tc->snd_sacks), 4);
438   TCP_TEST ((tc->snd_sacks[0].start == 1100),
439             "first sack block start %u expected %u", tc->snd_sacks[0].start,
440             1100);
441
442   /*
443    * Join middle segments
444    */
445   tcp_update_sack_list (tc, 800, 900);
446   if (verbose)
447     vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
448                      format_tcp_sacks, tc);
449
450   TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
451             vec_len (tc->snd_sacks), 3);
452   TCP_TEST ((tc->snd_sacks[0].start == 700),
453             "first sack block start %u expected %u", tc->snd_sacks[0].start,
454             1100);
455
456   /*
457    * Advance rcv_nxt to overlap all
458    */
459   tc->rcv_nxt = 1200;
460   tcp_update_sack_list (tc, 1200, 1200);
461   if (verbose)
462     vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
463   TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
464             vec_len (tc->snd_sacks), 0);
465
466
467   /*
468    * Add 2 blocks, overwrite first and update rcv_nxt to also remove it
469    */
470
471   vec_reset_length (tc->snd_sacks);
472   tc->rcv_nxt = 0;
473
474   tcp_update_sack_list (tc, 100, 200);
475   tcp_update_sack_list (tc, 300, 400);
476
477   if (verbose)
478     vlib_cli_output (vm, "add [100, 200] [300, 400]\n%U",
479                      format_tcp_sacks, tc);
480   TCP_TEST ((vec_len (tc->snd_sacks) == 2),
481             "sack blocks %d expected %d", vec_len (tc->snd_sacks), 2);
482   TCP_TEST ((tc->snd_sacks[0].start == 300),
483             "first sack block start %u expected %u", tc->snd_sacks[0].start,
484             300);
485
486   tc->rcv_nxt = 100;
487   tcp_update_sack_list (tc, 100, 100);
488   if (verbose)
489     vlib_cli_output (vm, "add [100, 200] rcv_nxt = 100\n%U",
490                      format_tcp_sacks, tc);
491   TCP_TEST ((vec_len (tc->snd_sacks) == 1),
492             "sack blocks %d expected %d", vec_len (tc->snd_sacks), 1);
493   TCP_TEST ((tc->snd_sacks[0].start == 300),
494             "first sack block start %u expected %u", tc->snd_sacks[0].start,
495             300);
496   return 0;
497 }
498
499 static int
500 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
501 {
502   int res = 0;
503
504   /* Run all tests */
505   if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
506     {
507       if (tcp_test_sack_tx (vm, input))
508         {
509           return -1;
510         }
511
512       if (tcp_test_sack_rx (vm, input))
513         {
514           return -1;
515         }
516     }
517   else
518     {
519       if (unformat (input, "tx"))
520         {
521           res = tcp_test_sack_tx (vm, input);
522         }
523       else if (unformat (input, "rx"))
524         {
525           res = tcp_test_sack_rx (vm, input);
526         }
527     }
528
529   return res;
530 }
531
532
533 typedef struct
534 {
535   u32 offset;
536   u32 len;
537 } test_pattern_t;
538
539 /* *INDENT-OFF* */
540 test_pattern_t test_pattern[] = {
541   {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
542   {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
543   {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
544   {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
545   {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
546   {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
547   {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
548   {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
549   {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
550   {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
551   {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
552   {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
553   {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
554   {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
555   {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
556   {664, 8}, {1052, 8}, {1440, 8}, {1828, 8},  {2216, 8}, {2604, 8}, {2888, 8},
557   {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
558   {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
559   {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
560   {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
561   {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
562   {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
563   {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
564   {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
565   {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
566   {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
567   {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
568   {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
569   {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
570   {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
571   {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
572   {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
573   {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
574   {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
575   {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
576   {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
577   {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
578   {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
579   {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
580   {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
581   {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
582   {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
583   {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
584   {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
585   {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
586   {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
587   {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
588   {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
589   {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
590   {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
591   {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
592   {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
593   {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
594   {2332, 8},
595   /* missing from original data set */
596   {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
597 };
598 /* *INDENT-ON* */
599
600 int
601 pattern_cmp (const void *arg1, const void *arg2)
602 {
603   test_pattern_t *a1 = (test_pattern_t *) arg1;
604   test_pattern_t *a2 = (test_pattern_t *) arg2;
605
606   if (a1->offset < a2->offset)
607     return -1;
608   else if (a1->offset > a2->offset)
609     return 1;
610   return 0;
611 }
612
613 static u8
614 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
615                        u32 pattern_length)
616 {
617   test_pattern_t *tp = pattern;
618   int i;
619
620   /* Go through the pattern and make 100% sure it's sane */
621   for (i = 0; i < pattern_length - 1; i++)
622     {
623       if (tp->offset + tp->len != (tp + 1)->offset)
624         {
625           vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
626                            (tp->offset + tp->len),
627                            (tp + 1)->offset - (tp->offset + tp->len));
628           return 0;
629         }
630       tp++;
631     }
632   return 1;
633 }
634
635 static test_pattern_t *
636 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
637                            u32 test_data_len)
638 {
639   test_pattern_t *validate_pattern = 0;
640
641   /* Validate, and try segments in order... */
642   vec_validate (validate_pattern, test_data_len - 1);
643   memcpy (validate_pattern, test_data,
644           test_data_len * sizeof (test_pattern_t));
645   qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
646          pattern_cmp);
647
648   if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
649     return 0;
650
651   return validate_pattern;
652 }
653
654 static svm_fifo_t *
655 fifo_prepare (u32 fifo_size)
656 {
657   svm_fifo_t *f;
658   f = svm_fifo_create (fifo_size);
659
660   /* Paint fifo data vector with -1's */
661   memset (f->data, 0xFF, fifo_size);
662
663   return f;
664 }
665
666 static int
667 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
668 {
669   int i;
670
671   for (i = start; i < len; i++)
672     {
673       if (data1[i] != data2[i])
674         {
675           *index = i;
676           return 1;
677         }
678     }
679   return 0;
680 }
681
682 int
683 tcp_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
684 {
685   svm_fifo_t *f;
686   u32 fifo_size = 1 << 20;
687   u32 *test_data = 0;
688   u32 offset;
689   int i, rv, verbose = 0;
690   u32 data_word, test_data_len, j;
691   ooo_segment_t *ooo_seg;
692   u8 *data, *s, *data_buf = 0;
693
694   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
695     {
696       if (unformat (input, "verbose"))
697         verbose = 1;
698     }
699
700   test_data_len = fifo_size / sizeof (u32);
701   vec_validate (test_data, test_data_len - 1);
702
703   for (i = 0; i < vec_len (test_data); i++)
704     test_data[i] = i;
705
706   f = fifo_prepare (fifo_size);
707
708   /*
709    * Enqueue an initial (un-dequeued) chunk
710    */
711   rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data);
712   TCP_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
713   TCP_TEST ((f->tail == 4), "fifo tail %u", f->tail);
714
715   /*
716    * Create 3 chunks in the future. The offsets are relative
717    * to the current fifo tail
718    */
719   for (i = 0; i < 3; i++)
720     {
721       offset = (2 * i + 1) * sizeof (u32) - f->tail;
722       data = (u8 *) (test_data + (2 * i + 1));
723       if (i == 0)
724         {
725           rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data);
726           rv = rv > 0 ? 0 : rv;
727         }
728       else
729         rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
730       if (verbose)
731         vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
732                          offset + sizeof (u32));
733       if (rv)
734         {
735           clib_warning ("enqueue returned %d", rv);
736           goto err;
737         }
738     }
739
740   if (verbose)
741     vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
742
743   TCP_TEST ((f->tail == 8), "fifo tail %u", f->tail);
744   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
745             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
746
747   /*
748    * Try adding a completely overlapped segment
749    */
750   offset = 3 * sizeof (u32) - f->tail;
751   data = (u8 *) (test_data + 3);
752   rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
753   if (rv)
754     {
755       clib_warning ("enqueue returned %d", rv);
756       goto err;
757     }
758
759   if (verbose)
760     vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
761
762   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
763             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
764
765   /*
766    * Make sure format functions are not buggy
767    */
768   s = format (0, "%U", format_svm_fifo, f, 2);
769   vec_free (s);
770
771   /*
772    * Paint some of missing data backwards
773    */
774   for (i = 3; i > 1; i--)
775     {
776       offset = (2 * i + 0) * sizeof (u32) - f->tail;
777       data = (u8 *) (test_data + (2 * i + 0));
778       rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
779       if (verbose)
780         vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
781                          offset + sizeof (u32));
782       if (rv)
783         {
784           clib_warning ("enqueue returned %d", rv);
785           goto err;
786         }
787     }
788
789   if (verbose)
790     vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
791                      1);
792   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
793             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
794   ooo_seg = svm_fifo_first_ooo_segment (f);
795   TCP_TEST ((ooo_seg->start == 12),
796             "first ooo seg position %u", ooo_seg->start);
797   TCP_TEST ((ooo_seg->length == 16),
798             "first ooo seg length %u", ooo_seg->length);
799
800   /*
801    * Enqueue the missing u32
802    */
803   rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2));
804   if (verbose)
805     vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
806                      1);
807   TCP_TEST ((rv == 20), "bytes to be enqueued %u", rv);
808   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
809             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
810
811   /*
812    * Collect results
813    */
814   for (i = 0; i < 7; i++)
815     {
816       rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word);
817       if (rv != sizeof (u32))
818         {
819           clib_warning ("bytes dequeues %u", rv);
820           goto err;
821         }
822       if (data_word != test_data[i])
823         {
824           clib_warning ("recovered [%d] %d not %d", i, data_word,
825                         test_data[i]);
826           goto err;
827         }
828     }
829
830   /*
831    * Test segment overlaps: last ooo segment overlaps all
832    */
833   svm_fifo_free (f);
834   f = fifo_prepare (fifo_size);
835
836   for (i = 0; i < 4; i++)
837     {
838       offset = (2 * i + 1) * sizeof (u32) - f->tail;
839       data = (u8 *) (test_data + (2 * i + 1));
840       rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
841       if (verbose)
842         vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
843                          offset + sizeof (u32));
844       if (rv)
845         {
846           clib_warning ("enqueue returned %d", rv);
847           goto err;
848         }
849     }
850
851   rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data);
852   TCP_TEST ((rv == 0), "ooo enqueued %u", rv);
853   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
854             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
855
856   vec_validate (data_buf, vec_len (data));
857   svm_fifo_peek (f, 0, vec_len (data), data_buf);
858   if (compare_data (data_buf, data, 8, vec_len (data), &j))
859     {
860       TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
861     }
862   vec_reset_length (data_buf);
863
864   /*
865    * Test segment overlaps: enqueue and overlap ooo segments
866    */
867   svm_fifo_free (f);
868   f = fifo_prepare (fifo_size);
869
870   for (i = 0; i < 4; i++)
871     {
872       offset = (2 * i + 1) * sizeof (u32) - f->tail;
873       data = (u8 *) (test_data + (2 * i + 1));
874       rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
875       if (verbose)
876         vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
877                          offset + sizeof (u32));
878       if (rv)
879         {
880           clib_warning ("enqueue returned %d", rv);
881           goto err;
882         }
883     }
884
885   if (verbose)
886     vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
887
888   rv = svm_fifo_enqueue_nowait (f, 29, data);
889   if (verbose)
890     vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
891                      1);
892   TCP_TEST ((rv == 32), "ooo enqueued %u", rv);
893   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
894             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
895
896   vec_validate (data_buf, vec_len (data));
897   svm_fifo_peek (f, 0, vec_len (data), data_buf);
898   if (compare_data (data_buf, data, 0, vec_len (data), &j))
899     {
900       TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
901     }
902
903   /* Try to peek beyond the data */
904   rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
905   TCP_TEST ((rv == 0), "peeked %u expected 0", rv);
906
907   vec_free (data_buf);
908   svm_fifo_free (f);
909   vec_free (test_data);
910
911   return 0;
912
913 err:
914   svm_fifo_free (f);
915   vec_free (test_data);
916   return -1;
917 }
918
919 static int
920 tcp_test_fifo2 (vlib_main_t * vm)
921 {
922   svm_fifo_t *f;
923   u32 fifo_size = 1 << 20;
924   int i, rv, test_data_len;
925   u64 data64;
926   test_pattern_t *tp, *vp, *test_data;
927   ooo_segment_t *ooo_seg;
928
929   test_data = test_pattern;
930   test_data_len = ARRAY_LEN (test_pattern);
931
932   vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
933
934   /* Create a fifo */
935   f = fifo_prepare (fifo_size);
936
937   /*
938    * Try with sorted data
939    */
940   for (i = 0; i < test_data_len; i++)
941     {
942       tp = vp + i;
943       data64 = tp->offset;
944       svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
945                                     (u8 *) & data64);
946     }
947
948   /* Expected result: one big fat chunk at offset 4 */
949   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
950             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
951   ooo_seg = svm_fifo_first_ooo_segment (f);
952   TCP_TEST ((ooo_seg->start == 4),
953             "first ooo seg position %u", ooo_seg->start);
954   TCP_TEST ((ooo_seg->length == 2996),
955             "first ooo seg length %u", ooo_seg->length);
956
957   data64 = 0;
958   rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
959   TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
960
961   svm_fifo_free (f);
962   vec_free (vp);
963
964   /*
965    * Now try it again w/ unsorted data...
966    */
967
968   f = fifo_prepare (fifo_size);
969
970   for (i = 0; i < test_data_len; i++)
971     {
972       tp = &test_data[i];
973       data64 = tp->offset;
974       rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
975                                          (u8 *) & data64);
976       if (rv)
977         {
978           clib_warning ("enqueue returned %d", rv);
979         }
980     }
981
982   /* Expecting the same result: one big fat chunk at offset 4 */
983   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
984             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
985   ooo_seg = svm_fifo_first_ooo_segment (f);
986   TCP_TEST ((ooo_seg->start == 4),
987             "first ooo seg position %u", ooo_seg->start);
988   TCP_TEST ((ooo_seg->length == 2996),
989             "first ooo seg length %u", ooo_seg->length);
990
991   data64 = 0;
992   rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64);
993
994   TCP_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
995
996   svm_fifo_free (f);
997
998   return 0;
999 }
1000
1001 static int
1002 tcp_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
1003 {
1004   svm_fifo_t *f;
1005   u32 fifo_size = 4 << 10;
1006   u32 fifo_initial_offset = 0;
1007   u32 total_size = 2 << 10;
1008   int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
1009   u8 *data_pattern = 0, *data_buf = 0;
1010   test_pattern_t *tp, *generate = 0;
1011   u32 nsegs = 2, seg_size, length_so_far;
1012   u32 current_offset, offset_increment, len_this_chunk;
1013   u32 seed = 0xdeaddabe, j;
1014   int i, rv;
1015
1016   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1017     {
1018       if (unformat (input, "fifo-size %d", &fifo_size))
1019         ;
1020       else if (unformat (input, "total-size %d", &total_size))
1021         ;
1022       else if (unformat (input, "verbose"))
1023         verbose = 1;
1024       else if (unformat (input, "overlap"))
1025         overlap = 1;
1026       else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
1027         ;
1028       else if (unformat (input, "seed %d", &seed))
1029         ;
1030       else if (unformat (input, "nsegs %d", &nsegs))
1031         ;
1032       else if (unformat (input, "no-randomize"))
1033         randomize = 0;
1034       else if (unformat (input, "in-seq-all"))
1035         in_seq_all = 1;
1036       else if (unformat (input, "drop"))
1037         drop = 1;
1038       else
1039         {
1040           clib_error_t *e = clib_error_return
1041             (0, "unknown input `%U'", format_unformat_error, input);
1042           clib_error_report (e);
1043           return -1;
1044         }
1045     }
1046
1047   if (total_size > fifo_size)
1048     {
1049       clib_warning ("total_size %d greater than fifo size %d", total_size,
1050                     fifo_size);
1051       return -1;
1052     }
1053   if (overlap && randomize == 0)
1054     {
1055       clib_warning ("Can't enqueue in-order with overlap");
1056       return -1;
1057     }
1058
1059   /*
1060    * Generate data
1061    */
1062   vec_validate (data_pattern, total_size - 1);
1063   for (i = 0; i < vec_len (data_pattern); i++)
1064     data_pattern[i] = i & 0xff;
1065
1066   /*
1067    * Generate segments
1068    */
1069   seg_size = total_size / nsegs;
1070   length_so_far = 0;
1071   current_offset = randomize;
1072   while (length_so_far < total_size)
1073     {
1074       vec_add2 (generate, tp, 1);
1075       len_this_chunk = clib_min (seg_size, total_size - length_so_far);
1076       tp->offset = current_offset;
1077       tp->len = len_this_chunk;
1078
1079       if (overlap && (len_this_chunk == seg_size))
1080         do
1081           {
1082             offset_increment = len_this_chunk
1083               % (1 + (random_u32 (&seed) % len_this_chunk));
1084           }
1085         while (offset_increment == 0);
1086       else
1087         offset_increment = len_this_chunk;
1088
1089       current_offset += offset_increment;
1090       length_so_far = tp->offset + tp->len;
1091     }
1092
1093   /*
1094    * Validate segment list. Only valid for non-overlap cases.
1095    */
1096   if (overlap == 0)
1097     fifo_validate_pattern (vm, generate, vec_len (generate));
1098
1099   if (verbose)
1100     {
1101       vlib_cli_output (vm, "raw data pattern:");
1102       for (i = 0; i < vec_len (generate); i++)
1103         {
1104           vlib_cli_output (vm, "[%d] offset %u len %u", i,
1105                            generate[i].offset, generate[i].len);
1106         }
1107     }
1108
1109   /* Randomize data pattern */
1110   if (randomize)
1111     {
1112       for (i = 0; i < vec_len (generate) / 2; i++)
1113         {
1114           u32 src_index, dst_index;
1115           test_pattern_t _tmp, *tmp = &_tmp;
1116
1117           src_index = random_u32 (&seed) % vec_len (generate);
1118           dst_index = random_u32 (&seed) % vec_len (generate);
1119
1120           tmp[0] = generate[dst_index];
1121           generate[dst_index] = generate[src_index];
1122           generate[src_index] = tmp[0];
1123         }
1124       if (verbose)
1125         {
1126           vlib_cli_output (vm, "randomized data pattern:");
1127           for (i = 0; i < vec_len (generate); i++)
1128             {
1129               vlib_cli_output (vm, "[%d] offset %u len %u", i,
1130                                generate[i].offset, generate[i].len);
1131             }
1132         }
1133     }
1134
1135   /*
1136    * Create a fifo and add segments
1137    */
1138   f = fifo_prepare (fifo_size);
1139
1140   /* manually set head and tail pointers to validate modular arithmetic */
1141   fifo_initial_offset = fifo_initial_offset % fifo_size;
1142   f->head = fifo_initial_offset;
1143   f->tail = fifo_initial_offset;
1144
1145   for (i = !randomize; i < vec_len (generate); i++)
1146     {
1147       tp = generate + i;
1148       svm_fifo_enqueue_with_offset (f,
1149                                     fifo_initial_offset + tp->offset -
1150                                     f->tail, tp->len,
1151                                     (u8 *) data_pattern + tp->offset);
1152     }
1153
1154   /* Add the first segment in order for non random data */
1155   if (!randomize)
1156     svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern);
1157
1158   /*
1159    * Expected result: one big fat chunk at offset 1 if randomize == 1
1160    */
1161
1162   if (verbose)
1163     vlib_cli_output (vm, "fifo before missing link: %U",
1164                      format_svm_fifo, f, 1 /* verbose */ );
1165
1166   /*
1167    * Add the missing byte if segments were randomized
1168    */
1169   if (randomize)
1170     {
1171       u32 bytes_to_enq = 1;
1172       if (in_seq_all)
1173         bytes_to_enq = total_size;
1174       rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0);
1175
1176       if (verbose)
1177         vlib_cli_output (vm, "in-order enqueue returned %d", rv);
1178
1179       TCP_TEST ((rv == total_size), "enqueued %u expected %u", rv,
1180                 total_size);
1181
1182     }
1183
1184   TCP_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
1185             svm_fifo_number_ooo_segments (f));
1186
1187   /*
1188    * Test if peeked data is the same as original data
1189    */
1190   vec_validate (data_buf, vec_len (data_pattern));
1191   svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
1192   if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1193     {
1194       TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1195                 data_pattern[j]);
1196     }
1197   vec_reset_length (data_buf);
1198
1199   /*
1200    * Dequeue or drop all data
1201    */
1202   if (drop)
1203     {
1204       svm_fifo_dequeue_drop (f, vec_len (data_pattern));
1205     }
1206   else
1207     {
1208       svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf);
1209       if (compare_data
1210           (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
1211         {
1212           TCP_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1213                     data_pattern[j]);
1214         }
1215     }
1216
1217   TCP_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
1218             svm_fifo_max_dequeue (f));
1219
1220   svm_fifo_free (f);
1221   vec_free (data_pattern);
1222   vec_free (data_buf);
1223
1224   return 0;
1225 }
1226
1227 static int
1228 tcp_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
1229 {
1230   svm_fifo_t *f;
1231   u32 fifo_size = 6 << 10;
1232   u32 fifo_initial_offset = 1000000000;
1233   u32 test_n_bytes = 5000, j;
1234   u8 *test_data = 0, *data_buf = 0;
1235   int i, rv, verbose = 0;
1236
1237   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1238     {
1239       if (unformat (input, "verbose"))
1240         verbose = 1;
1241       else
1242         {
1243           clib_error_t *e = clib_error_return
1244             (0, "unknown input `%U'", format_unformat_error, input);
1245           clib_error_report (e);
1246           return -1;
1247         }
1248     }
1249
1250   /*
1251    * Create a fifo and add segments
1252    */
1253   f = fifo_prepare (fifo_size);
1254
1255   /* Set head and tail pointers */
1256   fifo_initial_offset = fifo_initial_offset % fifo_size;
1257   svm_fifo_init_pointers (f, fifo_initial_offset);
1258
1259   vec_validate (test_data, test_n_bytes - 1);
1260   for (i = 0; i < vec_len (test_data); i++)
1261     test_data[i] = i;
1262
1263   for (i = test_n_bytes - 1; i > 0; i--)
1264     {
1265       rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
1266                                          sizeof (u8), &test_data[i]);
1267       if (verbose)
1268         vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
1269       if (rv)
1270         {
1271           clib_warning ("enqueue returned %d", rv);
1272           svm_fifo_free (f);
1273           vec_free (test_data);
1274           return -1;
1275         }
1276     }
1277
1278   svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]);
1279
1280   vec_validate (data_buf, vec_len (test_data));
1281
1282   svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf);
1283   rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
1284   if (rv)
1285     vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
1286                      test_data[j]);
1287   TCP_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1288
1289   svm_fifo_free (f);
1290   vec_free (test_data);
1291   return 0;
1292 }
1293
1294 static u32
1295 fifo_pos (svm_fifo_t * f, u32 pos)
1296 {
1297   return pos % f->nitems;
1298 }
1299
1300 static int
1301 tcp_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
1302 {
1303   svm_fifo_t *f;
1304   u32 fifo_size = 400, j = 0, offset = 200;
1305   int i, rv, verbose = 0;
1306   u8 *test_data = 0, *data_buf = 0;
1307   ooo_segment_t *ooo_seg;
1308
1309   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1310     {
1311       if (unformat (input, "verbose"))
1312         verbose = 1;
1313       else
1314         {
1315           clib_error_t *e = clib_error_return
1316             (0, "unknown input `%U'", format_unformat_error, input);
1317           clib_error_report (e);
1318           return -1;
1319         }
1320     }
1321
1322   f = fifo_prepare (fifo_size);
1323   svm_fifo_init_pointers (f, offset);
1324
1325   vec_validate (test_data, 399);
1326   for (i = 0; i < vec_len (test_data); i++)
1327     test_data[i] = i % 0xff;
1328
1329   /*
1330    * Start with [100, 200] and [300, 400]
1331    */
1332   svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1333   svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
1334
1335   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 2),
1336             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1337   TCP_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
1338   if (verbose)
1339     vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
1340                      format_svm_fifo, f, 2 /* verbose */ );
1341
1342   /*
1343    * Add [225, 275]
1344    */
1345
1346   rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[200]);
1347   if (verbose)
1348     vlib_cli_output (vm, "fifo after [225, 275] : %U",
1349                      format_svm_fifo, f, 2 /* verbose */ );
1350   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 3),
1351             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1352   ooo_seg = svm_fifo_first_ooo_segment (f);
1353   TCP_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
1354             "first seg start %u expected %u", ooo_seg->start,
1355             fifo_pos (f, 100 + offset));
1356   TCP_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
1357             ooo_seg->length, 100);
1358   ooo_seg = ooo_segment_next (f, ooo_seg);
1359   TCP_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
1360             "second seg start %u expected %u",
1361             ooo_seg->start, fifo_pos (f, 225 + offset));
1362   TCP_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
1363             ooo_seg->length, 50);
1364   ooo_seg = ooo_segment_next (f, ooo_seg);
1365   TCP_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
1366             "third seg start %u expected %u",
1367             ooo_seg->start, fifo_pos (f, 300 + offset));
1368   TCP_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
1369             ooo_seg->length, 100);
1370   TCP_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
1371   /*
1372    * Add [190, 310]
1373    */
1374   rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
1375   if (verbose)
1376     vlib_cli_output (vm, "fifo after [190, 310] : %U",
1377                      format_svm_fifo, f, 1 /* verbose */ );
1378   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1379             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1380   ooo_seg = svm_fifo_first_ooo_segment (f);
1381   TCP_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
1382             "first seg start %u expected %u",
1383             ooo_seg->start, fifo_pos (f, offset + 100));
1384   TCP_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
1385             ooo_seg->length, 300);
1386
1387   /*
1388    * Add [0, 150]
1389    */
1390   rv = svm_fifo_enqueue_nowait (f, 150, test_data);
1391
1392   if (verbose)
1393     vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
1394                      2 /* verbose */ );
1395
1396   TCP_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
1397   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 0),
1398             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1399
1400   vec_validate (data_buf, 399);
1401   svm_fifo_peek (f, 0, 400, data_buf);
1402   if (compare_data (data_buf, test_data, 0, 400, &j))
1403     {
1404       TCP_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
1405                 test_data[j]);
1406     }
1407
1408   /*
1409    * Add [100 200] and overlap it with [50 250]
1410    */
1411   svm_fifo_free (f);
1412   f = fifo_prepare (fifo_size);
1413
1414   svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
1415   svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
1416   TCP_TEST ((svm_fifo_number_ooo_segments (f) == 1),
1417             "number of ooo segments %u", svm_fifo_number_ooo_segments (f));
1418   ooo_seg = svm_fifo_first_ooo_segment (f);
1419   TCP_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
1420             ooo_seg->start, 50);
1421   TCP_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
1422             ooo_seg->length, 200);
1423
1424   svm_fifo_free (f);
1425   vec_free (test_data);
1426   return 0;
1427 }
1428
1429 /* *INDENT-OFF* */
1430 svm_fifo_trace_elem_t fifo_trace[] = {};
1431 /* *INDENT-ON* */
1432
1433 static int
1434 tcp_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
1435 {
1436   svm_fifo_t f;
1437   int verbose = 0;
1438   u8 no_read = 0, *str = 0;
1439
1440   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1441     {
1442       if (unformat (input, "verbose"))
1443         verbose = 1;
1444       else if (unformat (input, "no-read"))
1445         no_read = 1;
1446       else
1447         {
1448           clib_error_t *e = clib_error_return
1449             (0, "unknown input `%U'", format_unformat_error, input);
1450           clib_error_report (e);
1451           return -1;
1452         }
1453     }
1454
1455 #if SVMF_FIFO_TRACE
1456   f.trace = fifo_trace;
1457 #endif
1458
1459   str = svm_fifo_replay (str, &f, no_read, verbose);
1460   vlib_cli_output (vm, "%v", str);
1461   return 0;
1462 }
1463
1464 static int
1465 tcp_test_fifo (vlib_main_t * vm, unformat_input_t * input)
1466 {
1467   int res = 0;
1468   char *str;
1469
1470   /* Run all tests */
1471   if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
1472     {
1473       res = tcp_test_fifo1 (vm, input);
1474       if (res)
1475         return res;
1476
1477       res = tcp_test_fifo2 (vm);
1478       if (res)
1479         return res;
1480
1481       /*
1482        * Run a number of fifo3 configs
1483        */
1484       str = "nsegs 10 overlap seed 123";
1485       unformat_init_cstring (input, str);
1486       if (tcp_test_fifo3 (vm, input))
1487         return -1;
1488       unformat_free (input);
1489
1490       str = "nsegs 10 overlap seed 123 in-seq-all";
1491       unformat_init_cstring (input, str);
1492       if (tcp_test_fifo3 (vm, input))
1493         return -1;
1494       unformat_free (input);
1495
1496       str = "nsegs 10 overlap seed 123 initial-offset 3917";
1497       unformat_init_cstring (input, str);
1498       if (tcp_test_fifo3 (vm, input))
1499         return -1;
1500       unformat_free (input);
1501
1502       str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
1503       unformat_init_cstring (input, str);
1504       if (tcp_test_fifo3 (vm, input))
1505         return -1;
1506       unformat_free (input);
1507
1508       str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
1509       unformat_init_cstring (input, str);
1510       if (tcp_test_fifo3 (vm, input))
1511         return -1;
1512       unformat_free (input);
1513
1514       res = tcp_test_fifo4 (vm, input);
1515       if (res)
1516         return res;
1517
1518       res = tcp_test_fifo5 (vm, input);
1519       if (res)
1520         return res;
1521     }
1522   else
1523     {
1524       if (unformat (input, "fifo3"))
1525         {
1526           res = tcp_test_fifo3 (vm, input);
1527         }
1528       else if (unformat (input, "fifo2"))
1529         {
1530           res = tcp_test_fifo2 (vm);
1531         }
1532       else if (unformat (input, "fifo1"))
1533         {
1534           res = tcp_test_fifo1 (vm, input);
1535         }
1536       else if (unformat (input, "fifo4"))
1537         {
1538           res = tcp_test_fifo4 (vm, input);
1539         }
1540       else if (unformat (input, "fifo5"))
1541         {
1542           res = tcp_test_fifo5 (vm, input);
1543         }
1544       else if (unformat (input, "replay"))
1545         {
1546           res = tcp_test_fifo_replay (vm, input);
1547         }
1548     }
1549
1550   return res;
1551 }
1552
1553 static int
1554 tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
1555 {
1556   session_manager_main_t *smm = &session_manager_main;
1557   tcp_main_t *tm = &tcp_main;
1558   transport_connection_t _tc1, *tc1 = &_tc1, _tc2, *tc2 = &_tc2, *tconn;
1559   tcp_connection_t *tc;
1560   stream_session_t *s, *s1;
1561   u8 cmp = 0, is_filtered = 0;
1562
1563   /*
1564    * Allocate fake session and connection 1
1565    */
1566   pool_get (smm->sessions[0], s);
1567   memset (s, 0, sizeof (*s));
1568   s->session_index = s - smm->sessions[0];
1569
1570   pool_get (tm->connections[0], tc);
1571   memset (tc, 0, sizeof (*tc));
1572   tc->connection.c_index = tc - tm->connections[0];
1573   tc->connection.s_index = s->session_index;
1574   s->connection_index = tc->connection.c_index;
1575
1576   tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1577   tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000103);
1578   tc->connection.lcl_port = 35051;
1579   tc->connection.rmt_port = 53764;
1580   tc->connection.proto = TRANSPORT_PROTO_TCP;
1581   tc->connection.is_ip4 = 1;
1582   clib_memcpy (tc1, &tc->connection, sizeof (*tc1));
1583   s1 = s;
1584
1585   /*
1586    * Allocate fake session and connection 2
1587    */
1588   pool_get (session_manager_main.sessions[0], s);
1589   memset (s, 0, sizeof (*s));
1590   s->session_index = s - smm->sessions[0];
1591
1592   pool_get (tm->connections[0], tc);
1593   memset (tc, 0, sizeof (*tc));
1594   tc->connection.c_index = tc - tm->connections[0];
1595   tc->connection.s_index = s->session_index;
1596   s->connection_index = tc->connection.c_index;
1597
1598   tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
1599   tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000102);
1600   tc->connection.lcl_port = 38225;
1601   tc->connection.rmt_port = 53764;
1602   tc->connection.proto = TRANSPORT_PROTO_TCP;
1603   tc->connection.is_ip4 = 1;
1604   clib_memcpy (tc2, &tc->connection, sizeof (*tc2));
1605
1606   /*
1607    * Confirm that connection lookup works
1608    */
1609
1610   session_lookup_add_connection (tc1, session_handle (s1));
1611   tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1612                                          &tc1->rmt_ip.ip4,
1613                                          tc1->lcl_port, tc1->rmt_port,
1614                                          tc1->proto, 0, &is_filtered);
1615
1616   TCP_TEST ((tconn != 0), "connection exists");
1617   cmp = (memcmp (&tconn->rmt_ip, &tc1->rmt_ip, sizeof (tc1->rmt_ip)) == 0);
1618   TCP_TEST ((cmp), "rmt ip is identical %d", cmp);
1619   TCP_TEST ((tconn->lcl_port == tc1->lcl_port),
1620             "rmt port is identical %d", tconn->lcl_port == tc1->lcl_port);
1621
1622   /*
1623    * Non-existing connection lookup should not work
1624    */
1625
1626   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1627                                          &tc2->rmt_ip.ip4,
1628                                          tc2->lcl_port, tc2->rmt_port,
1629                                          tc2->proto, 0, &is_filtered);
1630   TCP_TEST ((tconn == 0), "lookup result should be null");
1631
1632   /*
1633    * Delete and lookup again
1634    */
1635   session_lookup_del_connection (tc1);
1636   tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
1637                                          &tc1->rmt_ip.ip4,
1638                                          tc1->lcl_port, tc1->rmt_port,
1639                                          tc1->proto, 0, &is_filtered);
1640   TCP_TEST ((tconn == 0), "lookup result should be null");
1641   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1642                                          &tc2->rmt_ip.ip4,
1643                                          tc2->lcl_port, tc2->rmt_port,
1644                                          tc2->proto, 0, &is_filtered);
1645   TCP_TEST ((tconn == 0), "lookup result should be null");
1646
1647   /*
1648    * Re-add and lookup tc2
1649    */
1650   session_lookup_add_connection (tc1, tc1->s_index);
1651   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
1652                                          &tc2->rmt_ip.ip4,
1653                                          tc2->lcl_port, tc2->rmt_port,
1654                                          tc2->proto, 0, &is_filtered);
1655   TCP_TEST ((tconn == 0), "lookup result should be null");
1656
1657   return 0;
1658 }
1659
1660 static int
1661 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
1662 {
1663   int rv = 0;
1664   tcp_connection_t *tc0;
1665   ip4_address_t local, remote;
1666   u16 local_port, remote_port;
1667   tcp_main_t *tm = vnet_get_tcp_main ();
1668   int is_add = 1;
1669
1670
1671   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1672     {
1673       if (unformat (input, "del"))
1674         is_add = 0;
1675       else if (unformat (input, "add"))
1676         is_add = 1;
1677       else
1678         break;
1679     }
1680
1681   if (is_add)
1682     {
1683       local.as_u32 = clib_host_to_net_u32 (0x06000101);
1684       remote.as_u32 = clib_host_to_net_u32 (0x06000102);
1685       local_port = clib_host_to_net_u16 (1234);
1686       remote_port = clib_host_to_net_u16 (11234);
1687
1688       pool_get (tm->connections[0], tc0);
1689       memset (tc0, 0, sizeof (*tc0));
1690
1691       tc0->state = TCP_STATE_ESTABLISHED;
1692       tc0->rcv_las = 1;
1693       tc0->c_c_index = tc0 - tm->connections[0];
1694       tc0->c_lcl_port = local_port;
1695       tc0->c_rmt_port = remote_port;
1696       tc0->c_is_ip4 = 1;
1697       tc0->c_thread_index = 0;
1698       tc0->c_lcl_ip4.as_u32 = local.as_u32;
1699       tc0->c_rmt_ip4.as_u32 = remote.as_u32;
1700       tc0->rcv_opts.mss = 1450;
1701       tcp_connection_init_vars (tc0);
1702
1703       TCP_EVT_DBG (TCP_EVT_OPEN, tc0);
1704
1705       if (stream_session_accept (&tc0->connection, 0 /* listener index */ ,
1706                                  0 /* notify */ ))
1707         clib_warning ("stream_session_accept failed");
1708
1709       stream_session_accept_notify (&tc0->connection);
1710     }
1711   else
1712     {
1713       tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
1714       tc0->state = TCP_STATE_CLOSED;
1715       stream_session_disconnect_notify (&tc0->connection);
1716     }
1717
1718   return rv;
1719 }
1720
1721 static clib_error_t *
1722 tcp_test (vlib_main_t * vm,
1723           unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1724 {
1725   int res = 0;
1726
1727   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1728     {
1729       if (unformat (input, "sack"))
1730         {
1731           res = tcp_test_sack (vm, input);
1732         }
1733       else if (unformat (input, "fifo"))
1734         {
1735           res = tcp_test_fifo (vm, input);
1736         }
1737       else if (unformat (input, "session"))
1738         {
1739           res = tcp_test_session (vm, input);
1740         }
1741       else if (unformat (input, "lookup"))
1742         {
1743           res = tcp_test_lookup (vm, input);
1744         }
1745       else if (unformat (input, "all"))
1746         {
1747           if ((res = tcp_test_sack (vm, input)))
1748             goto done;
1749           if ((res = tcp_test_fifo (vm, input)))
1750             goto done;
1751           if ((res = tcp_test_lookup (vm, input)))
1752             goto done;
1753         }
1754       else
1755         break;
1756     }
1757
1758 done:
1759   if (res)
1760     return clib_error_return (0, "TCP unit test failed");
1761   return 0;
1762 }
1763
1764 /* *INDENT-OFF* */
1765 VLIB_CLI_COMMAND (tcp_test_command, static) =
1766 {
1767   .path = "test tcp",
1768   .short_help = "internal tcp unit tests",
1769   .function = tcp_test,
1770 };
1771 /* *INDENT-ON* */
1772
1773 /*
1774  * fd.io coding-style-patch-verification: ON
1775  *
1776  * Local Variables:
1777  * eval: (c-set-style "gnu")
1778  * End:
1779  */