tcp_nv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. u32 loss_cwnd; /* cwnd at last loss */
  85. u8 nv_allow_cwnd_growth:1, /* whether cwnd can grow */
  86. nv_reset:1, /* whether to reset values */
  87. nv_catchup:1; /* whether we are growing because
  88. * of temporary cwnd decrease */
  89. u8 nv_eval_call_cnt; /* call count since last eval */
  90. u8 nv_min_cwnd; /* nv won't make a ca decision if cwnd is
  91. * smaller than this. It may grow to handle
  92. * TSO, LRO and interrupt coalescence because
  93. * with these a small cwnd cannot saturate
  94. * the link. Note that this is different from
  95. * the file local nv_min_cwnd */
  96. u8 nv_rtt_cnt; /* RTTs without making ca decision */;
  97. u32 nv_last_rtt; /* last rtt */
  98. u32 nv_min_rtt; /* active min rtt. Used to determine slope */
  99. u32 nv_min_rtt_new; /* min rtt for future use */
  100. u32 nv_rtt_max_rate; /* max rate seen during current RTT */
  101. u32 nv_rtt_start_seq; /* current RTT ends when packet arrives
  102. * acking beyond nv_rtt_start_seq */
  103. u32 nv_last_snd_una; /* Previous value of tp->snd_una. It is
  104. * used to determine bytes acked since last
  105. * call to bictcp_acked */
  106. u32 nv_no_cong_cnt; /* Consecutive no congestion decisions */
  107. };
  108. #define NV_INIT_RTT U32_MAX
  109. #define NV_MIN_CWND 4
  110. #define NV_MIN_CWND_GROW 2
  111. #define NV_TSO_CWND_BOUND 80
  112. static inline void tcpnv_reset(struct tcpnv *ca, struct sock *sk)
  113. {
  114. struct tcp_sock *tp = tcp_sk(sk);
  115. ca->nv_reset = 0;
  116. ca->loss_cwnd = 0;
  117. ca->nv_no_cong_cnt = 0;
  118. ca->nv_rtt_cnt = 0;
  119. ca->nv_last_rtt = 0;
  120. ca->nv_rtt_max_rate = 0;
  121. ca->nv_rtt_start_seq = tp->snd_una;
  122. ca->nv_eval_call_cnt = 0;
  123. ca->nv_last_snd_una = tp->snd_una;
  124. }
  125. static void tcpnv_init(struct sock *sk)
  126. {
  127. struct tcpnv *ca = inet_csk_ca(sk);
  128. tcpnv_reset(ca, sk);
  129. ca->nv_allow_cwnd_growth = 1;
  130. ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ;
  131. ca->nv_min_rtt = NV_INIT_RTT;
  132. ca->nv_min_rtt_new = NV_INIT_RTT;
  133. ca->nv_min_cwnd = NV_MIN_CWND;
  134. ca->nv_catchup = 0;
  135. ca->cwnd_growth_factor = 0;
  136. }
  137. static void tcpnv_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  138. {
  139. struct tcp_sock *tp = tcp_sk(sk);
  140. struct tcpnv *ca = inet_csk_ca(sk);
  141. u32 cnt;
  142. if (!tcp_is_cwnd_limited(sk))
  143. return;
  144. /* Only grow cwnd if NV has not detected congestion */
  145. if (!ca->nv_allow_cwnd_growth)
  146. return;
  147. if (tcp_in_slow_start(tp)) {
  148. acked = tcp_slow_start(tp, acked);
  149. if (!acked)
  150. return;
  151. }
  152. if (ca->cwnd_growth_factor < 0) {
  153. cnt = tp->snd_cwnd << -ca->cwnd_growth_factor;
  154. tcp_cong_avoid_ai(tp, cnt, acked);
  155. } else {
  156. cnt = max(4U, tp->snd_cwnd >> ca->cwnd_growth_factor);
  157. tcp_cong_avoid_ai(tp, cnt, acked);
  158. }
  159. }
  160. static u32 tcpnv_recalc_ssthresh(struct sock *sk)
  161. {
  162. const struct tcp_sock *tp = tcp_sk(sk);
  163. struct tcpnv *ca = inet_csk_ca(sk);
  164. ca->loss_cwnd = tp->snd_cwnd;
  165. return max((tp->snd_cwnd * nv_loss_dec_factor) >> 10, 2U);
  166. }
  167. static u32 tcpnv_undo_cwnd(struct sock *sk)
  168. {
  169. struct tcpnv *ca = inet_csk_ca(sk);
  170. return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
  171. }
  172. static void tcpnv_state(struct sock *sk, u8 new_state)
  173. {
  174. struct tcpnv *ca = inet_csk_ca(sk);
  175. if (new_state == TCP_CA_Open && ca->nv_reset) {
  176. tcpnv_reset(ca, sk);
  177. } else if (new_state == TCP_CA_Loss || new_state == TCP_CA_CWR ||
  178. new_state == TCP_CA_Recovery) {
  179. ca->nv_reset = 1;
  180. ca->nv_allow_cwnd_growth = 0;
  181. if (new_state == TCP_CA_Loss) {
  182. /* Reset cwnd growth factor to Reno value */
  183. if (ca->cwnd_growth_factor > 0)
  184. ca->cwnd_growth_factor = 0;
  185. /* Decrease growth rate if allowed */
  186. if (nv_cwnd_growth_rate_neg > 0 &&
  187. ca->cwnd_growth_factor > -8)
  188. ca->cwnd_growth_factor--;
  189. }
  190. }
  191. }
  192. /* Do congestion avoidance calculations for TCP-NV
  193. */
  194. static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
  195. {
  196. const struct inet_connection_sock *icsk = inet_csk(sk);
  197. struct tcp_sock *tp = tcp_sk(sk);
  198. struct tcpnv *ca = inet_csk_ca(sk);
  199. unsigned long now = jiffies;
  200. s64 rate64 = 0;
  201. u32 rate, max_win, cwnd_by_slope;
  202. u32 avg_rtt;
  203. u32 bytes_acked = 0;
  204. /* Some calls are for duplicates without timetamps */
  205. if (sample->rtt_us < 0)
  206. return;
  207. /* If not in TCP_CA_Open or TCP_CA_Disorder states, skip. */
  208. if (icsk->icsk_ca_state != TCP_CA_Open &&
  209. icsk->icsk_ca_state != TCP_CA_Disorder)
  210. return;
  211. /* Stop cwnd growth if we were in catch up mode */
  212. if (ca->nv_catchup && tp->snd_cwnd >= nv_min_cwnd) {
  213. ca->nv_catchup = 0;
  214. ca->nv_allow_cwnd_growth = 0;
  215. }
  216. bytes_acked = tp->snd_una - ca->nv_last_snd_una;
  217. ca->nv_last_snd_una = tp->snd_una;
  218. if (sample->in_flight == 0)
  219. return;
  220. /* Calculate moving average of RTT */
  221. if (nv_rtt_factor > 0) {
  222. if (ca->nv_last_rtt > 0) {
  223. avg_rtt = (((u64)sample->rtt_us) * nv_rtt_factor +
  224. ((u64)ca->nv_last_rtt)
  225. * (256 - nv_rtt_factor)) >> 8;
  226. } else {
  227. avg_rtt = sample->rtt_us;
  228. ca->nv_min_rtt = avg_rtt << 1;
  229. }
  230. ca->nv_last_rtt = avg_rtt;
  231. } else {
  232. avg_rtt = sample->rtt_us;
  233. }
  234. /* rate in 100's bits per second */
  235. rate64 = ((u64)sample->in_flight) * 8000000;
  236. rate = (u32)div64_u64(rate64, (u64)(avg_rtt ?: 1) * 100);
  237. /* Remember the maximum rate seen during this RTT
  238. * Note: It may be more than one RTT. This function should be
  239. * called at least nv_dec_eval_min_calls times.
  240. */
  241. if (ca->nv_rtt_max_rate < rate)
  242. ca->nv_rtt_max_rate = rate;
  243. /* We have valid information, increment counter */
  244. if (ca->nv_eval_call_cnt < 255)
  245. ca->nv_eval_call_cnt++;
  246. /* update min rtt if necessary */
  247. if (avg_rtt < ca->nv_min_rtt)
  248. ca->nv_min_rtt = avg_rtt;
  249. /* update future min_rtt if necessary */
  250. if (avg_rtt < ca->nv_min_rtt_new)
  251. ca->nv_min_rtt_new = avg_rtt;
  252. /* nv_min_rtt is updated with the minimum (possibley averaged) rtt
  253. * seen in the last sysctl_tcp_nv_reset_period seconds (i.e. a
  254. * warm reset). This new nv_min_rtt will be continued to be updated
  255. * and be used for another sysctl_tcp_nv_reset_period seconds,
  256. * when it will be updated again.
  257. * In practice we introduce some randomness, so the actual period used
  258. * is chosen randomly from the range:
  259. * [sysctl_tcp_nv_reset_period*3/4, sysctl_tcp_nv_reset_period*5/4)
  260. */
  261. if (time_after_eq(now, ca->nv_min_rtt_reset_jiffies)) {
  262. unsigned char rand;
  263. ca->nv_min_rtt = ca->nv_min_rtt_new;
  264. ca->nv_min_rtt_new = NV_INIT_RTT;
  265. get_random_bytes(&rand, 1);
  266. ca->nv_min_rtt_reset_jiffies =
  267. now + ((nv_reset_period * (384 + rand) * HZ) >> 9);
  268. /* Every so often we decrease ca->nv_min_cwnd in case previous
  269. * value is no longer accurate.
  270. */
  271. ca->nv_min_cwnd = max(ca->nv_min_cwnd / 2, NV_MIN_CWND);
  272. }
  273. /* Once per RTT check if we need to do congestion avoidance */
  274. if (before(ca->nv_rtt_start_seq, tp->snd_una)) {
  275. ca->nv_rtt_start_seq = tp->snd_nxt;
  276. if (ca->nv_rtt_cnt < 0xff)
  277. /* Increase counter for RTTs without CA decision */
  278. ca->nv_rtt_cnt++;
  279. /* If this function is only called once within an RTT
  280. * the cwnd is probably too small (in some cases due to
  281. * tso, lro or interrupt coalescence), so we increase
  282. * ca->nv_min_cwnd.
  283. */
  284. if (ca->nv_eval_call_cnt == 1 &&
  285. bytes_acked >= (ca->nv_min_cwnd - 1) * tp->mss_cache &&
  286. ca->nv_min_cwnd < (NV_TSO_CWND_BOUND + 1)) {
  287. ca->nv_min_cwnd = min(ca->nv_min_cwnd
  288. + NV_MIN_CWND_GROW,
  289. NV_TSO_CWND_BOUND + 1);
  290. ca->nv_rtt_start_seq = tp->snd_nxt +
  291. ca->nv_min_cwnd * tp->mss_cache;
  292. ca->nv_eval_call_cnt = 0;
  293. ca->nv_allow_cwnd_growth = 1;
  294. return;
  295. }
  296. /* Find the ideal cwnd for current rate from slope
  297. * slope = 80000.0 * mss / nv_min_rtt
  298. * cwnd_by_slope = nv_rtt_max_rate / slope
  299. */
  300. cwnd_by_slope = (u32)
  301. div64_u64(((u64)ca->nv_rtt_max_rate) * ca->nv_min_rtt,
  302. 80000ULL * tp->mss_cache);
  303. max_win = cwnd_by_slope + nv_pad;
  304. /* If cwnd > max_win, decrease cwnd
  305. * if cwnd < max_win, grow cwnd
  306. * else leave the same
  307. */
  308. if (tp->snd_cwnd > max_win) {
  309. /* there is congestion, check that it is ok
  310. * to make a CA decision
  311. * 1. We should have at least nv_dec_eval_min_calls
  312. * data points before making a CA decision
  313. * 2. We only make a congesion decision after
  314. * nv_rtt_min_cnt RTTs
  315. */
  316. if (ca->nv_rtt_cnt < nv_rtt_min_cnt) {
  317. return;
  318. } else if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
  319. if (ca->nv_eval_call_cnt <
  320. nv_ssthresh_eval_min_calls)
  321. return;
  322. /* otherwise we will decrease cwnd */
  323. } else if (ca->nv_eval_call_cnt <
  324. nv_dec_eval_min_calls) {
  325. if (ca->nv_allow_cwnd_growth &&
  326. ca->nv_rtt_cnt > nv_stop_rtt_cnt)
  327. ca->nv_allow_cwnd_growth = 0;
  328. return;
  329. }
  330. /* We have enough data to determine we are congested */
  331. ca->nv_allow_cwnd_growth = 0;
  332. tp->snd_ssthresh =
  333. (nv_ssthresh_factor * max_win) >> 3;
  334. if (tp->snd_cwnd - max_win > 2) {
  335. /* gap > 2, we do exponential cwnd decrease */
  336. int dec;
  337. dec = max(2U, ((tp->snd_cwnd - max_win) *
  338. nv_cong_dec_mult) >> 7);
  339. tp->snd_cwnd -= dec;
  340. } else if (nv_cong_dec_mult > 0) {
  341. tp->snd_cwnd = max_win;
  342. }
  343. if (ca->cwnd_growth_factor > 0)
  344. ca->cwnd_growth_factor = 0;
  345. ca->nv_no_cong_cnt = 0;
  346. } else if (tp->snd_cwnd <= max_win - nv_pad_buffer) {
  347. /* There is no congestion, grow cwnd if allowed*/
  348. if (ca->nv_eval_call_cnt < nv_inc_eval_min_calls)
  349. return;
  350. ca->nv_allow_cwnd_growth = 1;
  351. ca->nv_no_cong_cnt++;
  352. if (ca->cwnd_growth_factor < 0 &&
  353. nv_cwnd_growth_rate_neg > 0 &&
  354. ca->nv_no_cong_cnt > nv_cwnd_growth_rate_neg) {
  355. ca->cwnd_growth_factor++;
  356. ca->nv_no_cong_cnt = 0;
  357. } else if (ca->cwnd_growth_factor >= 0 &&
  358. nv_cwnd_growth_rate_pos > 0 &&
  359. ca->nv_no_cong_cnt >
  360. nv_cwnd_growth_rate_pos) {
  361. ca->cwnd_growth_factor++;
  362. ca->nv_no_cong_cnt = 0;
  363. }
  364. } else {
  365. /* cwnd is in-between, so do nothing */
  366. return;
  367. }
  368. /* update state */
  369. ca->nv_eval_call_cnt = 0;
  370. ca->nv_rtt_cnt = 0;
  371. ca->nv_rtt_max_rate = 0;
  372. /* Don't want to make cwnd < nv_min_cwnd
  373. * (it wasn't before, if it is now is because nv
  374. * decreased it).
  375. */
  376. if (tp->snd_cwnd < nv_min_cwnd)
  377. tp->snd_cwnd = nv_min_cwnd;
  378. }
  379. }
  380. /* Extract info for Tcp socket info provided via netlink */
  381. size_t tcpnv_get_info(struct sock *sk, u32 ext, int *attr,
  382. union tcp_cc_info *info)
  383. {
  384. const struct tcpnv *ca = inet_csk_ca(sk);
  385. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  386. info->vegas.tcpv_enabled = 1;
  387. info->vegas.tcpv_rttcnt = ca->nv_rtt_cnt;
  388. info->vegas.tcpv_rtt = ca->nv_last_rtt;
  389. info->vegas.tcpv_minrtt = ca->nv_min_rtt;
  390. *attr = INET_DIAG_VEGASINFO;
  391. return sizeof(struct tcpvegas_info);
  392. }
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_GPL(tcpnv_get_info);
  396. static struct tcp_congestion_ops tcpnv __read_mostly = {
  397. .init = tcpnv_init,
  398. .ssthresh = tcpnv_recalc_ssthresh,
  399. .cong_avoid = tcpnv_cong_avoid,
  400. .set_state = tcpnv_state,
  401. .undo_cwnd = tcpnv_undo_cwnd,
  402. .pkts_acked = tcpnv_acked,
  403. .get_info = tcpnv_get_info,
  404. .owner = THIS_MODULE,
  405. .name = "nv",
  406. };
  407. static int __init tcpnv_register(void)
  408. {
  409. BUILD_BUG_ON(sizeof(struct tcpnv) > ICSK_CA_PRIV_SIZE);
  410. return tcp_register_congestion_control(&tcpnv);
  411. }
  412. static void __exit tcpnv_unregister(void)
  413. {
  414. tcp_unregister_congestion_control(&tcpnv);
  415. }
  416. module_init(tcpnv_register);
  417. module_exit(tcpnv_unregister);
  418. MODULE_AUTHOR("Lawrence Brakmo");
  419. MODULE_LICENSE("GPL");
  420. MODULE_DESCRIPTION("TCP NV");
  421. MODULE_VERSION("1.0");