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