tcp_nv.c 16 KB

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