tcp_cdg.c 11 KB

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