tcp_lp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP Low Priority (TCP-LP)
  4. *
  5. * TCP Low Priority is a distributed algorithm whose goal is to utilize only
  6. * the excess network bandwidth as compared to the ``fair share`` of
  7. * bandwidth as targeted by TCP.
  8. *
  9. * As of 2.6.13, Linux supports pluggable congestion control algorithms.
  10. * Due to the limitation of the API, we take the following changes from
  11. * the original TCP-LP implementation:
  12. * o We use newReno in most core CA handling. Only add some checking
  13. * within cong_avoid.
  14. * o Error correcting in remote HZ, therefore remote HZ will be keeped
  15. * on checking and updating.
  16. * o Handling calculation of One-Way-Delay (OWD) within rtt_sample, since
  17. * OWD have a similar meaning as RTT. Also correct the buggy formular.
  18. * o Handle reaction for Early Congestion Indication (ECI) within
  19. * pkts_acked, as mentioned within pseudo code.
  20. * o OWD is handled in relative format, where local time stamp will in
  21. * tcp_time_stamp format.
  22. *
  23. * Original Author:
  24. * Aleksandar Kuzmanovic <[email protected]>
  25. * Available from:
  26. * http://www.ece.rice.edu/~akuzma/Doc/akuzma/TCP-LP.pdf
  27. * Original implementation for 2.4.19:
  28. * http://www-ece.rice.edu/networks/TCP-LP/
  29. *
  30. * 2.6.x module Authors:
  31. * Wong Hoi Sing, Edison <[email protected]>
  32. * Hung Hing Lun, Mike <[email protected]>
  33. * SourceForge project page:
  34. * http://tcp-lp-mod.sourceforge.net/
  35. */
  36. #include <linux/module.h>
  37. #include <net/tcp.h>
  38. /* resolution of owd */
  39. #define LP_RESOL TCP_TS_HZ
  40. /**
  41. * enum tcp_lp_state
  42. * @LP_VALID_RHZ: is remote HZ valid?
  43. * @LP_VALID_OWD: is OWD valid?
  44. * @LP_WITHIN_THR: are we within threshold?
  45. * @LP_WITHIN_INF: are we within inference?
  46. *
  47. * TCP-LP's state flags.
  48. * We create this set of state flag mainly for debugging.
  49. */
  50. enum tcp_lp_state {
  51. LP_VALID_RHZ = (1 << 0),
  52. LP_VALID_OWD = (1 << 1),
  53. LP_WITHIN_THR = (1 << 3),
  54. LP_WITHIN_INF = (1 << 4),
  55. };
  56. /**
  57. * struct lp
  58. * @flag: TCP-LP state flag
  59. * @sowd: smoothed OWD << 3
  60. * @owd_min: min OWD
  61. * @owd_max: max OWD
  62. * @owd_max_rsv: reserved max owd
  63. * @remote_hz: estimated remote HZ
  64. * @remote_ref_time: remote reference time
  65. * @local_ref_time: local reference time
  66. * @last_drop: time for last active drop
  67. * @inference: current inference
  68. *
  69. * TCP-LP's private struct.
  70. * We get the idea from original TCP-LP implementation where only left those we
  71. * found are really useful.
  72. */
  73. struct lp {
  74. u32 flag;
  75. u32 sowd;
  76. u32 owd_min;
  77. u32 owd_max;
  78. u32 owd_max_rsv;
  79. u32 remote_hz;
  80. u32 remote_ref_time;
  81. u32 local_ref_time;
  82. u32 last_drop;
  83. u32 inference;
  84. };
  85. /**
  86. * tcp_lp_init
  87. * @sk: socket to initialize congestion control algorithm for
  88. *
  89. * Init all required variables.
  90. * Clone the handling from Vegas module implementation.
  91. */
  92. static void tcp_lp_init(struct sock *sk)
  93. {
  94. struct lp *lp = inet_csk_ca(sk);
  95. lp->flag = 0;
  96. lp->sowd = 0;
  97. lp->owd_min = 0xffffffff;
  98. lp->owd_max = 0;
  99. lp->owd_max_rsv = 0;
  100. lp->remote_hz = 0;
  101. lp->remote_ref_time = 0;
  102. lp->local_ref_time = 0;
  103. lp->last_drop = 0;
  104. lp->inference = 0;
  105. }
  106. /**
  107. * tcp_lp_cong_avoid
  108. * @sk: socket to avoid congesting
  109. *
  110. * Implementation of cong_avoid.
  111. * Will only call newReno CA when away from inference.
  112. * From TCP-LP's paper, this will be handled in additive increasement.
  113. */
  114. static void tcp_lp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  115. {
  116. struct lp *lp = inet_csk_ca(sk);
  117. if (!(lp->flag & LP_WITHIN_INF))
  118. tcp_reno_cong_avoid(sk, ack, acked);
  119. }
  120. /**
  121. * tcp_lp_remote_hz_estimator
  122. * @sk: socket which needs an estimate for the remote HZs
  123. *
  124. * Estimate remote HZ.
  125. * We keep on updating the estimated value, where original TCP-LP
  126. * implementation only guest it for once and use forever.
  127. */
  128. static u32 tcp_lp_remote_hz_estimator(struct sock *sk)
  129. {
  130. struct tcp_sock *tp = tcp_sk(sk);
  131. struct lp *lp = inet_csk_ca(sk);
  132. s64 rhz = lp->remote_hz << 6; /* remote HZ << 6 */
  133. s64 m = 0;
  134. /* not yet record reference time
  135. * go away!! record it before come back!! */
  136. if (lp->remote_ref_time == 0 || lp->local_ref_time == 0)
  137. goto out;
  138. /* we can't calc remote HZ with no different!! */
  139. if (tp->rx_opt.rcv_tsval == lp->remote_ref_time ||
  140. tp->rx_opt.rcv_tsecr == lp->local_ref_time)
  141. goto out;
  142. m = TCP_TS_HZ *
  143. (tp->rx_opt.rcv_tsval - lp->remote_ref_time) /
  144. (tp->rx_opt.rcv_tsecr - lp->local_ref_time);
  145. if (m < 0)
  146. m = -m;
  147. if (rhz > 0) {
  148. m -= rhz >> 6; /* m is now error in remote HZ est */
  149. rhz += m; /* 63/64 old + 1/64 new */
  150. } else
  151. rhz = m << 6;
  152. out:
  153. /* record time for successful remote HZ calc */
  154. if ((rhz >> 6) > 0)
  155. lp->flag |= LP_VALID_RHZ;
  156. else
  157. lp->flag &= ~LP_VALID_RHZ;
  158. /* record reference time stamp */
  159. lp->remote_ref_time = tp->rx_opt.rcv_tsval;
  160. lp->local_ref_time = tp->rx_opt.rcv_tsecr;
  161. return rhz >> 6;
  162. }
  163. /**
  164. * tcp_lp_owd_calculator
  165. * @sk: socket to calculate one way delay for
  166. *
  167. * Calculate one way delay (in relative format).
  168. * Original implement OWD as minus of remote time difference to local time
  169. * difference directly. As this time difference just simply equal to RTT, when
  170. * the network status is stable, remote RTT will equal to local RTT, and result
  171. * OWD into zero.
  172. * It seems to be a bug and so we fixed it.
  173. */
  174. static u32 tcp_lp_owd_calculator(struct sock *sk)
  175. {
  176. struct tcp_sock *tp = tcp_sk(sk);
  177. struct lp *lp = inet_csk_ca(sk);
  178. s64 owd = 0;
  179. lp->remote_hz = tcp_lp_remote_hz_estimator(sk);
  180. if (lp->flag & LP_VALID_RHZ) {
  181. owd =
  182. tp->rx_opt.rcv_tsval * (LP_RESOL / lp->remote_hz) -
  183. tp->rx_opt.rcv_tsecr * (LP_RESOL / TCP_TS_HZ);
  184. if (owd < 0)
  185. owd = -owd;
  186. }
  187. if (owd > 0)
  188. lp->flag |= LP_VALID_OWD;
  189. else
  190. lp->flag &= ~LP_VALID_OWD;
  191. return owd;
  192. }
  193. /**
  194. * tcp_lp_rtt_sample
  195. * @sk: socket to add a rtt sample to
  196. * @rtt: round trip time, which is ignored!
  197. *
  198. * Implementation or rtt_sample.
  199. * Will take the following action,
  200. * 1. calc OWD,
  201. * 2. record the min/max OWD,
  202. * 3. calc smoothed OWD (SOWD).
  203. * Most ideas come from the original TCP-LP implementation.
  204. */
  205. static void tcp_lp_rtt_sample(struct sock *sk, u32 rtt)
  206. {
  207. struct lp *lp = inet_csk_ca(sk);
  208. s64 mowd = tcp_lp_owd_calculator(sk);
  209. /* sorry that we don't have valid data */
  210. if (!(lp->flag & LP_VALID_RHZ) || !(lp->flag & LP_VALID_OWD))
  211. return;
  212. /* record the next min owd */
  213. if (mowd < lp->owd_min)
  214. lp->owd_min = mowd;
  215. /* always forget the max of the max
  216. * we just set owd_max as one below it */
  217. if (mowd > lp->owd_max) {
  218. if (mowd > lp->owd_max_rsv) {
  219. if (lp->owd_max_rsv == 0)
  220. lp->owd_max = mowd;
  221. else
  222. lp->owd_max = lp->owd_max_rsv;
  223. lp->owd_max_rsv = mowd;
  224. } else
  225. lp->owd_max = mowd;
  226. }
  227. /* calc for smoothed owd */
  228. if (lp->sowd != 0) {
  229. mowd -= lp->sowd >> 3; /* m is now error in owd est */
  230. lp->sowd += mowd; /* owd = 7/8 owd + 1/8 new */
  231. } else
  232. lp->sowd = mowd << 3; /* take the measured time be owd */
  233. }
  234. /**
  235. * tcp_lp_pkts_acked
  236. * @sk: socket requiring congestion avoidance calculations
  237. *
  238. * Implementation of pkts_acked.
  239. * Deal with active drop under Early Congestion Indication.
  240. * Only drop to half and 1 will be handle, because we hope to use back
  241. * newReno in increase case.
  242. * We work it out by following the idea from TCP-LP's paper directly
  243. */
  244. static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
  245. {
  246. struct tcp_sock *tp = tcp_sk(sk);
  247. struct lp *lp = inet_csk_ca(sk);
  248. u32 now = tcp_time_stamp(tp);
  249. u32 delta;
  250. if (sample->rtt_us > 0)
  251. tcp_lp_rtt_sample(sk, sample->rtt_us);
  252. /* calc inference */
  253. delta = now - tp->rx_opt.rcv_tsecr;
  254. if ((s32)delta > 0)
  255. lp->inference = 3 * delta;
  256. /* test if within inference */
  257. if (lp->last_drop && (now - lp->last_drop < lp->inference))
  258. lp->flag |= LP_WITHIN_INF;
  259. else
  260. lp->flag &= ~LP_WITHIN_INF;
  261. /* test if within threshold */
  262. if (lp->sowd >> 3 <
  263. lp->owd_min + 15 * (lp->owd_max - lp->owd_min) / 100)
  264. lp->flag |= LP_WITHIN_THR;
  265. else
  266. lp->flag &= ~LP_WITHIN_THR;
  267. pr_debug("TCP-LP: %05o|%5u|%5u|%15u|%15u|%15u\n", lp->flag,
  268. tcp_snd_cwnd(tp), lp->remote_hz, lp->owd_min, lp->owd_max,
  269. lp->sowd >> 3);
  270. if (lp->flag & LP_WITHIN_THR)
  271. return;
  272. /* FIXME: try to reset owd_min and owd_max here
  273. * so decrease the chance the min/max is no longer suitable
  274. * and will usually within threshold when within inference */
  275. lp->owd_min = lp->sowd >> 3;
  276. lp->owd_max = lp->sowd >> 2;
  277. lp->owd_max_rsv = lp->sowd >> 2;
  278. /* happened within inference
  279. * drop snd_cwnd into 1 */
  280. if (lp->flag & LP_WITHIN_INF)
  281. tcp_snd_cwnd_set(tp, 1U);
  282. /* happened after inference
  283. * cut snd_cwnd into half */
  284. else
  285. tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp) >> 1U, 1U));
  286. /* record this drop time */
  287. lp->last_drop = now;
  288. }
  289. static struct tcp_congestion_ops tcp_lp __read_mostly = {
  290. .init = tcp_lp_init,
  291. .ssthresh = tcp_reno_ssthresh,
  292. .undo_cwnd = tcp_reno_undo_cwnd,
  293. .cong_avoid = tcp_lp_cong_avoid,
  294. .pkts_acked = tcp_lp_pkts_acked,
  295. .owner = THIS_MODULE,
  296. .name = "lp"
  297. };
  298. static int __init tcp_lp_register(void)
  299. {
  300. BUILD_BUG_ON(sizeof(struct lp) > ICSK_CA_PRIV_SIZE);
  301. return tcp_register_congestion_control(&tcp_lp);
  302. }
  303. static void __exit tcp_lp_unregister(void)
  304. {
  305. tcp_unregister_congestion_control(&tcp_lp);
  306. }
  307. module_init(tcp_lp_register);
  308. module_exit(tcp_lp_unregister);
  309. MODULE_AUTHOR("Wong Hoi Sing Edison, Hung Hing Lun Mike");
  310. MODULE_LICENSE("GPL");
  311. MODULE_DESCRIPTION("TCP Low Priority");