tcp_cdg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <net/tcp.h>
  31. #define HYSTART_ACK_TRAIN 1
  32. #define HYSTART_DELAY 2
  33. static int window __read_mostly = 8;
  34. static unsigned int backoff_beta __read_mostly = 0.7071 * 1024; /* sqrt 0.5 */
  35. static unsigned int backoff_factor __read_mostly = 42;
  36. static unsigned int hystart_detect __read_mostly = 3;
  37. static unsigned int use_ineff __read_mostly = 5;
  38. static bool use_shadow __read_mostly = true;
  39. static bool use_tolerance __read_mostly;
  40. module_param(window, int, 0444);
  41. MODULE_PARM_DESC(window, "gradient window size (power of two <= 256)");
  42. module_param(backoff_beta, uint, 0644);
  43. MODULE_PARM_DESC(backoff_beta, "backoff beta (0-1024)");
  44. module_param(backoff_factor, uint, 0644);
  45. MODULE_PARM_DESC(backoff_factor, "backoff probability scale factor");
  46. module_param(hystart_detect, uint, 0644);
  47. MODULE_PARM_DESC(hystart_detect, "use Hybrid Slow start "
  48. "(0: disabled, 1: ACK train, 2: delay threshold, 3: both)");
  49. module_param(use_ineff, uint, 0644);
  50. MODULE_PARM_DESC(use_ineff, "use ineffectual backoff detection (threshold)");
  51. module_param(use_shadow, bool, 0644);
  52. MODULE_PARM_DESC(use_shadow, "use shadow window heuristic");
  53. module_param(use_tolerance, bool, 0644);
  54. MODULE_PARM_DESC(use_tolerance, "use loss tolerance heuristic");
  55. struct minmax {
  56. union {
  57. struct {
  58. s32 min;
  59. s32 max;
  60. };
  61. u64 v64;
  62. };
  63. };
  64. enum cdg_state {
  65. CDG_UNKNOWN = 0,
  66. CDG_NONFULL = 1,
  67. CDG_FULL = 2,
  68. CDG_BACKOFF = 3,
  69. };
  70. struct cdg {
  71. struct minmax rtt;
  72. struct minmax rtt_prev;
  73. struct minmax *gradients;
  74. struct minmax gsum;
  75. bool gfilled;
  76. u8 tail;
  77. u8 state;
  78. u8 delack;
  79. u32 rtt_seq;
  80. u32 undo_cwnd;
  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_BH(sock_net(sk),
  142. LINUX_MIB_TCPHYSTARTTRAINDETECT);
  143. NET_ADD_STATS_BH(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_BH(sock_net(sk),
  159. LINUX_MIB_TCPHYSTARTDELAYDETECT);
  160. NET_ADD_STATS_BH(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, u32 num_acked, s32 rtt_us)
  259. {
  260. struct cdg *ca = inet_csk_ca(sk);
  261. struct tcp_sock *tp = tcp_sk(sk);
  262. if (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 (num_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, rtt_us);
  274. ca->delack--;
  275. return;
  276. } else if (num_acked > 1 && ca->delack < 5) {
  277. ca->delack++;
  278. }
  279. }
  280. ca->rtt.min = min_not_zero(ca->rtt.min, rtt_us);
  281. ca->rtt.max = max(ca->rtt.max, 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. ca->undo_cwnd = tp->snd_cwnd;
  288. if (ca->state == CDG_BACKOFF)
  289. return max(2U, (tp->snd_cwnd * min(1024U, backoff_beta)) >> 10);
  290. if (ca->state == CDG_NONFULL && use_tolerance)
  291. return tp->snd_cwnd;
  292. ca->shadow_wnd = min(ca->shadow_wnd >> 1, tp->snd_cwnd);
  293. if (use_shadow)
  294. return max3(2U, ca->shadow_wnd, tp->snd_cwnd >> 1);
  295. return max(2U, tp->snd_cwnd >> 1);
  296. }
  297. static u32 tcp_cdg_undo_cwnd(struct sock *sk)
  298. {
  299. struct cdg *ca = inet_csk_ca(sk);
  300. return max(tcp_sk(sk)->snd_cwnd, ca->undo_cwnd);
  301. }
  302. static void tcp_cdg_cwnd_event(struct sock *sk, const enum tcp_ca_event ev)
  303. {
  304. struct cdg *ca = inet_csk_ca(sk);
  305. struct tcp_sock *tp = tcp_sk(sk);
  306. struct minmax *gradients;
  307. switch (ev) {
  308. case CA_EVENT_CWND_RESTART:
  309. gradients = ca->gradients;
  310. if (gradients)
  311. memset(gradients, 0, window * sizeof(gradients[0]));
  312. memset(ca, 0, sizeof(*ca));
  313. ca->gradients = gradients;
  314. ca->rtt_seq = tp->snd_nxt;
  315. ca->shadow_wnd = tp->snd_cwnd;
  316. break;
  317. case CA_EVENT_COMPLETE_CWR:
  318. ca->state = CDG_UNKNOWN;
  319. ca->rtt_seq = tp->snd_nxt;
  320. ca->rtt_prev = ca->rtt;
  321. ca->rtt.v64 = 0;
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. static void tcp_cdg_init(struct sock *sk)
  328. {
  329. struct cdg *ca = inet_csk_ca(sk);
  330. struct tcp_sock *tp = tcp_sk(sk);
  331. /* We silently fall back to window = 1 if allocation fails. */
  332. if (window > 1)
  333. ca->gradients = kcalloc(window, sizeof(ca->gradients[0]),
  334. GFP_NOWAIT | __GFP_NOWARN);
  335. ca->rtt_seq = tp->snd_nxt;
  336. ca->shadow_wnd = tp->snd_cwnd;
  337. }
  338. static void tcp_cdg_release(struct sock *sk)
  339. {
  340. struct cdg *ca = inet_csk_ca(sk);
  341. kfree(ca->gradients);
  342. }
  343. struct tcp_congestion_ops tcp_cdg __read_mostly = {
  344. .cong_avoid = tcp_cdg_cong_avoid,
  345. .cwnd_event = tcp_cdg_cwnd_event,
  346. .pkts_acked = tcp_cdg_acked,
  347. .undo_cwnd = tcp_cdg_undo_cwnd,
  348. .ssthresh = tcp_cdg_ssthresh,
  349. .release = tcp_cdg_release,
  350. .init = tcp_cdg_init,
  351. .owner = THIS_MODULE,
  352. .name = "cdg",
  353. };
  354. static int __init tcp_cdg_register(void)
  355. {
  356. if (backoff_beta > 1024 || window < 1 || window > 256)
  357. return -ERANGE;
  358. if (!is_power_of_2(window))
  359. return -EINVAL;
  360. BUILD_BUG_ON(sizeof(struct cdg) > ICSK_CA_PRIV_SIZE);
  361. tcp_register_congestion_control(&tcp_cdg);
  362. return 0;
  363. }
  364. static void __exit tcp_cdg_unregister(void)
  365. {
  366. tcp_unregister_congestion_control(&tcp_cdg);
  367. }
  368. module_init(tcp_cdg_register);
  369. module_exit(tcp_cdg_unregister);
  370. MODULE_AUTHOR("Kenneth Klette Jonassen");
  371. MODULE_LICENSE("GPL");
  372. MODULE_DESCRIPTION("TCP CDG");