From 3ee30a3e01d34be48b7596ad8395f181b04006bf Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Tue, 4 Aug 2020 17:16:30 +0900
Subject: [PATCH] Take into account integer division remainder

---
 lib/ngtcp2_cc.c | 18 ++++++++++++++----
 lib/ngtcp2_cc.h |  2 ++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/lib/ngtcp2_cc.c b/lib/ngtcp2_cc.c
index 38d97cc7..a508dbe9 100644
--- a/lib/ngtcp2_cc.c
+++ b/lib/ngtcp2_cc.c
@@ -276,6 +276,7 @@ void ngtcp2_cc_cubic_cc_on_pkt_acked(ngtcp2_cc *ccx, ngtcp2_conn_stat *cstat,
   uint64_t target;
   uint64_t tx, kx, time_delta, delta;
   uint64_t add, tcp_add;
+  uint64_t m;
 
   if (pkt->pktns_id == NGTCP2_PKTNS_ID_APP && cc->window_end != -1 &&
       cc->window_end <= pkt->pkt_num) {
@@ -341,6 +342,9 @@ void ngtcp2_cc_cubic_cc_on_pkt_acked(ngtcp2_cc *ccx, ngtcp2_conn_stat *cstat,
                     "cubic-ca epoch_start=%" PRIu64 " k=%" PRIu64
                     " origin_point=%" PRIu64,
                     cc->epoch_start, cc->k, cc->origin_point);
+
+    cc->pending_add = 0;
+    cc->pending_w_add = 0;
   }
 
   min_rtt = cstat->min_rtt == UINT64_MAX ? cstat->initial_rtt : cstat->min_rtt;
@@ -366,13 +370,19 @@ void ngtcp2_cc_cubic_cc_on_pkt_acked(ngtcp2_cc *ccx, ngtcp2_conn_stat *cstat,
   }
 
   if (target > cstat->cwnd) {
-    add = cstat->max_udp_payload_size * (target - cstat->cwnd) / cstat->cwnd;
+    m = cc->pending_add + cstat->max_udp_payload_size * (target - cstat->cwnd);
+    add = m / cstat->cwnd;
+    cc->pending_add = m % cstat->cwnd;
   } else {
-    /* TODO too small, no increment at all */
-    add = cstat->max_udp_payload_size / (100 * cstat->cwnd);
+    m = cc->pending_add + cstat->max_udp_payload_size;
+    add = m / (100 * cstat->cwnd);
+    cc->pending_add = m % (100 * cstat->cwnd);
   }
 
-  cc->w_tcp += cstat->max_udp_payload_size * pkt->pktlen * 9 / 17 / cstat->cwnd;
+  m = cc->pending_w_add + cstat->max_udp_payload_size * pkt->pktlen * 9;
+
+  cc->w_tcp += m / (17 * cstat->cwnd);
+  cc->pending_w_add = m % (17 * cstat->cwnd);
 
   if (cc->w_tcp > cstat->cwnd) {
     tcp_add =
diff --git a/lib/ngtcp2_cc.h b/lib/ngtcp2_cc.h
index c3886a6e..05010d57 100644
--- a/lib/ngtcp2_cc.h
+++ b/lib/ngtcp2_cc.h
@@ -95,6 +95,8 @@ typedef struct ngtcp2_cubic_cc {
   uint64_t current_round_min_rtt;
   uint64_t last_round_min_rtt;
   int64_t window_end;
+  uint64_t pending_add;
+  uint64_t pending_w_add;
 } ngtcp2_cubic_cc;
 
 int ngtcp2_cc_cubic_cc_init(ngtcp2_cc *cc, ngtcp2_log *log,
-- 
GitLab