tcp: count first lost hole (VPP-1465)

Change-Id: I3ac136e2a10796d8fa86ddb6f0d6cabe5fa749f8
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/plugins/unittest/tcp_test.c b/src/plugins/unittest/tcp_test.c
index 608f1ef..d065787 100644
--- a/src/plugins/unittest/tcp_test.c
+++ b/src/plugins/unittest/tcp_test.c
@@ -129,7 +129,7 @@
   TCP_TEST ((sb->last_sacked_bytes == 400),
 	    "last sacked bytes %d", sb->last_sacked_bytes);
   TCP_TEST ((sb->high_sacked == 900), "high sacked %u", sb->high_sacked);
-  TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
+  TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
 
   /*
    * Inject odd blocks
@@ -276,7 +276,7 @@
 		     "\n%U", format_tcp_scoreboard, sb, tc);
   TCP_TEST ((pool_elts (sb->holes) == 5),
 	    "scoreboard has %d elements", pool_elts (sb->holes));
-  TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
+  TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
 
   tcp_rcv_sacks (tc, 950);
 
@@ -368,7 +368,7 @@
 	    sb->last_sacked_bytes);
   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
 	    sb->last_bytes_delivered);
-  TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
+  TCP_TEST ((sb->lost_bytes == 300), "lost bytes %u", sb->lost_bytes);
 
   tc->snd_una = 100;
   tcp_rcv_sacks (tc, 200);
@@ -410,6 +410,32 @@
   TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
 	    sb->last_bytes_delivered);
   TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
+
+  /*
+   * Add [1200, 1500] and test that [1000, 1200] is lost (bytes condition)
+   * snd_una = 1000 and snd_una_max = 1600
+   */
+  tc->snd_una = 1000;
+  tc->snd_una_max = 1600;
+  vec_reset_length (tc->rcv_opts.sacks);
+  block.start = 1200;
+  block.end = 1500;
+  vec_add1 (tc->rcv_opts.sacks, block);
+  tc->rcv_opts.n_sack_blocks = vec_len (tc->rcv_opts.sacks);
+  tcp_rcv_sacks (tc, 1000);
+  if (verbose)
+    vlib_cli_output (vm, "\nacked [1200, 1500] test first hole is lost:\n%U",
+		     format_tcp_scoreboard, sb, tc);
+  TCP_TEST ((pool_elts (sb->holes) == 2), "scoreboard has %d elements",
+	    pool_elts (sb->holes));
+  TCP_TEST ((sb->sacked_bytes == 300), "sacked bytes %d", sb->sacked_bytes);
+  TCP_TEST ((sb->last_sacked_bytes == 300), "last sacked bytes %d",
+	    sb->last_sacked_bytes);
+  TCP_TEST ((sb->last_bytes_delivered == 0), "last bytes delivered %d",
+	    sb->last_bytes_delivered);
+  TCP_TEST ((sb->lost_bytes == 200), "lost bytes %u", sb->lost_bytes);
+  TCP_TEST ((sb->snd_una_adv == 0), "snd_una_adv %u", sb->snd_una_adv);
+
   return 0;
 }
 
diff --git a/src/vnet/tcp/tcp_input.c b/src/vnet/tcp/tcp_input.c
index e75c77d..87bacc2 100644
--- a/src/vnet/tcp/tcp_input.c
+++ b/src/vnet/tcp/tcp_input.c
@@ -680,29 +680,32 @@
     {
       bytes = sb->high_sacked - left->end;
       blks = 1;
-      if (bytes > (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
-	  && left->prev == TCP_INVALID_SACK_HOLE_INDEX)
-	sb->lost_bytes += scoreboard_hole_bytes (left);
     }
 
-  right = left;
-  while ((left = scoreboard_prev_hole (sb, right))
-	 && (bytes < (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
-	     && blks < TCP_DUPACK_THRESHOLD))
+  while ((right = left)
+	 && bytes < (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
+	 && blks < TCP_DUPACK_THRESHOLD
+	 /* left not updated if above conditions fail */
+	 && (left = scoreboard_prev_hole (sb, right)))
     {
       bytes += right->start - left->end;
       blks++;
-      right = left;
     }
 
-  while (left)
+  /* left is first lost */
+  if (left)
     {
-      bytes += right->start - left->end;
-      sb->lost_bytes += scoreboard_hole_bytes (left);
-      left->is_lost = 1;
-      right = left;
-      left = scoreboard_prev_hole (sb, left);
+      do
+	{
+	  sb->lost_bytes += scoreboard_hole_bytes (right);
+	  left->is_lost = 1;
+	  left = scoreboard_prev_hole (sb, right);
+	  if (left)
+	    bytes += right->start - left->end;
+	}
+      while ((right = left));
     }
+
   sb->sacked_bytes = bytes;
 }