tcp_nv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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);
  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. s64 rate64 = 0;
  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) * 8000000;
  258. rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
  259. /* Remember the maximum rate seen during this RTT
  260. * Note: It may be more than one RTT. This function should be
  261. * called at least nv_dec_eval_min_calls times.
  262. */
  263. if (ca->nv_rtt_max_rate < rate)
  264. ca->nv_rtt_max_rate = rate;
  265. /* We have valid information, increment counter */
  266. if (ca->nv_eval_call_cnt < 255)
  267. ca->nv_eval_call_cnt++;
  268. /* Apply bounds to rtt. Only used to update min_rtt */
  269. avg_rtt = nv_get_bounded_rtt(ca, avg_rtt);
  270. /* update min rtt if necessary */
  271. if (avg_rtt < ca->nv_min_rtt)
  272. ca->nv_min_rtt = avg_rtt;
  273. /* update future min_rtt if necessary */
  274. if (avg_rtt < ca->nv_min_rtt_new)
  275. ca->nv_min_rtt_new = avg_rtt;
  276. /* nv_min_rtt is updated with the minimum (possibley averaged) rtt
  277. * seen in the last sysctl_tcp_nv_reset_period seconds (i.e. a
  278. * warm reset). This new nv_min_rtt will be continued to be updated
  279. * and be used for another sysctl_tcp_nv_reset_period seconds,
  280. * when it will be updated again.
  281. * In practice we introduce some randomness, so the actual period used
  282. * is chosen randomly from the range:
  283. * [sysctl_tcp_nv_reset_period*3/4, sysctl_tcp_nv_reset_period*5/4)
  284. */
  285. if (time_after_eq(now, ca->nv_min_rtt_reset_jiffies)) {
  286. unsigned char rand;
  287. ca->nv_min_rtt = ca->nv_min_rtt_new;
  288. ca->nv_min_rtt_new = NV_INIT_RTT;
  289. get_random_bytes(&rand, 1);
  290. ca->nv_min_rtt_reset_jiffies =
  291. now + ((nv_reset_period * (384 + rand) * HZ) >> 9);
  292. /* Every so often we decrease ca->nv_min_cwnd in case previous
  293. * value is no longer accurate.
  294. */
  295. ca->nv_min_cwnd = max(ca->nv_min_cwnd / 2, NV_MIN_CWND);
  296. }
  297. /* Once per RTT check if we need to do congestion avoidance */
  298. if (before(ca->nv_rtt_start_seq, tp->snd_una)) {
  299. ca->nv_rtt_start_seq = tp->snd_nxt;
  300. if (ca->nv_rtt_cnt < 0xff)
  301. /* Increase counter for RTTs without CA decision */
  302. ca->nv_rtt_cnt++;
  303. /* If this function is only called once within an RTT
  304. * the cwnd is probably too small (in some cases due to
  305. * tso, lro or interrupt coalescence), so we increase
  306. * ca->nv_min_cwnd.
  307. */
  308. if (ca->nv_eval_call_cnt == 1 &&
  309. bytes_acked >= (ca->nv_min_cwnd - 1) * tp->mss_cache &&
  310. ca->nv_min_cwnd < (NV_TSO_CWND_BOUND + 1)) {
  311. ca->nv_min_cwnd = min(ca->nv_min_cwnd
  312. + NV_MIN_CWND_GROW,
  313. NV_TSO_CWND_BOUND + 1);
  314. ca->nv_rtt_start_seq = tp->snd_nxt +
  315. ca->nv_min_cwnd * tp->mss_cache;
  316. ca->nv_eval_call_cnt = 0;
  317. ca->nv_allow_cwnd_growth = 1;
  318. return;
  319. }
  320. /* Find the ideal cwnd for current rate from slope
  321. * slope = 80000.0 * mss / nv_min_rtt
  322. * cwnd_by_slope = nv_rtt_max_rate / slope
  323. */
  324. cwnd_by_slope = (u32)
  325. div64_u64(((u64)ca->nv_rtt_max_rate) * ca->nv_min_rtt,
  326. (u64)(80000 * tp->mss_cache));
  327. max_win = cwnd_by_slope + nv_pad;
  328. /* If cwnd > max_win, decrease cwnd
  329. * if cwnd < max_win, grow cwnd
  330. * else leave the same
  331. */
  332. if (tp->snd_cwnd > max_win) {
  333. /* there is congestion, check that it is ok
  334. * to make a CA decision
  335. * 1. We should have at least nv_dec_eval_min_calls
  336. * data points before making a CA decision
  337. * 2. We only make a congesion decision after
  338. * nv_rtt_min_cnt RTTs
  339. */
  340. if (ca->nv_rtt_cnt < nv_rtt_min_cnt) {
  341. return;
  342. } else if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
  343. if (ca->nv_eval_call_cnt <
  344. nv_ssthresh_eval_min_calls)
  345. return;
  346. /* otherwise we will decrease cwnd */
  347. } else if (ca->nv_eval_call_cnt <
  348. nv_dec_eval_min_calls) {
  349. if (ca->nv_allow_cwnd_growth &&
  350. ca->nv_rtt_cnt > nv_stop_rtt_cnt)
  351. ca->nv_allow_cwnd_growth = 0;
  352. return;
  353. }
  354. /* We have enough data to determine we are congested */
  355. ca->nv_allow_cwnd_growth = 0;
  356. tp->snd_ssthresh =
  357. (nv_ssthresh_factor * max_win) >> 3;
  358. if (tp->snd_cwnd - max_win > 2) {
  359. /* gap > 2, we do exponential cwnd decrease */
  360. int dec;
  361. dec = max(2U, ((tp->snd_cwnd - max_win) *
  362. nv_cong_dec_mult) >> 7);
  363. tp->snd_cwnd -= dec;
  364. } else if (nv_cong_dec_mult > 0) {
  365. tp->snd_cwnd = max_win;
  366. }
  367. if (ca->cwnd_growth_factor > 0)
  368. ca->cwnd_growth_factor = 0;
  369. ca->nv_no_cong_cnt = 0;
  370. } else if (tp->snd_cwnd <= max_win - nv_pad_buffer) {
  371. /* There is no congestion, grow cwnd if allowed*/
  372. if (ca->nv_eval_call_cnt < nv_inc_eval_min_calls)
  373. return;
  374. ca->nv_allow_cwnd_growth = 1;
  375. ca->nv_no_cong_cnt++;
  376. if (ca->cwnd_growth_factor < 0 &&
  377. nv_cwnd_growth_rate_neg > 0 &&
  378. ca->nv_no_cong_cnt > nv_cwnd_growth_rate_neg) {
  379. ca->cwnd_growth_factor++;
  380. ca->nv_no_cong_cnt = 0;
  381. } else if (ca->cwnd_growth_factor >= 0 &&
  382. nv_cwnd_growth_rate_pos > 0 &&
  383. ca->nv_no_cong_cnt >
  384. nv_cwnd_growth_rate_pos) {
  385. ca->cwnd_growth_factor++;
  386. ca->nv_no_cong_cnt = 0;
  387. }
  388. } else {
  389. /* cwnd is in-between, so do nothing */
  390. return;
  391. }
  392. /* update state */
  393. ca->nv_eval_call_cnt = 0;
  394. ca->nv_rtt_cnt = 0;
  395. ca->nv_rtt_max_rate = 0;
  396. /* Don't want to make cwnd < nv_min_cwnd
  397. * (it wasn't before, if it is now is because nv
  398. * decreased it).
  399. */
  400. if (tp->snd_cwnd < nv_min_cwnd)
  401. tp->snd_cwnd = nv_min_cwnd;
  402. }
  403. }
  404. /* Extract info for Tcp socket info provided via netlink */
  405. static size_t tcpnv_get_info(struct sock *sk, u32 ext, int *attr,
  406. union tcp_cc_info *info)
  407. {
  408. const struct tcpnv *ca = inet_csk_ca(sk);
  409. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  410. info->vegas.tcpv_enabled = 1;
  411. info->vegas.tcpv_rttcnt = ca->nv_rtt_cnt;
  412. info->vegas.tcpv_rtt = ca->nv_last_rtt;
  413. info->vegas.tcpv_minrtt = ca->nv_min_rtt;
  414. *attr = INET_DIAG_VEGASINFO;
  415. return sizeof(struct tcpvegas_info);
  416. }
  417. return 0;
  418. }
  419. static struct tcp_congestion_ops tcpnv __read_mostly = {
  420. .init = tcpnv_init,
  421. .ssthresh = tcpnv_recalc_ssthresh,
  422. .cong_avoid = tcpnv_cong_avoid,
  423. .set_state = tcpnv_state,
  424. .undo_cwnd = tcp_reno_undo_cwnd,
  425. .pkts_acked = tcpnv_acked,
  426. .get_info = tcpnv_get_info,
  427. .owner = THIS_MODULE,
  428. .name = "nv",
  429. };
  430. static int __init tcpnv_register(void)
  431. {
  432. BUILD_BUG_ON(sizeof(struct tcpnv) > ICSK_CA_PRIV_SIZE);
  433. return tcp_register_congestion_control(&tcpnv);
  434. }
  435. static void __exit tcpnv_unregister(void)
  436. {
  437. tcp_unregister_congestion_control(&tcpnv);
  438. }
  439. module_init(tcpnv_register);
  440. module_exit(tcpnv_unregister);
  441. MODULE_AUTHOR("Lawrence Brakmo");
  442. MODULE_LICENSE("GPL");
  443. MODULE_DESCRIPTION("TCP NV");
  444. MODULE_VERSION("1.0");