lsquic_cubic.c revision 5392f7a3
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_cubic.c -- LSQUIC CUBIC implementation. 4 */ 5 6#include <inttypes.h> 7#include <math.h> 8#include <stddef.h> 9#include <stdlib.h> 10#include <string.h> 11#include <sys/queue.h> 12#ifdef WIN32 13#include <vc_compat.h> 14#endif 15 16#include "lsquic_int_types.h" 17#include "lsquic_types.h" 18#include "lsquic_hash.h" 19#include "lsquic_util.h" 20#include "lsquic_cong_ctl.h" 21#include "lsquic_sfcw.h" 22#include "lsquic_conn_flow.h" 23#include "lsquic_varint.h" 24#include "lsquic_hq.h" 25#include "lsquic_stream.h" 26#include "lsquic_rtt.h" 27#include "lsquic_conn_public.h" 28#include "lsquic_packet_common.h" 29#include "lsquic_packet_out.h" 30#include "lsquic_cubic.h" 31 32#define LSQUIC_LOGGER_MODULE LSQLM_CUBIC 33#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(cubic->cu_conn) 34#include "lsquic_logger.h" 35 36#define FAST_CONVERGENCE 1 37#define BETA 205 /* 205/1024 */ 38#define C 410 /* 410/1024 */ 39#define TWO_MINUS_BETA_OVER_TWO 922 /* 922/1024 */ 40#define ONE_MINUS_BETA 819 /* 819/1024 */ 41#define ONE_OVER_C 2560 /* 2560/1024 */ 42 43static void 44cubic_reset (struct lsquic_cubic *cubic) 45{ 46 memset(cubic, 0, offsetof(struct lsquic_cubic, cu_conn)); 47 cubic->cu_cwnd = 32 * TCP_MSS; 48 cubic->cu_last_max_cwnd = 32 * TCP_MSS; 49 cubic->cu_tcp_cwnd = 32 * TCP_MSS; 50} 51 52 53static void 54cubic_update (struct lsquic_cubic *cubic, lsquic_time_t now, unsigned n_bytes) 55{ 56 double delta_t, t; 57 lsquic_time_t target; 58 59 if (0 == cubic->cu_epoch_start) 60 { 61 cubic->cu_epoch_start = now; 62 if (cubic->cu_cwnd < cubic->cu_last_max_cwnd) 63 { 64 cubic->cu_K = cbrt(cubic->cu_last_max_cwnd / TCP_MSS / 2); 65 cubic->cu_origin_point = cubic->cu_last_max_cwnd; 66 } 67 else 68 { 69 cubic->cu_K = 0; 70 cubic->cu_origin_point = cubic->cu_cwnd; 71 } 72 LSQ_DEBUG("cwnd: %lu; last_max_cwnd: %lu; K: %lf; origin_point: %lu", 73 cubic->cu_cwnd, cubic->cu_last_max_cwnd, cubic->cu_K, cubic->cu_origin_point); 74 } 75 76 delta_t = (double) (now + cubic->cu_min_delay - cubic->cu_epoch_start) / 1000000; 77 if (delta_t < cubic->cu_K) 78 { 79 t = cubic->cu_K - delta_t; 80 target = cubic->cu_origin_point - t * t * t * 0.4 * TCP_MSS; 81 LSQ_DEBUG("delta_t: %lf; t: %lf; target 1: %"PRIu64, delta_t, t, target); 82 } 83 else 84 { 85 t = delta_t - cubic->cu_K; 86 target = cubic->cu_origin_point + t * t * t * 0.4 * TCP_MSS; 87 LSQ_DEBUG("target 2: %"PRIu64, target); 88 } 89 90 if (cubic->cu_flags & CU_TCP_FRIENDLY) 91 { 92 cubic->cu_tcp_cwnd += n_bytes * TCP_MSS * ONE_MINUS_BETA / 1024 93 / cubic->cu_tcp_cwnd; 94 LSQ_DEBUG("delta_t: %lf; last_max: %lu; cu_tcp_cwnd: %lu; target: " 95 "%"PRIu64"; over: %d; left: %d", delta_t, cubic->cu_last_max_cwnd, 96 cubic->cu_tcp_cwnd, target, cubic->cu_tcp_cwnd > target, 97 delta_t < cubic->cu_K); 98 if (cubic->cu_tcp_cwnd > target) 99 target = cubic->cu_tcp_cwnd; 100 } 101 102 if (target == 0) 103 target = TCP_MSS; 104 105 cubic->cu_cwnd = target; 106} 107 108 109void 110lsquic_cubic_set_flags (struct lsquic_cubic *cubic, enum cubic_flags flags) 111{ 112 LSQ_DEBUG("%s(cubic, 0x%X)", __func__, flags); 113 cubic->cu_flags = flags; 114} 115 116 117static void 118lsquic_cubic_init (void *cong_ctl, const struct lsquic_conn_public *conn_pub, 119 enum quic_ft_bit UNUSED_retx_frames) 120{ 121 struct lsquic_cubic *const cubic = cong_ctl; 122 cubic_reset(cubic); 123 cubic->cu_ssthresh = 10000 * TCP_MSS; /* Emulate "unbounded" slow start */ 124 cubic->cu_conn = conn_pub->lconn; 125 cubic->cu_rtt_stats = &conn_pub->rtt_stats; 126 cubic->cu_flags = DEFAULT_CUBIC_FLAGS; 127#ifndef NDEBUG 128 const char *s; 129 s = getenv("LSQUIC_CUBIC_SAMPLING_RATE"); 130 if (s) 131 cubic->cu_sampling_rate = atoi(s); 132 else 133#endif 134 cubic->cu_sampling_rate = 100000; 135 LSQ_DEBUG("%s(cubic, $conn)", __func__); 136 LSQ_INFO("initialized"); 137} 138 139 140#define LOG_CWND(c) do { \ 141 if (LSQ_LOG_ENABLED(LSQ_LOG_INFO)) { \ 142 lsquic_time_t now = lsquic_time_now(); \ 143 now -= now % (c)->cu_sampling_rate; \ 144 if (now > (c)->cu_last_logged) { \ 145 LSQ_INFO("CWND: %lu", (c)->cu_cwnd); \ 146 (c)->cu_last_logged = now; \ 147 } \ 148 } \ 149} while (0) 150 151 152static void 153lsquic_cubic_was_quiet (void *cong_ctl, lsquic_time_t now, uint64_t in_flight) 154{ 155 struct lsquic_cubic *const cubic = cong_ctl; 156 LSQ_DEBUG("%s(cubic, %"PRIu64")", __func__, now); 157 cubic->cu_epoch_start = 0; 158} 159 160 161static void 162lsquic_cubic_ack (void *cong_ctl, struct lsquic_packet_out *packet_out, 163 unsigned n_bytes, lsquic_time_t now_time, int app_limited) 164{ 165 struct lsquic_cubic *const cubic = cong_ctl; 166 lsquic_time_t rtt; 167 168 rtt = now_time - packet_out->po_sent; 169 LSQ_DEBUG("%s(cubic, %"PRIu64", %"PRIu64", %d, %u)", __func__, now_time, rtt, 170 app_limited, n_bytes); 171 if (0 == cubic->cu_min_delay || rtt < cubic->cu_min_delay) 172 { 173 cubic->cu_min_delay = rtt; 174 LSQ_INFO("min_delay: %"PRIu64, rtt); 175 } 176 177 if (cubic->cu_cwnd <= cubic->cu_ssthresh) 178 { 179 cubic->cu_cwnd += TCP_MSS; 180 LSQ_DEBUG("ACK: slow threshold, cwnd: %lu", cubic->cu_cwnd); 181 } 182 else if (!app_limited) 183 { 184 cubic_update(cubic, now_time, n_bytes); 185 LSQ_DEBUG("ACK: cwnd: %lu", cubic->cu_cwnd); 186 } 187 188 LOG_CWND(cubic); 189} 190 191 192static void 193lsquic_cubic_loss (void *cong_ctl) 194{ 195 struct lsquic_cubic *const cubic = cong_ctl; 196 LSQ_DEBUG("%s(cubic)", __func__); 197 cubic->cu_epoch_start = 0; 198 if (FAST_CONVERGENCE && cubic->cu_cwnd < cubic->cu_last_max_cwnd) 199 cubic->cu_last_max_cwnd = cubic->cu_cwnd * TWO_MINUS_BETA_OVER_TWO / 1024; 200 else 201 cubic->cu_last_max_cwnd = cubic->cu_cwnd; 202 cubic->cu_cwnd = cubic->cu_cwnd * ONE_MINUS_BETA / 1024; 203 cubic->cu_tcp_cwnd = cubic->cu_cwnd; 204 cubic->cu_ssthresh = cubic->cu_cwnd; 205 LSQ_INFO("loss detected, last_max_cwnd: %lu, cwnd: %lu", 206 cubic->cu_last_max_cwnd, cubic->cu_cwnd); 207 LOG_CWND(cubic); 208} 209 210 211static void 212lsquic_cubic_timeout (void *cong_ctl) 213{ 214 struct lsquic_cubic *const cubic = cong_ctl; 215 unsigned long cwnd; 216 217 cwnd = cubic->cu_cwnd; 218 LSQ_DEBUG("%s(cubic)", __func__); 219 cubic_reset(cubic); 220 cubic->cu_ssthresh = cwnd / 2; 221 cubic->cu_tcp_cwnd = 2 * TCP_MSS; 222 cubic->cu_cwnd = 2 * TCP_MSS; 223 LSQ_INFO("timeout, cwnd: %lu", cubic->cu_cwnd); 224 LOG_CWND(cubic); 225} 226 227 228static void 229lsquic_cubic_cleanup (void *cong_ctl) 230{ 231} 232 233 234static uint64_t 235lsquic_cubic_get_cwnd (void *cong_ctl) 236{ 237 struct lsquic_cubic *const cubic = cong_ctl; 238 return cubic->cu_cwnd; 239} 240 241 242static int 243in_slow_start (void *cong_ctl) 244{ 245 struct lsquic_cubic *const cubic = cong_ctl; 246 return cubic->cu_cwnd < cubic->cu_ssthresh; 247} 248 249 250static uint64_t 251lsquic_cubic_pacing_rate (void *cong_ctl, int in_recovery) 252{ 253 struct lsquic_cubic *const cubic = cong_ctl; 254 uint64_t bandwidth, pacing_rate; 255 lsquic_time_t srtt; 256 257 srtt = lsquic_rtt_stats_get_srtt(cubic->cu_rtt_stats); 258 if (srtt == 0) 259 srtt = 50000; 260 bandwidth = cubic->cu_cwnd * 1000000 / srtt; 261 if (in_slow_start(cubic)) 262 pacing_rate = bandwidth * 2; 263 else if (in_recovery) 264 pacing_rate = bandwidth; 265 else 266 pacing_rate = bandwidth + bandwidth / 4; 267 268 return pacing_rate; 269} 270 271 272 273const struct cong_ctl_if lsquic_cong_cubic_if = 274{ 275 .cci_ack = lsquic_cubic_ack, 276 .cci_cleanup = lsquic_cubic_cleanup, 277 .cci_get_cwnd = lsquic_cubic_get_cwnd, 278 .cci_init = lsquic_cubic_init, 279 .cci_pacing_rate = lsquic_cubic_pacing_rate, 280 .cci_loss = lsquic_cubic_loss, 281 .cci_timeout = lsquic_cubic_timeout, 282 .cci_was_quiet = lsquic_cubic_was_quiet, 283}; 284