tcp_lp.c 8.8 KB

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