tcp: move features to separate files
[vpp.git] / src / plugins / unittest / tcp_test.c
1 /*
2  * Copyright (c) 2017-2019 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 #include <vnet/tcp/tcp_inlines.h>
17
18 #define TCP_TEST_I(_cond, _comment, _args...)                   \
19 ({                                                              \
20   int _evald = (_cond);                                         \
21   if (!(_evald)) {                                              \
22     fformat(stderr, "FAIL:%d: " _comment "\n",                  \
23             __LINE__, ##_args);                                 \
24   } else {                                                      \
25     fformat(stderr, "PASS:%d: " _comment "\n",                  \
26             __LINE__, ##_args);                                 \
27   }                                                             \
28   _evald;                                                       \
29 })
30
31 #define TCP_TEST(_cond, _comment, _args...)                     \
32 {                                                               \
33     if (!TCP_TEST_I(_cond, _comment, ##_args)) {                \
34         return 1;                                               \
35     }                                                           \
36 }
37
38 /* *INDENT-OFF* */
39 scoreboard_trace_elt_t sb_trace[] = {};
40 /* *INDENT-ON* */
41
42 static int
43 tcp_test_scoreboard_replay (vlib_main_t * vm, unformat_input_t * input)
44 {
45   int verbose = 0;
46   tcp_connection_t _tc, *tc = &_tc;
47   u8 *s = 0;
48
49   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
50     {
51       if (unformat (input, "detail"))
52         verbose = 1;
53       else
54         {
55           clib_error_t *e = clib_error_return
56             (0, "unknown input `%U'", format_unformat_error, input);
57           clib_error_report (e);
58           return -1;
59         }
60     }
61
62 #if TCP_SCOREBOARD_TRACE
63   tc->sack_sb.trace = sb_trace;
64 #endif
65   s = tcp_scoreboard_replay (s, tc, verbose);
66   vlib_cli_output (vm, "%v", s);
67   return 0;
68 }
69
70 static int
71 tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
72 {
73   tcp_connection_t _tc, *tc = &_tc;
74   sack_scoreboard_t *sb = &tc->sack_sb;
75   sack_block_t *sacks = 0, block;
76   sack_scoreboard_hole_t *hole;
77   int i, verbose = 0;
78
79   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
80     {
81       if (unformat (input, "verbose"))
82         verbose = 1;
83       else if (unformat (input, "replay"))
84         return tcp_test_scoreboard_replay (vm, input);
85     }
86
87   clib_memset (tc, 0, sizeof (*tc));
88
89   tc->flags |= TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY;
90   tc->snd_una = 0;
91   tc->snd_una_max = 1000;
92   tc->snd_nxt = 1000;
93   tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
94   tc->snd_mss = 150;
95   scoreboard_init (&tc->sack_sb);
96
97   for (i = 0; i < 1000 / 100; i++)
98     {
99       block.start = i * 100;
100       block.end = (i + 1) * 100;
101       vec_add1 (sacks, block);
102     }
103
104   /*
105    * Inject even blocks
106    */
107
108   for (i = 0; i < 1000 / 200; i++)
109     {
110       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2]);
111     }
112   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
113   tcp_rcv_sacks (tc, 0);
114
115   if (verbose)
116     vlib_cli_output (vm, "sb after even blocks (mss %u):\n%U",
117                      tc->snd_mss, format_tcp_scoreboard, sb, tc);
118
119   TCP_TEST ((pool_elts (sb->holes) == 5),
120             "scoreboard has %d elements", pool_elts (sb->holes));
121
122   /* First SACK block should be rejected */
123   hole = scoreboard_first_hole (sb);
124   TCP_TEST ((hole->start == 0 && hole->end == 200),
125             "first hole start %u end %u", hole->start, hole->end);
126   hole = scoreboard_last_hole (sb);
127   TCP_TEST ((hole->start == 900 && hole->end == 1000),
128             "last hole start %u end %u", hole->start, hole->end);
129   TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
130   TCP_TEST ((!sb->is_reneging), "is not reneging");
131   TCP_TEST ((sb->last_sacked_bytes == 400),
132             "last sacked bytes %d", sb->last_sacked_bytes);
133   TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
134   TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
135
136   /*
137    * Inject odd blocks except the last
138    *
139    */
140
141   vec_reset_length (tc->rcv_opts.sacks);
142   for (i = 0; i < 800 / 200; i++)
143     {
144       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
145     }
146   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
147   tcp_rcv_sacks (tc, 0);
148
149   if (verbose)
150     vlib_cli_output (vm, "\nsb after odd blocks:\n%U", format_tcp_scoreboard,
151                      sb, tc);
152
153   hole = scoreboard_first_hole (sb);
154   TCP_TEST ((pool_elts (sb->holes) == 2),
155             "scoreboard has %d holes", pool_elts (sb->holes));
156   TCP_TEST ((hole->start == 0 && hole->end == 100),
157             "first hole start %u end %u", hole->start, hole->end);
158   TCP_TEST ((sb->sacked_bytes == 800), "sacked bytes %d", sb->sacked_bytes);
159   TCP_TEST ((!sb->is_reneging), "is not reneging");
160   TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
161   TCP_TEST ((sb->last_sacked_bytes == 400),
162             "last sacked bytes %d", sb->last_sacked_bytes);
163   TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
164
165   /*
166    *  Ack until byte 100 - this is reneging because we should ack until 900
167    */
168   tcp_rcv_sacks (tc, 100);
169   if (verbose)
170     vlib_cli_output (vm, "\nack until byte 100:\n%U", format_tcp_scoreboard,
171                      sb, tc);
172
173   TCP_TEST ((pool_elts (sb->holes) == 1), "scoreboard has %d elements",
174             pool_elts (sb->holes));
175   TCP_TEST ((sb->is_reneging), "is reneging");
176
177   /*
178    * Make sure we accept duplicate acks while reneging.
179    */
180   tc->snd_una = 100;
181   sb->high_rxt = 950;
182
183   block.start = 900;
184   block.end = 950;
185   vec_add1 (tc->rcv_opts.sacks, block);
186
187   tcp_rcv_sacks (tc, 100);
188   TCP_TEST ((pool_elts (sb->holes) == 1), "scoreboard has %d elements",
189             pool_elts (sb->holes));
190   TCP_TEST ((sb->is_reneging), "is reneging");
191   TCP_TEST ((sb->last_sacked_bytes == 50), "last sacked bytes %d",
192             sb->last_sacked_bytes);
193   TCP_TEST ((sb->rxt_sacked == 50), "last rxt sacked bytes %d",
194             sb->rxt_sacked);
195
196   /*
197    * Sack all up to 950
198    */
199   tcp_rcv_sacks (tc, 950);
200   TCP_TEST ((sb->high_sacked == 950), "max sacked byte %u", sb->high_sacked);
201   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
202   TCP_TEST ((sb->last_sacked_bytes == 0),
203             "last sacked bytes %d", sb->last_sacked_bytes);
204   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
205   TCP_TEST ((!sb->is_reneging), "is not reneging");
206
207   /*
208    * Sack [960 970] [980 990]
209    */
210   sb->high_rxt = 985;
211
212   tc->snd_una = 950;
213   vec_reset_length (tc->rcv_opts.sacks);
214   block.start = 960;
215   block.end = 970;
216   vec_add1 (tc->rcv_opts.sacks, block);
217
218   block.start = 980;
219   block.end = 990;
220   vec_add1 (tc->rcv_opts.sacks, block);
221
222   tcp_rcv_sacks (tc, 950);
223   TCP_TEST ((sb->high_sacked == 990), "max sacked byte %u", sb->high_sacked);
224   TCP_TEST ((sb->sacked_bytes == 20), "sacked bytes %d", sb->sacked_bytes);
225   TCP_TEST ((sb->last_sacked_bytes == 20),
226             "last sacked bytes %d", sb->last_sacked_bytes);
227   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
228   TCP_TEST ((!sb->is_reneging), "is not reneging");
229   TCP_TEST ((sb->rxt_sacked == 15), "last rxt sacked bytes %d",
230             sb->rxt_sacked);
231
232   /*
233    * Ack up to 960 (reneging) + [961 971]
234    */
235   tc->rcv_opts.sacks[0].start = 961;
236   tc->rcv_opts.sacks[0].end = 971;
237
238   tcp_rcv_sacks (tc, 960);
239
240   TCP_TEST ((sb->is_reneging), "is reneging");
241   TCP_TEST ((sb->sacked_bytes == 21), "sacked bytes %d", sb->sacked_bytes);
242   TCP_TEST ((sb->last_sacked_bytes == 1),
243             "last sacked bytes %d", sb->last_sacked_bytes);
244   TCP_TEST ((sb->rxt_sacked == 11), "last rxt sacked bytes %d",
245             sb->rxt_sacked);
246   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
247             sb->last_bytes_delivered);
248
249   /*
250    * Ack up to 960 (reneging) + [961 990]
251    */
252   tc->snd_una = 960;
253   tc->rcv_opts.sacks[0].start = 961;
254   tc->rcv_opts.sacks[0].end = 990;
255
256   tcp_rcv_sacks (tc, 960);
257
258   TCP_TEST ((sb->is_reneging), "is reneging");
259   TCP_TEST ((sb->sacked_bytes == 30), "sacked bytes %d", sb->sacked_bytes);
260   TCP_TEST ((sb->last_sacked_bytes == 9),
261             "last sacked bytes %d", sb->last_sacked_bytes);
262   TCP_TEST ((sb->rxt_sacked == 9), "last rxt sacked bytes %d",
263             sb->rxt_sacked);
264
265   /*
266    * Sack remaining bytes [990 1000]
267    */
268   tc->rcv_opts.sacks[0].start = 990;
269   tc->rcv_opts.sacks[0].end = 1000;
270
271   tcp_rcv_sacks (tc, 960);
272
273   TCP_TEST ((sb->is_reneging), "is reneging");
274   TCP_TEST ((sb->sacked_bytes == 40), "sacked bytes %d", sb->sacked_bytes);
275   TCP_TEST ((sb->last_sacked_bytes == 10),
276             "last sacked bytes %d", sb->last_sacked_bytes);
277   TCP_TEST ((sb->rxt_sacked == 0), "last rxt sacked bytes %d",
278             sb->rxt_sacked);
279   TCP_TEST (pool_elts (sb->holes) == 0, "no holes left");
280
281   /*
282    * Ack up to 970 no sack blocks
283    */
284   vec_reset_length (tc->rcv_opts.sacks);
285   tc->rcv_opts.flags &= ~TCP_OPTS_FLAG_SACK;
286   tcp_rcv_sacks (tc, 970);
287
288   TCP_TEST ((sb->is_reneging), "is reneging");
289   TCP_TEST ((sb->sacked_bytes == 30), "sacked bytes %d", sb->sacked_bytes);
290   TCP_TEST ((sb->last_sacked_bytes == 0),
291             "last sacked bytes %d", sb->last_sacked_bytes);
292   TCP_TEST ((sb->rxt_sacked == 0), "last rxt sacked bytes %d",
293             sb->rxt_sacked);
294
295   /*
296    * Ack all up to 1000
297    */
298   tc->snd_una = 970;
299   tcp_rcv_sacks (tc, 1000);
300   TCP_TEST ((sb->high_sacked == 1000), "max sacked byte %u", sb->high_sacked);
301   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
302   TCP_TEST (sb->last_bytes_delivered == 30, "last bytes delivered %d",
303             sb->last_bytes_delivered);
304   TCP_TEST ((sb->last_sacked_bytes == 0),
305             "last sacked bytes %d", sb->last_sacked_bytes);
306   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
307   TCP_TEST ((!sb->is_reneging), "is not reneging");
308
309   /*
310    * Add new block
311    */
312   tc->flags = 0;
313   tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
314   vec_reset_length (tc->rcv_opts.sacks);
315
316   block.start = 1200;
317   block.end = 1300;
318   vec_add1 (tc->rcv_opts.sacks, block);
319
320   tc->snd_una_max = 1500;
321   tc->snd_una = 1000;
322   tc->snd_nxt = 1500;
323   tcp_rcv_sacks (tc, 1000);
324
325   if (verbose)
326     vlib_cli_output (vm, "\nadd [1200, 1300] snd_una_max 1500, snd_una 1000:"
327                      " \n%U", format_tcp_scoreboard, sb, tc);
328
329   TCP_TEST ((!sb->is_reneging), "is not reneging");
330   TCP_TEST ((pool_elts (sb->holes) == 2),
331             "scoreboard has %d holes", pool_elts (sb->holes));
332   hole = scoreboard_first_hole (sb);
333   TCP_TEST ((hole->start == 1000 && hole->end == 1200),
334             "first hole start %u end %u", hole->start, hole->end);
335   TCP_TEST ((sb->high_sacked == 1300), "max sacked byte %u", sb->high_sacked);
336   hole = scoreboard_last_hole (sb);
337   TCP_TEST ((hole->start == 1300 && hole->end == 1500),
338             "last hole start %u end %u", hole->start, hole->end);
339   TCP_TEST ((sb->sacked_bytes == 100), "sacked bytes %d", sb->sacked_bytes);
340   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
341
342   /*
343    * Ack first hole
344    */
345
346   vec_reset_length (tc->rcv_opts.sacks);
347   /* Ack up to 1300 to avoid reneging */
348   tcp_rcv_sacks (tc, 1300);
349
350   if (verbose)
351     vlib_cli_output (vm, "\nsb ack up to byte 1300:\n%U",
352                      format_tcp_scoreboard, sb, tc);
353
354   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
355   TCP_TEST ((pool_elts (sb->holes) == 1),
356             "scoreboard has %d elements", pool_elts (sb->holes));
357   TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
358             sb->last_bytes_delivered);
359   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
360   TCP_TEST ((sb->head != TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
361   TCP_TEST ((sb->tail != TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
362   TCP_TEST ((!sb->is_reneging), "is not reneging");
363
364   /*
365    * Add some more blocks and then remove all
366    */
367   vec_reset_length (tc->rcv_opts.sacks);
368   tc->snd_una = 1300;
369   tc->snd_nxt = tc->snd_una_max = 1900;
370   for (i = 0; i < 5; i++)
371     {
372       block.start = i * 100 + 1200;
373       block.end = (i + 1) * 100 + 1200;
374       vec_add1 (tc->rcv_opts.sacks, block);
375     }
376   tcp_rcv_sacks (tc, 1900);
377
378   scoreboard_clear (sb);
379   if (verbose)
380     vlib_cli_output (vm, "\nsb cleared all:\n%U", format_tcp_scoreboard, sb,
381                      tc);
382
383   TCP_TEST ((pool_elts (sb->holes) == 0),
384             "number of holes %d", pool_elts (sb->holes));
385   TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
386   TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
387
388   /*
389    * Re-inject odd blocks and ack them all
390    */
391
392   tc->snd_una = 0;
393   tc->snd_una_max = 1000;
394   tc->snd_nxt = 1000;
395   vec_reset_length (tc->rcv_opts.sacks);
396   for (i = 0; i < 5; i++)
397     {
398       vec_add1 (tc->rcv_opts.sacks, sacks[i * 2 + 1]);
399     }
400   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
401   tcp_rcv_sacks (tc, 0);
402   if (verbose)
403     vlib_cli_output (vm, "\nsb added odd blocks snd_una 0 snd_una_max 1000:"
404                      "\n%U", format_tcp_scoreboard, sb, tc);
405   TCP_TEST ((pool_elts (sb->holes) == 5),
406             "scoreboard has %d elements", pool_elts (sb->holes));
407   TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
408   hole = scoreboard_last_hole (sb);
409   TCP_TEST ((hole->end == 900), "last hole end %u", hole->end);
410   TCP_TEST ((sb->high_sacked == 1000), "high sacked %u", sb->high_sacked);
411
412   /*
413    * Renege bytes from 950 to 1000
414    */
415   tcp_rcv_sacks (tc, 950);
416
417   if (verbose)
418     vlib_cli_output (vm, "\nack [0, 950]:\n%U", format_tcp_scoreboard, sb,
419                      tc);
420
421   TCP_TEST ((pool_elts (sb->holes) == 0), "scoreboard has %d elements",
422             pool_elts (sb->holes));
423   TCP_TEST ((sb->is_reneging), "is reneging");
424   TCP_TEST ((sb->sacked_bytes == 50), "sacked bytes %d", sb->sacked_bytes);
425   TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
426             sb->last_sacked_bytes);
427   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
428   TCP_TEST ((sb->high_sacked == 1000), "high sacked %u", sb->high_sacked);
429
430   scoreboard_clear (sb);
431
432   /*
433    * Inject one block, ack it and overlap hole
434    */
435
436   tc->snd_una = 0;
437   tc->snd_una_max = 1000;
438   tc->snd_nxt = 1000;
439
440   block.start = 100;
441   block.end = 500;
442   vec_add1 (tc->rcv_opts.sacks, block);
443   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
444
445   tcp_rcv_sacks (tc, 0);
446
447   if (verbose)
448     vlib_cli_output (vm, "\nsb added [100, 500] snd_una 0 snd_una_max 1000:"
449                      "\n%U", format_tcp_scoreboard, sb, tc);
450
451   tcp_rcv_sacks (tc, 800);
452
453   if (verbose)
454     vlib_cli_output (vm, "\nsb ack [0, 800]:\n%U", format_tcp_scoreboard, sb,
455                      tc);
456
457   TCP_TEST ((pool_elts (sb->holes) == 1),
458             "scoreboard has %d elements", pool_elts (sb->holes));
459   TCP_TEST ((!sb->is_reneging), "is not reneging");
460   TCP_TEST ((sb->sacked_bytes == 0), "sacked bytes %d", sb->sacked_bytes);
461   TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
462             sb->last_sacked_bytes);
463   TCP_TEST ((sb->last_bytes_delivered == 400),
464             "last bytes delivered %d", sb->last_bytes_delivered);
465   TCP_TEST ((sb->lost_bytes == 0), "lost bytes %u", sb->lost_bytes);
466   TCP_TEST ((sb->head != TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
467   TCP_TEST ((sb->tail != TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
468
469   /*
470    * One hole close to head, patch head, split in two and start acking
471    * the lowest part
472    */
473   scoreboard_clear (sb);
474   tc->snd_una = 0;
475   tc->snd_una_max = 1000;
476   tc->snd_nxt = 1000;
477
478   block.start = 500;
479   block.end = 1000;
480   vec_add1 (tc->rcv_opts.sacks, block);
481   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
482
483   tcp_rcv_sacks (tc, 0);
484   if (verbose)
485     vlib_cli_output (vm, "\nsb added [500, 1000]:\n%U",
486                      format_tcp_scoreboard, sb, tc);
487   TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
488   TCP_TEST ((sb->last_sacked_bytes == 500), "last sacked bytes %d",
489             sb->last_sacked_bytes);
490   TCP_TEST ((sb->lost_bytes == 500), "lost bytes %u", sb->lost_bytes);
491
492   vec_reset_length (tc->rcv_opts.sacks);
493   block.start = 300;
494   block.end = 400;
495   vec_add1 (tc->rcv_opts.sacks, block);
496   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
497   tcp_rcv_sacks (tc, 100);
498   if (verbose)
499     vlib_cli_output (vm, "\nsb added [0, 100] [300, 400]:\n%U",
500                      format_tcp_scoreboard, sb, tc);
501   TCP_TEST ((pool_elts (sb->holes) == 2),
502             "scoreboard has %d elements", pool_elts (sb->holes));
503   TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
504   TCP_TEST ((sb->last_sacked_bytes == 100), "last sacked bytes %d",
505             sb->last_sacked_bytes);
506   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
507             sb->last_bytes_delivered);
508   TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
509
510   /*
511    * Ack [100 300] in two steps
512    *
513    * Step 1. Ack [100 200] which delivers 100 of the bytes lost
514    */
515   tc->snd_una = 100;
516   tcp_rcv_sacks (tc, 200);
517   TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
518   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
519             sb->last_bytes_delivered);
520   TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
521
522   /*
523    * Step 2. Ack up to 300, although 300 400 is sacked, so this is interpreted
524    * as reneging.
525    */
526   tc->snd_una = 200;
527   tcp_rcv_sacks (tc, 300);
528   if (verbose)
529     vlib_cli_output (vm, "\nacked [100, 300] in two steps:\n%U",
530                      format_tcp_scoreboard, sb, tc);
531   TCP_TEST ((sb->sacked_bytes == 600), "sacked bytes %d", sb->sacked_bytes);
532   TCP_TEST ((sb->lost_bytes == 100), "lost bytes %u", sb->lost_bytes);
533   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
534             sb->last_bytes_delivered);
535   TCP_TEST ((sb->is_reneging), "is reneging");
536
537   /*
538    * Ack [300 500]. Delivers reneged segment [300 400] and reneges bytes
539    * above 500
540    */
541   tc->snd_una = 300;
542   tcp_rcv_sacks (tc, 500);
543   if (verbose)
544     vlib_cli_output (vm, "\nacked [400, 500]:\n%U", format_tcp_scoreboard, sb,
545                      tc);
546   TCP_TEST ((pool_elts (sb->holes) == 0),
547             "scoreboard has %d elements", pool_elts (sb->holes));
548   TCP_TEST ((sb->sacked_bytes == 500), "sacked bytes %d", sb->sacked_bytes);
549   TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
550             sb->last_sacked_bytes);
551   TCP_TEST ((sb->last_bytes_delivered == 100), "last bytes delivered %d",
552             sb->last_bytes_delivered);
553   TCP_TEST ((sb->is_reneging), "is reneging");
554   TCP_TEST ((sb->head == TCP_INVALID_SACK_HOLE_INDEX), "head %u", sb->head);
555   TCP_TEST ((sb->tail == TCP_INVALID_SACK_HOLE_INDEX), "tail %u", sb->tail);
556
557   /*
558    * Ack up to 1000 to deliver all bytes
559    */
560   tc->snd_una = 500;
561   tcp_rcv_sacks (tc, 1000);
562   if (verbose)
563     vlib_cli_output (vm, "\nAck high sacked:\n%U", format_tcp_scoreboard, sb,
564                      tc);
565   TCP_TEST ((sb->last_sacked_bytes == 0), "last sacked bytes %d",
566             sb->last_sacked_bytes);
567   TCP_TEST ((sb->last_bytes_delivered == 500), "last bytes delivered %d",
568             sb->last_bytes_delivered);
569   TCP_TEST ((!sb->is_reneging), "is not reneging");
570
571   /*
572    * Add [1200, 1500] and test that [1000, 1200] is lost (bytes condition)
573    * snd_una = 1000 and snd_una_max = 1600
574    */
575   tc->snd_una = 1000;
576   tc->snd_nxt = tc->snd_una_max = 1600;
577   vec_reset_length (tc->rcv_opts.sacks);
578   block.start = 1200;
579   block.end = 1500;
580   vec_add1 (tc->rcv_opts.sacks, block);
581   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
582   tcp_rcv_sacks (tc, 1000);
583   if (verbose)
584     vlib_cli_output (vm, "\nacked [1200, 1500] test first hole is lost:\n%U",
585                      format_tcp_scoreboard, sb, tc);
586   TCP_TEST ((pool_elts (sb->holes) == 2), "scoreboard has %d elements",
587             pool_elts (sb->holes));
588   TCP_TEST ((sb->sacked_bytes == 300), "sacked bytes %d", sb->sacked_bytes);
589   TCP_TEST ((sb->last_sacked_bytes == 300), "last sacked bytes %d",
590             sb->last_sacked_bytes);
591   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
592             sb->last_bytes_delivered);
593   TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
594   TCP_TEST ((!sb->is_reneging), "is not reneging");
595
596   /*
597    * Restart
598    */
599   scoreboard_clear (sb);
600   vec_reset_length (tc->rcv_opts.sacks);
601
602   /*
603    * Inject [100 500]
604    */
605
606   tc->flags |= TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY;
607   tc->snd_una = 0;
608   tc->snd_una_max = 1000;
609   tc->snd_nxt = 1000;
610   sb->high_rxt = 0;
611
612   block.start = 100;
613   block.end = 500;
614   vec_add1 (tc->rcv_opts.sacks, block);
615   tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
616
617   tcp_rcv_sacks (tc, 0);
618
619   TCP_TEST ((sb->sacked_bytes == 400), "sacked bytes %d", sb->sacked_bytes);
620   TCP_TEST ((sb->last_sacked_bytes == 400), "last sacked bytes %d",
621             sb->last_sacked_bytes);
622   TCP_TEST ((!sb->is_reneging), "is not reneging");
623
624   /*
625    * Renege, sack all of the remaining bytes and cover some rxt bytes
626    */
627   sb->high_rxt = 700;
628   tc->rcv_opts.sacks[0].start = 500;
629   tc->rcv_opts.sacks[0].end = 1000;
630
631   tcp_rcv_sacks (tc, 100);
632
633   TCP_TEST ((sb->sacked_bytes == 900), "sacked bytes %d", sb->sacked_bytes);
634   TCP_TEST ((sb->last_sacked_bytes == 500), "last sacked bytes %d",
635             sb->last_sacked_bytes);
636   TCP_TEST (sb->is_reneging, "is reneging");
637   TCP_TEST ((sb->rxt_sacked == 300), "last rxt sacked bytes %d",
638             sb->rxt_sacked);
639
640   return 0;
641 }
642
643 static int
644 tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
645 {
646   tcp_connection_t _tc, *tc = &_tc;
647   sack_block_t *sacks;
648   int i, verbose = 0, expected;
649
650   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
651     {
652       if (unformat (input, "verbose"))
653         verbose = 1;
654       else
655         {
656           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
657                            input);
658           return -1;
659         }
660     }
661
662   clib_memset (tc, 0, sizeof (*tc));
663
664   /*
665    * Add odd sack block pairs
666    */
667   for (i = 1; i < 10; i += 2)
668     {
669       tcp_update_sack_list (tc, i * 100, (i + 1) * 100);
670     }
671
672   TCP_TEST ((vec_len (tc->snd_sacks) == 5), "sack blocks %d expected %d",
673             vec_len (tc->snd_sacks), 5);
674   TCP_TEST ((tc->snd_sacks[0].start = 900),
675             "first sack block start %u expected %u", tc->snd_sacks[0].start,
676             900);
677
678   /*
679    * Try to add one extra
680    */
681   sacks = vec_dup (tc->snd_sacks);
682
683   tcp_update_sack_list (tc, 1100, 1200);
684   if (verbose)
685     vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
686                      format_tcp_sacks, tc);
687   expected = 5 < TCP_MAX_SACK_BLOCKS ? 6 : 5;
688   TCP_TEST ((vec_len (tc->snd_sacks) == expected),
689             "sack blocks %d expected %d", vec_len (tc->snd_sacks), expected);
690   TCP_TEST ((tc->snd_sacks[0].start == 1100),
691             "first sack block start %u expected %u", tc->snd_sacks[0].start,
692             1100);
693
694   /* restore */
695   vec_free (tc->snd_sacks);
696   tc->snd_sacks = sacks;
697
698   /*
699    * Overlap first 2 segment
700    */
701   tc->rcv_nxt = 300;
702   tcp_update_sack_list (tc, 300, 300);
703   if (verbose)
704     vlib_cli_output (vm, "overlap first 2 segments:\n%U",
705                      format_tcp_sacks, tc);
706   TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
707             vec_len (tc->snd_sacks), 3);
708   TCP_TEST ((tc->snd_sacks[0].start == 900),
709             "first sack block start %u expected %u", tc->snd_sacks[0].start,
710             500);
711
712   /*
713    * Add a new segment
714    */
715   tcp_update_sack_list (tc, 1100, 1200);
716   if (verbose)
717     vlib_cli_output (vm, "add new segment [1100, 1200]\n%U",
718                      format_tcp_sacks, tc);
719   TCP_TEST ((vec_len (tc->snd_sacks) == 4), "sack blocks %d expected %d",
720             vec_len (tc->snd_sacks), 4);
721   TCP_TEST ((tc->snd_sacks[0].start == 1100),
722             "first sack block start %u expected %u", tc->snd_sacks[0].start,
723             1100);
724
725   /*
726    * Join middle segments
727    */
728   tcp_update_sack_list (tc, 800, 900);
729   if (verbose)
730     vlib_cli_output (vm, "join middle segments [800, 900]\n%U",
731                      format_tcp_sacks, tc);
732
733   TCP_TEST ((vec_len (tc->snd_sacks) == 3), "sack blocks %d expected %d",
734             vec_len (tc->snd_sacks), 3);
735   TCP_TEST ((tc->snd_sacks[0].start == 700),
736             "first sack block start %u expected %u", tc->snd_sacks[0].start,
737             1100);
738
739   /*
740    * Advance rcv_nxt to overlap all
741    */
742   tc->rcv_nxt = 1200;
743   tcp_update_sack_list (tc, 1200, 1200);
744   if (verbose)
745     vlib_cli_output (vm, "advance rcv_nxt to 1200\n%U", format_tcp_sacks, tc);
746   TCP_TEST ((vec_len (tc->snd_sacks) == 0), "sack blocks %d expected %d",
747             vec_len (tc->snd_sacks), 0);
748
749
750   /*
751    * Add 2 blocks, overwrite first and update rcv_nxt to also remove it
752    */
753
754   vec_reset_length (tc->snd_sacks);
755   tc->rcv_nxt = 0;
756
757   tcp_update_sack_list (tc, 100, 200);
758   tcp_update_sack_list (tc, 300, 400);
759
760   if (verbose)
761     vlib_cli_output (vm, "add [100, 200] [300, 400]\n%U",
762                      format_tcp_sacks, tc);
763   TCP_TEST ((vec_len (tc->snd_sacks) == 2),
764             "sack blocks %d expected %d", vec_len (tc->snd_sacks), 2);
765   TCP_TEST ((tc->snd_sacks[0].start == 300),
766             "first sack block start %u expected %u", tc->snd_sacks[0].start,
767             300);
768
769   tc->rcv_nxt = 100;
770   tcp_update_sack_list (tc, 100, 100);
771   if (verbose)
772     vlib_cli_output (vm, "add [100, 200] rcv_nxt = 100\n%U",
773                      format_tcp_sacks, tc);
774   TCP_TEST ((vec_len (tc->snd_sacks) == 1),
775             "sack blocks %d expected %d", vec_len (tc->snd_sacks), 1);
776   TCP_TEST ((tc->snd_sacks[0].start == 300),
777             "first sack block start %u expected %u", tc->snd_sacks[0].start,
778             300);
779   return 0;
780 }
781
782 static int
783 tcp_test_sack (vlib_main_t * vm, unformat_input_t * input)
784 {
785   int res = 0;
786
787   /* Run all tests */
788   if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
789     {
790       if (tcp_test_sack_tx (vm, input))
791         {
792           return -1;
793         }
794
795       if (tcp_test_sack_rx (vm, input))
796         {
797           return -1;
798         }
799     }
800   else
801     {
802       if (unformat (input, "tx"))
803         {
804           res = tcp_test_sack_tx (vm, input);
805         }
806       else if (unformat (input, "rx"))
807         {
808           res = tcp_test_sack_rx (vm, input);
809         }
810     }
811
812   return res;
813 }
814
815 static int
816 tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
817 {
818   session_main_t *smm = &session_main;
819   transport_connection_t _tc1, *tc1 = &_tc1, _tc2, *tc2 = &_tc2, *tconn;
820   tcp_connection_t *tc;
821   session_t *s, *s1;
822   u8 cmp = 0, is_filtered = 0;
823   u32 sidx;
824
825   /*
826    * Allocate fake session and connection 1
827    */
828   pool_get (smm->wrk[0].sessions, s);
829   clib_memset (s, 0, sizeof (*s));
830   s->session_index = sidx = s - smm->wrk[0].sessions;
831
832   tc = tcp_connection_alloc (0);
833   tc->connection.s_index = s->session_index;
834   s->connection_index = tc->connection.c_index;
835
836   tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
837   tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000103);
838   tc->connection.lcl_port = 35051;
839   tc->connection.rmt_port = 53764;
840   tc->connection.proto = TRANSPORT_PROTO_TCP;
841   tc->connection.is_ip4 = 1;
842   clib_memcpy_fast (tc1, &tc->connection, sizeof (*tc1));
843
844   /*
845    * Allocate fake session and connection 2
846    */
847   pool_get (smm->wrk[0].sessions, s);
848   clib_memset (s, 0, sizeof (*s));
849   s->session_index = s - smm->wrk[0].sessions;
850
851   tc = tcp_connection_alloc (0);
852   tc->connection.s_index = s->session_index;
853   s->connection_index = tc->connection.c_index;
854
855   tc->connection.lcl_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000101);
856   tc->connection.rmt_ip.ip4.as_u32 = clib_host_to_net_u32 (0x06000102);
857   tc->connection.lcl_port = 38225;
858   tc->connection.rmt_port = 53764;
859   tc->connection.proto = TRANSPORT_PROTO_TCP;
860   tc->connection.is_ip4 = 1;
861   clib_memcpy_fast (tc2, &tc->connection, sizeof (*tc2));
862
863   /*
864    * Confirm that connection lookup works
865    */
866
867   s1 = pool_elt_at_index (smm->wrk[0].sessions, sidx);
868   session_lookup_add_connection (tc1, session_handle (s1));
869   tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
870                                          &tc1->rmt_ip.ip4,
871                                          tc1->lcl_port, tc1->rmt_port,
872                                          tc1->proto, 0, &is_filtered);
873
874   TCP_TEST ((tconn != 0), "connection exists");
875   cmp = (memcmp (&tconn->rmt_ip, &tc1->rmt_ip, sizeof (tc1->rmt_ip)) == 0);
876   TCP_TEST ((cmp), "rmt ip is identical %d", cmp);
877   TCP_TEST ((tconn->lcl_port == tc1->lcl_port),
878             "rmt port is identical %d", tconn->lcl_port == tc1->lcl_port);
879
880   /*
881    * Non-existing connection lookup should not work
882    */
883
884   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
885                                          &tc2->rmt_ip.ip4,
886                                          tc2->lcl_port, tc2->rmt_port,
887                                          tc2->proto, 0, &is_filtered);
888   TCP_TEST ((tconn == 0), "lookup result should be null");
889
890   /*
891    * Delete and lookup again
892    */
893   session_lookup_del_connection (tc1);
894   tconn = session_lookup_connection_wt4 (0, &tc1->lcl_ip.ip4,
895                                          &tc1->rmt_ip.ip4,
896                                          tc1->lcl_port, tc1->rmt_port,
897                                          tc1->proto, 0, &is_filtered);
898   TCP_TEST ((tconn == 0), "lookup result should be null");
899   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
900                                          &tc2->rmt_ip.ip4,
901                                          tc2->lcl_port, tc2->rmt_port,
902                                          tc2->proto, 0, &is_filtered);
903   TCP_TEST ((tconn == 0), "lookup result should be null");
904
905   /*
906    * Re-add and lookup tc2
907    */
908   session_lookup_add_connection (tc1, tc1->s_index);
909   tconn = session_lookup_connection_wt4 (0, &tc2->lcl_ip.ip4,
910                                          &tc2->rmt_ip.ip4,
911                                          tc2->lcl_port, tc2->rmt_port,
912                                          tc2->proto, 0, &is_filtered);
913   TCP_TEST ((tconn == 0), "lookup result should be null");
914
915   return 0;
916 }
917
918 static int
919 tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
920 {
921   int rv = 0;
922   tcp_connection_t *tc0;
923   ip4_address_t local, remote;
924   u16 local_port, remote_port;
925   int is_add = 1;
926
927
928   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
929     {
930       if (unformat (input, "del"))
931         is_add = 0;
932       else if (unformat (input, "add"))
933         is_add = 1;
934       else
935         break;
936     }
937
938   if (is_add)
939     {
940       local.as_u32 = clib_host_to_net_u32 (0x06000101);
941       remote.as_u32 = clib_host_to_net_u32 (0x06000102);
942       local_port = clib_host_to_net_u16 (1234);
943       remote_port = clib_host_to_net_u16 (11234);
944
945       tc0 = tcp_connection_alloc (0);
946
947       tc0->state = TCP_STATE_ESTABLISHED;
948       tc0->rcv_las = 1;
949       tc0->c_lcl_port = local_port;
950       tc0->c_rmt_port = remote_port;
951       tc0->c_is_ip4 = 1;
952       tc0->c_thread_index = 0;
953       tc0->c_lcl_ip4.as_u32 = local.as_u32;
954       tc0->c_rmt_ip4.as_u32 = remote.as_u32;
955       tc0->rcv_opts.mss = 1450;
956       tcp_connection_init_vars (tc0);
957
958       TCP_EVT (TCP_EVT_OPEN, tc0);
959
960       if (session_stream_accept (&tc0->connection, 0 /* listener index */ ,
961                                  0 /* thread index */ , 0 /* notify */ ))
962         clib_warning ("stream_session_accept failed");
963
964       session_stream_accept_notify (&tc0->connection);
965     }
966   else
967     {
968       tc0 = tcp_connection_get (0 /* connection index */ , 0 /* thread */ );
969       tc0->state = TCP_STATE_CLOSED;
970       session_transport_closing_notify (&tc0->connection);
971     }
972
973   return rv;
974 }
975
976 static inline int
977 tbt_seq_lt (u32 a, u32 b)
978 {
979   return seq_lt (a, b);
980 }
981
982 static int
983 tcp_test_delivery (vlib_main_t * vm, unformat_input_t * input)
984 {
985   u32 thread_index = 0, snd_una, *min_seqs = 0;
986   tcp_rate_sample_t _rs = { 0 }, *rs = &_rs;
987   tcp_connection_t _tc, *tc = &_tc;
988   sack_scoreboard_t *sb = &tc->sack_sb;
989   int __clib_unused verbose = 0, i;
990   u64 rate = 1000, burst = 100;
991   sack_block_t *sacks = 0;
992   tcp_byte_tracker_t *bt;
993   rb_node_t *root, *rbn;
994   tcp_bt_sample_t *bts;
995
996   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
997     {
998       if (unformat (input, "verbose"))
999         verbose = 1;
1000       else
1001         {
1002           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1003                            input);
1004           return -1;
1005         }
1006     }
1007
1008   /* Init data structures */
1009   memset (tc, 0, sizeof (*tc));
1010   session_main.wrk[thread_index].last_vlib_time = 1;
1011   transport_connection_tx_pacer_update (&tc->connection, rate, 1e6);
1012
1013   tcp_bt_init (tc);
1014   bt = tc->bt;
1015
1016   /*
1017    * Track simple bursts without rxt
1018    */
1019
1020   /* 1) track first burst a time 1 */
1021   tcp_bt_track_tx (tc, burst);
1022
1023   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1024   TCP_TEST (pool_elts (bt->samples) == 1, "should have 1 sample");
1025   bts = pool_elt_at_index (bt->samples, bt->head);
1026   TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1027   TCP_TEST (bts->next == TCP_BTS_INVALID_INDEX, "next should be invalid");
1028   TCP_TEST (bts->prev == TCP_BTS_INVALID_INDEX, "prev should be invalid");
1029   TCP_TEST (bts->delivered_time == 1, "delivered time should be 1");
1030   TCP_TEST (bts->delivered == 0, "delivered should be 0");
1031   TCP_TEST (!(bts->flags & TCP_BTS_IS_RXT), "not retransmitted");
1032   TCP_TEST (!(bts->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1033
1034   /* 2) check delivery rate at time 2 */
1035   session_main.wrk[thread_index].last_vlib_time = 2;
1036   tc->snd_una = tc->snd_nxt = burst;
1037   tc->bytes_acked = burst;
1038
1039   tcp_bt_sample_delivery_rate (tc, rs);
1040
1041   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1042   TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1043   TCP_TEST (tc->delivered_time == 2, "delivered time should be 2");
1044   TCP_TEST (tc->delivered == burst, "delivered should be 100");
1045   TCP_TEST (rs->interval_time == 1, "ack time should be 1");
1046   TCP_TEST (rs->delivered == burst, "delivered should be 100");
1047   TCP_TEST (rs->prior_delivered == 0, "sample delivered should be 0");
1048   TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1049   TCP_TEST (tc->first_tx_time == 1, "first_tx_time %u", tc->first_tx_time);
1050
1051   /* 3) track second burst at time 2 */
1052   tcp_bt_track_tx (tc, burst);
1053   tc->snd_nxt += burst;
1054
1055   /* 4) track second burst at time 3 */
1056   session_main.wrk[thread_index].last_vlib_time = 3;
1057   tcp_bt_track_tx (tc, burst);
1058   tc->snd_nxt += burst;
1059
1060   TCP_TEST (pool_elts (bt->samples) == 2, "should have 2 samples");
1061
1062   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1063   bts = pool_elt_at_index (bt->samples, bt->head);
1064   TCP_TEST (bts->min_seq == tc->snd_una, "min seq should be snd_una");
1065   TCP_TEST (bts->next == bt->tail, "next should tail");
1066
1067   bts = pool_elt_at_index (bt->samples, bt->tail);
1068   TCP_TEST (bts->min_seq == tc->snd_nxt - burst,
1069             "min seq should be snd_nxt prior to burst");
1070   TCP_TEST (bts->prev == bt->head, "prev should be head");
1071
1072   /* 5) check delivery rate at time 4 */
1073   session_main.wrk[thread_index].last_vlib_time = 4;
1074   tc->snd_una = tc->snd_nxt;
1075   tc->bytes_acked = 2 * burst;
1076
1077   tcp_bt_sample_delivery_rate (tc, rs);
1078
1079   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1080   TCP_TEST (pool_elts (bt->samples) == 0, "sample should've been consumed");
1081   TCP_TEST (tc->delivered_time == 4, "delivered time should be 4");
1082   TCP_TEST (tc->delivered == 3 * burst, "delivered should be 300 is %u",
1083             tc->delivered);
1084   TCP_TEST (rs->interval_time == 2, "ack time should be 2");
1085   TCP_TEST (rs->delivered == 2 * burst, "delivered should be 200");
1086   TCP_TEST (rs->prior_delivered == burst, "delivered should be 100");
1087   TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1088   TCP_TEST (tc->first_tx_time == 2, "first_tx_time %u", tc->first_tx_time);
1089
1090   /*
1091    * Track retransmissions
1092    *
1093    * snd_una should be 300 at this point
1094    */
1095
1096   snd_una = tc->snd_una;
1097
1098   /* 1) track first burst at time 4 */
1099   tcp_bt_track_tx (tc, burst);
1100   tc->snd_nxt += burst;
1101
1102   /* 2) track second burst at time 5 */
1103   session_main.wrk[thread_index].last_vlib_time = 5;
1104   tcp_bt_track_tx (tc, burst);
1105   tc->snd_nxt += burst;
1106
1107   /* 3) track third burst at time 6 */
1108   session_main.wrk[thread_index].last_vlib_time = 6;
1109   tcp_bt_track_tx (tc, burst);
1110   tc->snd_nxt += burst;
1111
1112   /* 4) track fourth burst at time 7 */
1113   session_main.wrk[thread_index].last_vlib_time = 7;
1114   /* Limited until last burst is acked */
1115   tc->app_limited = snd_una + 4 * burst - 1;
1116   tcp_bt_track_tx (tc, burst);
1117   tc->snd_nxt += burst;
1118
1119   /* 5) check delivery rate at time 8
1120    *
1121    * tc->snd_una = snd_una + 10
1122    * sacks:
1123    * [snd_una + burst, snd_una + burst + 10]
1124    * [snd_una + 2 * burst + 10, snd_una + 2 * burst + 20]
1125    */
1126   session_main.wrk[thread_index].last_vlib_time = 8;
1127   tc->snd_una += 10;
1128   tc->bytes_acked = 10;
1129   sb->last_sacked_bytes = 20;
1130
1131   TCP_TEST (pool_elts (bt->samples) == 4, "there should be 4 samples");
1132
1133   vec_validate (sacks, 1);
1134   sacks[0].start = snd_una + burst;
1135   sacks[0].end = snd_una + burst + 10;
1136   sacks[1].start = snd_una + 2 * burst + 10;
1137   sacks[1].end = snd_una + 2 * burst + 20;
1138   tc->rcv_opts.sacks = sacks;
1139
1140   tcp_bt_sample_delivery_rate (tc, rs);
1141
1142   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1143   TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1144             pool_elts (bt->samples));
1145   TCP_TEST (tc->delivered_time == 8, "delivered time should be 8");
1146   TCP_TEST (tc->delivered == 3 * burst + 30, "delivered should be %u is %u",
1147             3 * burst + 30, tc->delivered);
1148   /* All 3 samples have the same delivered number of bytes. So the first is
1149    * the reference for delivery estimate. */
1150   TCP_TEST (rs->interval_time == 4, "ack time should be 4 is %.2f",
1151             rs->interval_time);
1152   TCP_TEST (rs->delivered == 30, "delivered should be 30");
1153   TCP_TEST (rs->prior_delivered == 3 * burst,
1154             "sample delivered should be %u", 3 * burst);
1155   TCP_TEST (!(rs->flags & TCP_BTS_IS_RXT), "not retransmitted");
1156   TCP_TEST (!(rs->flags & TCP_BTS_IS_APP_LIMITED), "not app limited");
1157   /* All 3 samples have the same delivered number of bytes. The first
1158    * sets the first tx time */
1159   TCP_TEST (tc->first_tx_time == 4, "first_tx_time %u", tc->first_tx_time);
1160
1161   /* 6) Retransmit and track at time 9
1162    *
1163    * delivered = 3 * burst + 30
1164    * delivered_time = 8 (last ack)
1165    *
1166    * segments:
1167    * [snd_una + 10, snd_una + burst]
1168    * [snd_una + burst + 10, snd_una + 2 * burst + 10]
1169    * [snd_una + 2 * burst + 20, snd_una + 4 * burst]
1170    */
1171   session_main.wrk[thread_index].last_vlib_time = 9;
1172
1173   tcp_bt_track_rxt (tc, snd_una + 10, snd_una + burst);
1174   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1175   /* The retransmit covers everything left from first burst */
1176   TCP_TEST (pool_elts (bt->samples) == 7, "there should be 7 samples %u",
1177             pool_elts (bt->samples));
1178
1179   tcp_bt_track_rxt (tc, snd_una + burst + 10, snd_una + 2 * burst + 10);
1180   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1181   TCP_TEST (pool_elts (bt->samples) == 6, "there should be 6 samples %u",
1182             pool_elts (bt->samples));
1183
1184   /* Retransmit covers last sample entirely so it should be removed */
1185   tcp_bt_track_rxt (tc, snd_una + 2 * burst + 20, snd_una + 4 * burst);
1186   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1187   TCP_TEST (pool_elts (bt->samples) == 5, "there should be 5 samples %u",
1188             pool_elts (bt->samples));
1189
1190   vec_validate (min_seqs, 4);
1191   min_seqs[0] = snd_una + 10;
1192   min_seqs[1] = snd_una + burst;
1193   min_seqs[2] = snd_una + burst + 10;
1194   min_seqs[3] = snd_una + 2 * burst + 10;
1195   min_seqs[4] = snd_una + 2 * burst + 20;
1196
1197   root = bt->sample_lookup.nodes + bt->sample_lookup.root;
1198   bts = bt->samples + bt->head;
1199   for (i = 0; i < vec_len (min_seqs); i++)
1200     {
1201       if (bts->min_seq != min_seqs[i])
1202         TCP_TEST (0, "should be %u is %u", min_seqs[i], bts->min_seq);
1203       rbn = rb_tree_search_subtree_custom (&bt->sample_lookup, root,
1204                                            bts->min_seq, tbt_seq_lt);
1205       if (rbn->opaque != bts - bt->samples)
1206         TCP_TEST (0, "lookup should work");
1207       bts = bt->samples + bts->next;
1208     }
1209
1210   /* 7) check delivery rate at time 10
1211    *
1212    * tc->snd_una = snd_una + 2 * burst
1213    * sacks:
1214    * [snd_una + 2 * burst + 20, snd_una + 2 * burst + 30]
1215    * [snd_una + 2 * burst + 50, snd_una + 2 * burst + 60]
1216    */
1217   session_main.wrk[thread_index].last_vlib_time = 10;
1218   tc->snd_una = snd_una + 2 * burst;
1219   tc->bytes_acked = 2 * burst - 10;
1220   sb->last_sacked_bytes = 20;
1221
1222   sacks[0].start = snd_una + 2 * burst + 20;
1223   sacks[0].end = snd_una + 2 * burst + 30;
1224   sacks[1].start = snd_una + 2 * burst + 50;
1225   sacks[1].end = snd_una + 2 * burst + 60;
1226
1227   tcp_bt_sample_delivery_rate (tc, rs);
1228
1229   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1230   TCP_TEST (pool_elts (bt->samples) == 5, "num samples should be 5 is %u",
1231             pool_elts (bt->samples));
1232   TCP_TEST (tc->delivered_time == 10, "delivered time should be 10");
1233   TCP_TEST (tc->delivered == 5 * burst + 40, "delivered should be %u is %u",
1234             5 * burst + 40, tc->delivered);
1235   /* A rxt was acked and delivered time for it is 8 (last ack time) so
1236    * ack_time is 2 (8 - 10). However, first_tx_time for rxt was 4 and rxt
1237    * time 9. Therefore snd_time is 5 (9 - 4)*/
1238   TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1239             rs->interval_time);
1240   /* delivered_now - delivered_rxt ~ 5 * burst + 40 - 3 * burst - 30 */
1241   TCP_TEST (rs->delivered == 2 * burst + 10, "delivered should be 210 is %u",
1242             rs->delivered);
1243   TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1244             "sample delivered should be %u", 3 * burst + 30);
1245   TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1246   /* Sample is app limited because of the retransmits */
1247   TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1248   TCP_TEST (tc->app_limited, "app limited should be set");
1249   TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1250
1251
1252   /*
1253    * 8) check delivery rate at time 11
1254    */
1255   session_main.wrk[thread_index].last_vlib_time = 11;
1256   tc->snd_una = tc->snd_nxt;
1257   tc->bytes_acked = 2 * burst;
1258   sb->last_sacked_bytes = 0;
1259   sb->last_bytes_delivered = 40;
1260
1261   memset (rs, 0, sizeof (*rs));
1262   tcp_bt_sample_delivery_rate (tc, rs);
1263
1264   TCP_TEST (tcp_bt_is_sane (bt), "tracker should be sane");
1265   TCP_TEST (pool_elts (bt->samples) == 0, "num samples should be 0 is %u",
1266             pool_elts (bt->samples));
1267   TCP_TEST (tc->delivered_time == 11, "delivered time should be 11");
1268   TCP_TEST (tc->delivered == 7 * burst, "delivered should be %u is %u",
1269             7 * burst, tc->delivered);
1270   /* Delivered time at retransmit was 8 so ack_time is 11 - 8 = 3. However,
1271    * first_tx_time for rxt was 4 and rxt time was 9. Therefore snd_time
1272    * is 9 - 4 = 5 */
1273   TCP_TEST (rs->interval_time == 5, "ack time should be 5 is %.2f",
1274             rs->interval_time);
1275   /* delivered_now - delivered_rxt ~ 7 * burst - 3 * burst - 30.
1276    * That's because we didn't retransmit any new segment. */
1277   TCP_TEST (rs->delivered == 4 * burst - 30, "delivered should be 160 is %u",
1278             rs->delivered);
1279   TCP_TEST (rs->prior_delivered == 3 * burst + 30,
1280             "sample delivered should be %u", 3 * burst + 30);
1281   TCP_TEST (rs->flags & TCP_BTS_IS_RXT, "is retransmitted");
1282   TCP_TEST (rs->flags & TCP_BTS_IS_APP_LIMITED, "is app limited");
1283   TCP_TEST (tc->app_limited == 0, "app limited should be cleared");
1284   TCP_TEST (tc->first_tx_time == 9, "first_tx_time %u", tc->first_tx_time);
1285
1286   /*
1287    * 9) test flush
1288    */
1289
1290   tcp_bt_track_tx (tc, burst);
1291   tc->snd_nxt += burst;
1292
1293   session_main.wrk[thread_index].last_vlib_time = 12;
1294   tcp_bt_track_tx (tc, burst);
1295   tc->snd_nxt += burst;
1296
1297   tcp_bt_flush_samples (tc);
1298
1299   /*
1300    * Cleanup
1301    */
1302   vec_free (sacks);
1303   vec_free (min_seqs);
1304   tcp_bt_cleanup (tc);
1305   return 0;
1306 }
1307
1308 static clib_error_t *
1309 tcp_test (vlib_main_t * vm,
1310           unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1311 {
1312   int res = 0;
1313
1314   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1315     {
1316       if (unformat (input, "sack"))
1317         {
1318           res = tcp_test_sack (vm, input);
1319         }
1320       else if (unformat (input, "session"))
1321         {
1322           res = tcp_test_session (vm, input);
1323         }
1324       else if (unformat (input, "lookup"))
1325         {
1326           res = tcp_test_lookup (vm, input);
1327         }
1328       else if (unformat (input, "delivery"))
1329         {
1330           res = tcp_test_delivery (vm, input);
1331         }
1332       else if (unformat (input, "all"))
1333         {
1334           if ((res = tcp_test_sack (vm, input)))
1335             goto done;
1336           if ((res = tcp_test_lookup (vm, input)))
1337             goto done;
1338           if ((res = tcp_test_delivery (vm, input)))
1339             goto done;
1340         }
1341       else
1342         break;
1343     }
1344
1345 done:
1346   if (res)
1347     return clib_error_return (0, "TCP unit test failed");
1348   return 0;
1349 }
1350
1351 /* *INDENT-OFF* */
1352 VLIB_CLI_COMMAND (tcp_test_command, static) =
1353 {
1354   .path = "test tcp",
1355   .short_help = "internal tcp unit tests",
1356   .function = tcp_test,
1357 };
1358 /* *INDENT-ON* */
1359
1360 /*
1361  * fd.io coding-style-patch-verification: ON
1362  *
1363  * Local Variables:
1364  * eval: (c-set-style "gnu")
1365  * End:
1366  */