tcp_cdg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CAIA Delay-Gradient (CDG) congestion control
  4. *
  5. * This implementation is based on the paper:
  6. * D.A. Hayes and G. Armitage. "Revisiting TCP congestion control using
  7. * delay gradients." In IFIP Networking, pages 328-341. Springer, 2011.
  8. *
  9. * Scavenger traffic (Less-than-Best-Effort) should disable coexistence
  10. * heuristics using parameters use_shadow=0 and use_ineff=0.
  11. *
  12. * Parameters window, backoff_beta, and backoff_factor are crucial for
  13. * throughput and delay. Future work is needed to determine better defaults,
  14. * and to provide guidelines for use in different environments/contexts.
  15. *
  16. * Except for window, knobs are configured via /sys/module/tcp_cdg/parameters/.
  17. * Parameter window is only configurable when loading tcp_cdg as a module.
  18. *
  19. * Notable differences from paper/FreeBSD:
  20. * o Using Hybrid Slow start and Proportional Rate Reduction.
  21. * o Add toggle for shadow window mechanism. Suggested by David Hayes.
  22. * o Add toggle for non-congestion loss tolerance.
  23. * o Scaling parameter G is changed to a backoff factor;
  24. * conversion is given by: backoff_factor = 1000/(G * window).
  25. * o Limit shadow window to 2 * cwnd, or to cwnd when application limited.
  26. * o More accurate e^-x.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/random.h>
  30. #include <linux/module.h>
  31. #include <linux/sched/clock.h>
  32. #include <net/tcp.h>
  33. #define HYSTART_ACK_TRAIN 1
  34. #define HYSTART_DELAY 2
  35. static int window __read_mostly = 8;
  36. static unsigned int backoff_beta __read_mostly = 0.7071 * 1024; /* sqrt 0.5 */
  37. static unsigned int backoff_factor __read_mostly = 42;
  38. static unsigned int hystart_detect __read_mostly = 3;
  39. static unsigned int use_ineff __read_mostly = 5;
  40. static bool use_shadow __read_mostly = true;
  41. static bool use_tolerance __read_mostly;
  42. module_param(window, int, 0444);
  43. MODULE_PARM_DESC(window, "gradient window size (power of two <= 256)");
  44. module_param(backoff_beta, uint, 0644);
  45. MODULE_PARM_DESC(backoff_beta, "backoff beta (0-1024)");
  46. module_param(backoff_factor, uint, 0644);
  47. MODULE_PARM_DESC(backoff_factor, "backoff probability scale factor");
  48. module_param(hystart_detect, uint, 0644);
  49. MODULE_PARM_DESC(hystart_detect, "use Hybrid Slow start "
  50. "(0: disabled, 1: ACK train, 2: delay threshold, 3: both)");
  51. module_param(use_ineff, uint, 0644);
  52. MODULE_PARM_DESC(use_ineff, "use ineffectual backoff detection (threshold)");
  53. module_param(use_shadow, bool, 0644);
  54. MODULE_PARM_DESC(use_shadow, "use shadow window heuristic");
  55. module_param(use_tolerance, bool, 0644);
  56. MODULE_PARM_DESC(use_tolerance, "use loss tolerance heuristic");
  57. struct cdg_minmax {
  58. union {
  59. struct {
  60. s32 min;
  61. s32 max;
  62. };
  63. u64 v64;
  64. };
  65. };
  66. enum cdg_state {
  67. CDG_UNKNOWN = 0,
  68. CDG_NONFULL = 1,
  69. CDG_FULL = 2,
  70. CDG_BACKOFF = 3,
  71. };
  72. struct cdg {
  73. struct cdg_minmax rtt;
  74. struct cdg_minmax rtt_prev;
  75. struct cdg_minmax *gradients;
  76. struct cdg_minmax gsum;
  77. bool gfilled;
  78. u8 tail;
  79. u8 state;
  80. u8 delack;
  81. u32 rtt_seq;
  82. u32 shadow_wnd;
  83. u16 backoff_cnt;
  84. u16 sample_cnt;
  85. s32 delay_min;
  86. u32 last_ack;
  87. u32 round_start;
  88. };
  89. /**
  90. * nexp_u32 - negative base-e exponential
  91. * @ux: x in units of micro
  92. *
  93. * Returns exp(ux * -1e-6) * U32_MAX.
  94. */
  95. static u32 __pure nexp_u32(u32 ux)
  96. {
  97. static const u16 v[] = {
  98. /* exp(-x)*65536-1 for x = 0, 0.000256, 0.000512, ... */
  99. 65535,
  100. 65518, 65501, 65468, 65401, 65267, 65001, 64470, 63422,
  101. 61378, 57484, 50423, 38795, 22965, 8047, 987, 14,
  102. };
  103. u32 msb = ux >> 8;
  104. u32 res;
  105. int i;
  106. /* Cut off when ux >= 2^24 (actual result is <= 222/U32_MAX). */
  107. if (msb > U16_MAX)
  108. return 0;
  109. /* Scale first eight bits linearly: */
  110. res = U32_MAX - (ux & 0xff) * (U32_MAX / 1000000);
  111. /* Obtain e^(x + y + ...) by computing e^x * e^y * ...: */
  112. for (i = 1; msb; i++, msb >>= 1) {
  113. u32 y = v[i & -(msb & 1)] + U32_C(1);
  114. res = ((u64)res * y) >> 16;
  115. }
  116. return res;
  117. }
  118. /* Based on the HyStart algorithm (by Ha et al.) that is implemented in
  119. * tcp_cubic. Differences/experimental changes:
  120. * o Using Hayes' delayed ACK filter.
  121. * o Using a usec clock for the ACK train.
  122. * o Reset ACK train when application limited.
  123. * o Invoked at any cwnd (i.e. also when cwnd < 16).
  124. * o Invoked only when cwnd < ssthresh (i.e. not when cwnd == ssthresh).
  125. */
  126. static void tcp_cdg_hystart_update(struct sock *sk)
  127. {
  128. struct cdg *ca = inet_csk_ca(sk);
  129. struct tcp_sock *tp = tcp_sk(sk);
  130. ca->delay_min = min_not_zero(ca->delay_min, ca->rtt.min);
  131. if (ca->delay_min == 0)
  132. return;
  133. if (hystart_detect & HYSTART_ACK_TRAIN) {
  134. u32 now_us = tp->tcp_mstamp;
  135. if (ca->last_ack == 0 || !tcp_is_cwnd_limited(sk)) {
  136. ca->last_ack = now_us;
  137. ca->round_start = now_us;
  138. } else if (before(now_us, ca->last_ack + 3000)) {
  139. u32 base_owd = max(ca->delay_min / 2U, 125U);
  140. ca->last_ack = now_us;
  141. if (after(now_us, ca->round_start + base_owd)) {
  142. NET_INC_STATS(sock_net(sk),
  143. LINUX_MIB_TCPHYSTARTTRAINDETECT);
  144. NET_ADD_STATS(sock_net(sk),
  145. LINUX_MIB_TCPHYSTARTTRAINCWND,
  146. tcp_snd_cwnd(tp));
  147. tp->snd_ssthresh = tcp_snd_cwnd(tp);
  148. return;
  149. }
  150. }
  151. }
  152. if (hystart_detect & HYSTART_DELAY) {
  153. if (ca->sample_cnt < 8) {
  154. ca->sample_cnt++;
  155. } else {
  156. s32 thresh = max(ca->delay_min + ca->delay_min / 8U,
  157. 125U);
  158. if (ca->rtt.min > thresh) {
  159. NET_INC_STATS(sock_net(sk),
  160. LINUX_MIB_TCPHYSTARTDELAYDETECT);
  161. NET_ADD_STATS(sock_net(sk),
  162. LINUX_MIB_TCPHYSTARTDELAYCWND,
  163. tcp_snd_cwnd(tp));
  164. tp->snd_ssthresh = tcp_snd_cwnd(tp);
  165. }
  166. }
  167. }
  168. }
  169. static s32 tcp_cdg_grad(struct cdg *ca)
  170. {
  171. s32 gmin = ca->rtt.min - ca->rtt_prev.min;
  172. s32 gmax = ca->rtt.max - ca->rtt_prev.max;
  173. s32 grad;
  174. if (ca->gradients) {
  175. ca->gsum.min += gmin - ca->gradients[ca->tail].min;
  176. ca->gsum.max += gmax - ca->gradients[ca->tail].max;
  177. ca->gradients[ca->tail].min = gmin;
  178. ca->gradients[ca->tail].max = gmax;
  179. ca->tail = (ca->tail + 1) & (window - 1);
  180. gmin = ca->gsum.min;
  181. gmax = ca->gsum.max;
  182. }
  183. /* We keep sums to ignore gradients during cwnd reductions;
  184. * the paper's smoothed gradients otherwise simplify to:
  185. * (rtt_latest - rtt_oldest) / window.
  186. *
  187. * We also drop division by window here.
  188. */
  189. grad = gmin > 0 ? gmin : gmax;
  190. /* Extrapolate missing values in gradient window: */
  191. if (!ca->gfilled) {
  192. if (!ca->gradients && window > 1)
  193. grad *= window; /* Memory allocation failed. */
  194. else if (ca->tail == 0)
  195. ca->gfilled = true;
  196. else
  197. grad = (grad * window) / (int)ca->tail;
  198. }
  199. /* Backoff was effectual: */
  200. if (gmin <= -32 || gmax <= -32)
  201. ca->backoff_cnt = 0;
  202. if (use_tolerance) {
  203. /* Reduce small variations to zero: */
  204. gmin = DIV_ROUND_CLOSEST(gmin, 64);
  205. gmax = DIV_ROUND_CLOSEST(gmax, 64);
  206. if (gmin > 0 && gmax <= 0)
  207. ca->state = CDG_FULL;
  208. else if ((gmin > 0 && gmax > 0) || gmax < 0)
  209. ca->state = CDG_NONFULL;
  210. }
  211. return grad;
  212. }
  213. static bool tcp_cdg_backoff(struct sock *sk, u32 grad)
  214. {
  215. struct cdg *ca = inet_csk_ca(sk);
  216. struct tcp_sock *tp = tcp_sk(sk);
  217. if (get_random_u32() <= nexp_u32(grad * backoff_factor))
  218. return false;
  219. if (use_ineff) {
  220. ca->backoff_cnt++;
  221. if (ca->backoff_cnt > use_ineff)
  222. return false;
  223. }
  224. ca->shadow_wnd = max(ca->shadow_wnd, tcp_snd_cwnd(tp));
  225. ca->state = CDG_BACKOFF;
  226. tcp_enter_cwr(sk);
  227. return true;
  228. }
  229. /* Not called in CWR or Recovery state. */
  230. static void tcp_cdg_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  231. {
  232. struct cdg *ca = inet_csk_ca(sk);
  233. struct tcp_sock *tp = tcp_sk(sk);
  234. u32 prior_snd_cwnd;
  235. u32 incr;
  236. if (tcp_in_slow_start(tp) && hystart_detect)
  237. tcp_cdg_hystart_update(sk);
  238. if (after(ack, ca->rtt_seq) && ca->rtt.v64) {
  239. s32 grad = 0;
  240. if (ca->rtt_prev.v64)
  241. grad = tcp_cdg_grad(ca);
  242. ca->rtt_seq = tp->snd_nxt;
  243. ca->rtt_prev = ca->rtt;
  244. ca->rtt.v64 = 0;
  245. ca->last_ack = 0;
  246. ca->sample_cnt = 0;
  247. if (grad > 0 && tcp_cdg_backoff(sk, grad))
  248. return;
  249. }
  250. if (!tcp_is_cwnd_limited(sk)) {
  251. ca->shadow_wnd = min(ca->shadow_wnd, tcp_snd_cwnd(tp));
  252. return;
  253. }
  254. prior_snd_cwnd = tcp_snd_cwnd(tp);
  255. tcp_reno_cong_avoid(sk, ack, acked);
  256. incr = tcp_snd_cwnd(tp) - prior_snd_cwnd;
  257. ca->shadow_wnd = max(ca->shadow_wnd, ca->shadow_wnd + incr);
  258. }
  259. static void tcp_cdg_acked(struct sock *sk, const struct ack_sample *sample)
  260. {
  261. struct cdg *ca = inet_csk_ca(sk);
  262. struct tcp_sock *tp = tcp_sk(sk);
  263. if (sample->rtt_us <= 0)
  264. return;
  265. /* A heuristic for filtering delayed ACKs, adapted from:
  266. * D.A. Hayes. "Timing enhancements to the FreeBSD kernel to support
  267. * delay and rate based TCP mechanisms." TR 100219A. CAIA, 2010.
  268. */
  269. if (tp->sacked_out == 0) {
  270. if (sample->pkts_acked == 1 && ca->delack) {
  271. /* A delayed ACK is only used for the minimum if it is
  272. * provenly lower than an existing non-zero minimum.
  273. */
  274. ca->rtt.min = min(ca->rtt.min, sample->rtt_us);
  275. ca->delack--;
  276. return;
  277. } else if (sample->pkts_acked > 1 && ca->delack < 5) {
  278. ca->delack++;
  279. }
  280. }
  281. ca->rtt.min = min_not_zero(ca->rtt.min, sample->rtt_us);
  282. ca->rtt.max = max(ca->rtt.max, sample->rtt_us);
  283. }
  284. static u32 tcp_cdg_ssthresh(struct sock *sk)
  285. {
  286. struct cdg *ca = inet_csk_ca(sk);
  287. struct tcp_sock *tp = tcp_sk(sk);
  288. if (ca->state == CDG_BACKOFF)
  289. return max(2U, (tcp_snd_cwnd(tp) * min(1024U, backoff_beta)) >> 10);
  290. if (ca->state == CDG_NONFULL && use_tolerance)
  291. return tcp_snd_cwnd(tp);
  292. ca->shadow_wnd = min(ca->shadow_wnd >> 1, tcp_snd_cwnd(tp));
  293. if (use_shadow)
  294. return max3(2U, ca->shadow_wnd, tcp_snd_cwnd(tp) >> 1);
  295. return max(2U, tcp_snd_cwnd(tp) >> 1);
  296. }
  297. static void tcp_cdg_cwnd_event(struct sock *sk, const enum tcp_ca_event ev)
  298. {
  299. struct cdg *ca = inet_csk_ca(sk);
  300. struct tcp_sock *tp = tcp_sk(sk);
  301. struct cdg_minmax *gradients;
  302. switch (ev) {
  303. case CA_EVENT_CWND_RESTART:
  304. gradients = ca->gradients;
  305. if (gradients)
  306. memset(gradients, 0, window * sizeof(gradients[0]));
  307. memset(ca, 0, sizeof(*ca));
  308. ca->gradients = gradients;
  309. ca->rtt_seq = tp->snd_nxt;
  310. ca->shadow_wnd = tcp_snd_cwnd(tp);
  311. break;
  312. case CA_EVENT_COMPLETE_CWR:
  313. ca->state = CDG_UNKNOWN;
  314. ca->rtt_seq = tp->snd_nxt;
  315. ca->rtt_prev = ca->rtt;
  316. ca->rtt.v64 = 0;
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. static void tcp_cdg_init(struct sock *sk)
  323. {
  324. struct cdg *ca = inet_csk_ca(sk);
  325. struct tcp_sock *tp = tcp_sk(sk);
  326. ca->gradients = NULL;
  327. /* We silently fall back to window = 1 if allocation fails. */
  328. if (window > 1)
  329. ca->gradients = kcalloc(window, sizeof(ca->gradients[0]),
  330. GFP_NOWAIT | __GFP_NOWARN);
  331. ca->rtt_seq = tp->snd_nxt;
  332. ca->shadow_wnd = tcp_snd_cwnd(tp);
  333. }
  334. static void tcp_cdg_release(struct sock *sk)
  335. {
  336. struct cdg *ca = inet_csk_ca(sk);
  337. kfree(ca->gradients);
  338. ca->gradients = NULL;
  339. }
  340. static struct tcp_congestion_ops tcp_cdg __read_mostly = {
  341. .cong_avoid = tcp_cdg_cong_avoid,
  342. .cwnd_event = tcp_cdg_cwnd_event,
  343. .pkts_acked = tcp_cdg_acked,
  344. .undo_cwnd = tcp_reno_undo_cwnd,
  345. .ssthresh = tcp_cdg_ssthresh,
  346. .release = tcp_cdg_release,
  347. .init = tcp_cdg_init,
  348. .owner = THIS_MODULE,
  349. .name = "cdg",
  350. };
  351. static int __init tcp_cdg_register(void)
  352. {
  353. if (backoff_beta > 1024 || window < 1 || window > 256)
  354. return -ERANGE;
  355. if (!is_power_of_2(window))
  356. return -EINVAL;
  357. BUILD_BUG_ON(sizeof(struct cdg) > ICSK_CA_PRIV_SIZE);
  358. tcp_register_congestion_control(&tcp_cdg);
  359. return 0;
  360. }
  361. static void __exit tcp_cdg_unregister(void)
  362. {
  363. tcp_unregister_congestion_control(&tcp_cdg);
  364. }
  365. module_init(tcp_cdg_register);
  366. module_exit(tcp_cdg_unregister);
  367. MODULE_AUTHOR("Kenneth Klette Jonassen");
  368. MODULE_LICENSE("GPL");
  369. MODULE_DESCRIPTION("TCP CDG");