tcp_lp.c 8.7 KB

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