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