tcp_nv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * TCP NV: TCP with Congestion Avoidance
  3. *
  4. * TCP-NV is a successor of TCP-Vegas that has been developed to
  5. * deal with the issues that occur in modern networks.
  6. * Like TCP-Vegas, TCP-NV supports true congestion avoidance,
  7. * the ability to detect congestion before packet losses occur.
  8. * When congestion (queue buildup) starts to occur, TCP-NV
  9. * predicts what the cwnd size should be for the current
  10. * throughput and it reduces the cwnd proportionally to
  11. * the difference between the current cwnd and the predicted cwnd.
  12. *
  13. * NV is only recommeneded for traffic within a data center, and when
  14. * all the flows are NV (at least those within the data center). This
  15. * is due to the inherent unfairness between flows using losses to
  16. * detect congestion (congestion control) and those that use queue
  17. * buildup to detect congestion (congestion avoidance).
  18. *
  19. * Note: High NIC coalescence values may lower the performance of NV
  20. * due to the increased noise in RTT values. In particular, we have
  21. * seen issues with rx-frames values greater than 8.
  22. *
  23. * TODO:
  24. * 1) Add mechanism to deal with reverse congestion.
  25. */
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/math64.h>
  29. #include <net/tcp.h>
  30. #include <linux/inet_diag.h>
  31. /* TCP NV parameters
  32. *
  33. * nv_pad Max number of queued packets allowed in network
  34. * nv_pad_buffer Do not grow cwnd if this closed to nv_pad
  35. * nv_reset_period How often (in) seconds)to reset min_rtt
  36. * nv_min_cwnd Don't decrease cwnd below this if there are no losses
  37. * nv_cong_dec_mult Decrease cwnd by X% (30%) of congestion when detected
  38. * nv_ssthresh_factor On congestion set ssthresh to this * <desired cwnd> / 8
  39. * nv_rtt_factor RTT averaging factor
  40. * nv_loss_dec_factor Decrease cwnd by this (50%) when losses occur
  41. * nv_dec_eval_min_calls Wait this many RTT measurements before dec cwnd
  42. * nv_inc_eval_min_calls Wait this many RTT measurements before inc cwnd
  43. * nv_ssthresh_eval_min_calls Wait this many RTT measurements before stopping
  44. * slow-start due to congestion
  45. * nv_stop_rtt_cnt Only grow cwnd for this many RTTs after non-congestion
  46. * nv_rtt_min_cnt Wait these many RTTs before making congesion decision
  47. * nv_cwnd_growth_rate_neg
  48. * nv_cwnd_growth_rate_pos
  49. * How quickly to double growth rate (not rate) of cwnd when not
  50. * congested. One value (nv_cwnd_growth_rate_neg) for when
  51. * rate < 1 pkt/RTT (after losses). The other (nv_cwnd_growth_rate_pos)
  52. * otherwise.
  53. */
  54. static int nv_pad __read_mostly = 10;
  55. static int nv_pad_buffer __read_mostly = 2;
  56. static int nv_reset_period __read_mostly = 5; /* in seconds */
  57. static int nv_min_cwnd __read_mostly = 2;
  58. static int nv_cong_dec_mult __read_mostly = 30 * 128 / 100; /* = 30% */
  59. static int nv_ssthresh_factor __read_mostly = 8; /* = 1 */
  60. static int nv_rtt_factor __read_mostly = 128; /* = 1/2*old + 1/2*new */
  61. static int nv_loss_dec_factor __read_mostly = 512; /* => 50% */
  62. static int nv_cwnd_growth_rate_neg __read_mostly = 8;
  63. static int nv_cwnd_growth_rate_pos __read_mostly; /* 0 => fixed like Reno */
  64. static int nv_dec_eval_min_calls __read_mostly = 60;
  65. static int nv_inc_eval_min_calls __read_mostly = 20;
  66. static int nv_ssthresh_eval_min_calls __read_mostly = 30;
  67. static int nv_stop_rtt_cnt __read_mostly = 10;
  68. static int nv_rtt_min_cnt __read_mostly = 2;
  69. module_param(nv_pad, int, 0644);
  70. MODULE_PARM_DESC(nv_pad, "max queued packets allowed in network");
  71. module_param(nv_reset_period, int, 0644);
  72. MODULE_PARM_DESC(nv_reset_period, "nv_min_rtt reset period (secs)");
  73. module_param(nv_min_cwnd, int, 0644);
  74. MODULE_PARM_DESC(nv_min_cwnd, "NV will not decrease cwnd below this value"
  75. " without losses");
  76. /* TCP NV Parameters */
  77. struct tcpnv {
  78. unsigned long nv_min_rtt_reset_jiffies; /* when to switch to
  79. * nv_min_rtt_new */
  80. s8 cwnd_growth_factor; /* Current cwnd growth factor,
  81. * < 0 => less than 1 packet/RTT */
  82. u8 available8;
  83. u16 available16;
  84. u8 nv_allow_cwnd_growth:1, /* whether cwnd can grow */
  85. nv_reset:1, /* whether to reset values */
  86. nv_catchup:1; /* whether we are growing because
  87. * of temporary cwnd decrease */
  88. u8 nv_eval_call_cnt; /* call count since last eval */
  89. u8 nv_min_cwnd; /* nv won't make a ca decision if cwnd is
  90. * smaller than this. It may grow to handle
  91. * TSO, LRO and interrupt coalescence because
  92. * with these a small cwnd cannot saturate
  93. * the link. Note that this is different from
  94. * the file local nv_min_cwnd */
  95. u8 nv_rtt_cnt; /* RTTs without making ca decision */;
  96. u32 nv_last_rtt; /* last rtt */
  97. u32 nv_min_rtt; /* active min rtt. Used to determine slope */
  98. u32 nv_min_rtt_new; /* min rtt for future use */
  99. u32 nv_rtt_max_rate; /* max rate seen during current RTT */
  100. u32 nv_rtt_start_seq; /* current RTT ends when packet arrives
  101. * acking beyond nv_rtt_start_seq */
  102. u32 nv_last_snd_una; /* Previous value of tp->snd_una. It is
  103. * used to determine bytes acked since last
  104. * call to bictcp_acked */
  105. u32 nv_no_cong_cnt; /* Consecutive no congestion decisions */
  106. };
  107. #define NV_INIT_RTT U32_MAX
  108. #define NV_MIN_CWND 4
  109. #define NV_MIN_CWND_GROW 2
  110. #define NV_TSO_CWND_BOUND 80
  111. static inline void tcpnv_reset(struct tcpnv *ca, struct sock *sk)
  112. {
  113. struct tcp_sock *tp = tcp_sk(sk);
  114. ca->nv_reset = 0;
  115. ca->nv_no_cong_cnt = 0;
  116. ca->nv_rtt_cnt = 0;
  117. ca->nv_last_rtt = 0;
  118. ca->nv_rtt_max_rate = 0;
  119. ca->nv_rtt_start_seq = tp->snd_una;
  120. ca->nv_eval_call_cnt = 0;
  121. ca->nv_last_snd_una = tp->snd_una;
  122. }
  123. static void tcpnv_init(struct sock *sk)
  124. {
  125. struct tcpnv *ca = inet_csk_ca(sk);
  126. tcpnv_reset(ca, sk);
  127. ca->nv_allow_cwnd_growth = 1;
  128. ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ;
  129. ca->nv_min_rtt = NV_INIT_RTT;
  130. ca->nv_min_rtt_new = NV_INIT_RTT;
  131. ca->nv_min_cwnd = NV_MIN_CWND;
  132. ca->nv_catchup = 0;
  133. ca->cwnd_growth_factor = 0;
  134. }
  135. static void tcpnv_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  136. {
  137. struct tcp_sock *tp = tcp_sk(sk);
  138. struct tcpnv *ca = inet_csk_ca(sk);
  139. u32 cnt;
  140. if (!tcp_is_cwnd_limited(sk))
  141. return;
  142. /* Only grow cwnd if NV has not detected congestion */
  143. if (!ca->nv_allow_cwnd_growth)
  144. return;
  145. if (tcp_in_slow_start(tp)) {
  146. acked = tcp_slow_start(tp, acked);
  147. if (!acked)
  148. return;
  149. }
  150. if (ca->cwnd_growth_factor < 0) {
  151. cnt = tp->snd_cwnd << -ca->cwnd_growth_factor;
  152. tcp_cong_avoid_ai(tp, cnt, acked);
  153. } else {
  154. cnt = max(4U, tp->snd_cwnd >> ca->cwnd_growth_factor);
  155. tcp_cong_avoid_ai(tp, cnt, acked);
  156. }
  157. }
  158. static u32 tcpnv_recalc_ssthresh(struct sock *sk)
  159. {
  160. const struct tcp_sock *tp = tcp_sk(sk);
  161. return max((tp->snd_cwnd * nv_loss_dec_factor) >> 10, 2U);
  162. }
  163. static void tcpnv_state(struct sock *sk, u8 new_state)
  164. {
  165. struct tcpnv *ca = inet_csk_ca(sk);
  166. if (new_state == TCP_CA_Open && ca->nv_reset) {
  167. tcpnv_reset(ca, sk);
  168. } else if (new_state == TCP_CA_Loss || new_state == TCP_CA_CWR ||
  169. new_state == TCP_CA_Recovery) {
  170. ca->nv_reset = 1;
  171. ca->nv_allow_cwnd_growth = 0;
  172. if (new_state == TCP_CA_Loss) {
  173. /* Reset cwnd growth factor to Reno value */
  174. if (ca->cwnd_growth_factor > 0)
  175. ca->cwnd_growth_factor = 0;
  176. /* Decrease growth rate if allowed */
  177. if (nv_cwnd_growth_rate_neg > 0 &&
  178. ca->cwnd_growth_factor > -8)
  179. ca->cwnd_growth_factor--;
  180. }
  181. }
  182. }
  183. /* Do congestion avoidance calculations for TCP-NV
  184. */
  185. static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
  186. {
  187. const struct inet_connection_sock *icsk = inet_csk(sk);
  188. struct tcp_sock *tp = tcp_sk(sk);
  189. struct tcpnv *ca = inet_csk_ca(sk);
  190. unsigned long now = jiffies;
  191. s64 rate64 = 0;
  192. u32 rate, max_win, cwnd_by_slope;
  193. u32 avg_rtt;
  194. u32 bytes_acked = 0;
  195. /* Some calls are for duplicates without timetamps */
  196. if (sample->rtt_us < 0)
  197. return;
  198. /* If not in TCP_CA_Open or TCP_CA_Disorder states, skip. */
  199. if (icsk->icsk_ca_state != TCP_CA_Open &&
  200. icsk->icsk_ca_state != TCP_CA_Disorder)
  201. return;
  202. /* Stop cwnd growth if we were in catch up mode */
  203. if (ca->nv_catchup && tp->snd_cwnd >= nv_min_cwnd) {
  204. ca->nv_catchup = 0;
  205. ca->nv_allow_cwnd_growth = 0;
  206. }
  207. bytes_acked = tp->snd_una - ca->nv_last_snd_una;
  208. ca->nv_last_snd_una = tp->snd_una;
  209. if (sample->in_flight == 0)
  210. return;
  211. /* Calculate moving average of RTT */
  212. if (nv_rtt_factor > 0) {
  213. if (ca->nv_last_rtt > 0) {
  214. avg_rtt = (((u64)sample->rtt_us) * nv_rtt_factor +
  215. ((u64)ca->nv_last_rtt)
  216. * (256 - nv_rtt_factor)) >> 8;
  217. } else {
  218. avg_rtt = sample->rtt_us;
  219. ca->nv_min_rtt = avg_rtt << 1;
  220. }
  221. ca->nv_last_rtt = avg_rtt;
  222. } else {
  223. avg_rtt = sample->rtt_us;
  224. }
  225. /* rate in 100's bits per second */
  226. rate64 = ((u64)sample->in_flight) * 8000000;
  227. rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
  228. /* Remember the maximum rate seen during this RTT
  229. * Note: It may be more than one RTT. This function should be
  230. * called at least nv_dec_eval_min_calls times.
  231. */
  232. if (ca->nv_rtt_max_rate < rate)
  233. ca->nv_rtt_max_rate = rate;
  234. /* We have valid information, increment counter */
  235. if (ca->nv_eval_call_cnt < 255)
  236. ca->nv_eval_call_cnt++;
  237. /* update min rtt if necessary */
  238. if (avg_rtt < ca->nv_min_rtt)
  239. ca->nv_min_rtt = avg_rtt;
  240. /* update future min_rtt if necessary */
  241. if (avg_rtt < ca->nv_min_rtt_new)
  242. ca->nv_min_rtt_new = avg_rtt;
  243. /* nv_min_rtt is updated with the minimum (possibley averaged) rtt
  244. * seen in the last sysctl_tcp_nv_reset_period seconds (i.e. a
  245. * warm reset). This new nv_min_rtt will be continued to be updated
  246. * and be used for another sysctl_tcp_nv_reset_period seconds,
  247. * when it will be updated again.
  248. * In practice we introduce some randomness, so the actual period used
  249. * is chosen randomly from the range:
  250. * [sysctl_tcp_nv_reset_period*3/4, sysctl_tcp_nv_reset_period*5/4)
  251. */
  252. if (time_after_eq(now, ca->nv_min_rtt_reset_jiffies)) {
  253. unsigned char rand;
  254. ca->nv_min_rtt = ca->nv_min_rtt_new;
  255. ca->nv_min_rtt_new = NV_INIT_RTT;
  256. get_random_bytes(&rand, 1);
  257. ca->nv_min_rtt_reset_jiffies =
  258. now + ((nv_reset_period * (384 + rand) * HZ) >> 9);
  259. /* Every so often we decrease ca->nv_min_cwnd in case previous
  260. * value is no longer accurate.
  261. */
  262. ca->nv_min_cwnd = max(ca->nv_min_cwnd / 2, NV_MIN_CWND);
  263. }
  264. /* Once per RTT check if we need to do congestion avoidance */
  265. if (before(ca->nv_rtt_start_seq, tp->snd_una)) {
  266. ca->nv_rtt_start_seq = tp->snd_nxt;
  267. if (ca->nv_rtt_cnt < 0xff)
  268. /* Increase counter for RTTs without CA decision */
  269. ca->nv_rtt_cnt++;
  270. /* If this function is only called once within an RTT
  271. * the cwnd is probably too small (in some cases due to
  272. * tso, lro or interrupt coalescence), so we increase
  273. * ca->nv_min_cwnd.
  274. */
  275. if (ca->nv_eval_call_cnt == 1 &&
  276. bytes_acked >= (ca->nv_min_cwnd - 1) * tp->mss_cache &&
  277. ca->nv_min_cwnd < (NV_TSO_CWND_BOUND + 1)) {
  278. ca->nv_min_cwnd = min(ca->nv_min_cwnd
  279. + NV_MIN_CWND_GROW,
  280. NV_TSO_CWND_BOUND + 1);
  281. ca->nv_rtt_start_seq = tp->snd_nxt +
  282. ca->nv_min_cwnd * tp->mss_cache;
  283. ca->nv_eval_call_cnt = 0;
  284. ca->nv_allow_cwnd_growth = 1;
  285. return;
  286. }
  287. /* Find the ideal cwnd for current rate from slope
  288. * slope = 80000.0 * mss / nv_min_rtt
  289. * cwnd_by_slope = nv_rtt_max_rate / slope
  290. */
  291. cwnd_by_slope = (u32)
  292. div64_u64(((u64)ca->nv_rtt_max_rate) * ca->nv_min_rtt,
  293. (u64)(80000 * tp->mss_cache));
  294. max_win = cwnd_by_slope + nv_pad;
  295. /* If cwnd > max_win, decrease cwnd
  296. * if cwnd < max_win, grow cwnd
  297. * else leave the same
  298. */
  299. if (tp->snd_cwnd > max_win) {
  300. /* there is congestion, check that it is ok
  301. * to make a CA decision
  302. * 1. We should have at least nv_dec_eval_min_calls
  303. * data points before making a CA decision
  304. * 2. We only make a congesion decision after
  305. * nv_rtt_min_cnt RTTs
  306. */
  307. if (ca->nv_rtt_cnt < nv_rtt_min_cnt) {
  308. return;
  309. } else if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
  310. if (ca->nv_eval_call_cnt <
  311. nv_ssthresh_eval_min_calls)
  312. return;
  313. /* otherwise we will decrease cwnd */
  314. } else if (ca->nv_eval_call_cnt <
  315. nv_dec_eval_min_calls) {
  316. if (ca->nv_allow_cwnd_growth &&
  317. ca->nv_rtt_cnt > nv_stop_rtt_cnt)
  318. ca->nv_allow_cwnd_growth = 0;
  319. return;
  320. }
  321. /* We have enough data to determine we are congested */
  322. ca->nv_allow_cwnd_growth = 0;
  323. tp->snd_ssthresh =
  324. (nv_ssthresh_factor * max_win) >> 3;
  325. if (tp->snd_cwnd - max_win > 2) {
  326. /* gap > 2, we do exponential cwnd decrease */
  327. int dec;
  328. dec = max(2U, ((tp->snd_cwnd - max_win) *
  329. nv_cong_dec_mult) >> 7);
  330. tp->snd_cwnd -= dec;
  331. } else if (nv_cong_dec_mult > 0) {
  332. tp->snd_cwnd = max_win;
  333. }
  334. if (ca->cwnd_growth_factor > 0)
  335. ca->cwnd_growth_factor = 0;
  336. ca->nv_no_cong_cnt = 0;
  337. } else if (tp->snd_cwnd <= max_win - nv_pad_buffer) {
  338. /* There is no congestion, grow cwnd if allowed*/
  339. if (ca->nv_eval_call_cnt < nv_inc_eval_min_calls)
  340. return;
  341. ca->nv_allow_cwnd_growth = 1;
  342. ca->nv_no_cong_cnt++;
  343. if (ca->cwnd_growth_factor < 0 &&
  344. nv_cwnd_growth_rate_neg > 0 &&
  345. ca->nv_no_cong_cnt > nv_cwnd_growth_rate_neg) {
  346. ca->cwnd_growth_factor++;
  347. ca->nv_no_cong_cnt = 0;
  348. } else if (ca->cwnd_growth_factor >= 0 &&
  349. nv_cwnd_growth_rate_pos > 0 &&
  350. ca->nv_no_cong_cnt >
  351. nv_cwnd_growth_rate_pos) {
  352. ca->cwnd_growth_factor++;
  353. ca->nv_no_cong_cnt = 0;
  354. }
  355. } else {
  356. /* cwnd is in-between, so do nothing */
  357. return;
  358. }
  359. /* update state */
  360. ca->nv_eval_call_cnt = 0;
  361. ca->nv_rtt_cnt = 0;
  362. ca->nv_rtt_max_rate = 0;
  363. /* Don't want to make cwnd < nv_min_cwnd
  364. * (it wasn't before, if it is now is because nv
  365. * decreased it).
  366. */
  367. if (tp->snd_cwnd < nv_min_cwnd)
  368. tp->snd_cwnd = nv_min_cwnd;
  369. }
  370. }
  371. /* Extract info for Tcp socket info provided via netlink */
  372. static size_t tcpnv_get_info(struct sock *sk, u32 ext, int *attr,
  373. union tcp_cc_info *info)
  374. {
  375. const struct tcpnv *ca = inet_csk_ca(sk);
  376. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  377. info->vegas.tcpv_enabled = 1;
  378. info->vegas.tcpv_rttcnt = ca->nv_rtt_cnt;
  379. info->vegas.tcpv_rtt = ca->nv_last_rtt;
  380. info->vegas.tcpv_minrtt = ca->nv_min_rtt;
  381. *attr = INET_DIAG_VEGASINFO;
  382. return sizeof(struct tcpvegas_info);
  383. }
  384. return 0;
  385. }
  386. static struct tcp_congestion_ops tcpnv __read_mostly = {
  387. .init = tcpnv_init,
  388. .ssthresh = tcpnv_recalc_ssthresh,
  389. .cong_avoid = tcpnv_cong_avoid,
  390. .set_state = tcpnv_state,
  391. .undo_cwnd = tcp_reno_undo_cwnd,
  392. .pkts_acked = tcpnv_acked,
  393. .get_info = tcpnv_get_info,
  394. .owner = THIS_MODULE,
  395. .name = "nv",
  396. };
  397. static int __init tcpnv_register(void)
  398. {
  399. BUILD_BUG_ON(sizeof(struct tcpnv) > ICSK_CA_PRIV_SIZE);
  400. return tcp_register_congestion_control(&tcpnv);
  401. }
  402. static void __exit tcpnv_unregister(void)
  403. {
  404. tcp_unregister_congestion_control(&tcpnv);
  405. }
  406. module_init(tcpnv_register);
  407. module_exit(tcpnv_unregister);
  408. MODULE_AUTHOR("Lawrence Brakmo");
  409. MODULE_LICENSE("GPL");
  410. MODULE_DESCRIPTION("TCP NV");
  411. MODULE_VERSION("1.0");