cc_vegas.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2009-2010
  5. * Swinburne University of Technology, Melbourne, Australia
  6. * Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
  7. * Copyright (c) 2010-2011 The FreeBSD Foundation
  8. * All rights reserved.
  9. *
  10. * This software was developed at the Centre for Advanced Internet
  11. * Architectures, Swinburne University of Technology, by David Hayes and
  12. * Lawrence Stewart, made possible in part by a grant from the Cisco University
  13. * Research Program Fund at Community Foundation Silicon Valley.
  14. *
  15. * Portions of this software were developed at the Centre for Advanced Internet
  16. * Architectures, Swinburne University of Technology, Melbourne, Australia by
  17. * David Hayes under sponsorship from the FreeBSD Foundation.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. /*
  41. * An implementation of the Vegas congestion control algorithm for FreeBSD,
  42. * based on L. S. Brakmo and L. L. Peterson, "TCP Vegas: end to end congestion
  43. * avoidance on a global internet", IEEE J. Sel. Areas Commun., vol. 13, no. 8,
  44. * pp. 1465-1480, Oct. 1995. The original Vegas duplicate ack policy has not
  45. * been implemented, since clock ticks are not as coarse as they were (i.e.
  46. * 500ms) when Vegas was designed. Also, packets are timed once per RTT as in
  47. * the original paper.
  48. *
  49. * Originally released as part of the NewTCP research project at Swinburne
  50. * University of Technology's Centre for Advanced Internet Architectures,
  51. * Melbourne, Australia, which was made possible in part by a grant from the
  52. * Cisco University Research Program Fund at Community Foundation Silicon
  53. * Valley. More details are available at:
  54. * http://caia.swin.edu.au/urp/newtcp/
  55. */
  56. #include <sys/cdefs.h>
  57. __FBSDID("$FreeBSD$");
  58. #include <sys/param.h>
  59. #include <sys/kernel.h>
  60. #include <sys/khelp.h>
  61. #include <sys/malloc.h>
  62. #include <sys/module.h>
  63. #include <sys/queue.h>
  64. #include <sys/socket.h>
  65. #include <sys/socketvar.h>
  66. #include <sys/sysctl.h>
  67. #include <sys/systm.h>
  68. #include <net/vnet.h>
  69. #include <netinet/tcp.h>
  70. #include <netinet/tcp_timer.h>
  71. #include <netinet/tcp_var.h>
  72. #include <netinet/cc/cc.h>
  73. #include <netinet/cc/cc_module.h>
  74. #include <netinet/khelp/h_ertt.h>
  75. /*
  76. * Private signal type for rate based congestion signal.
  77. * See <netinet/cc.h> for appropriate bit-range to use for private signals.
  78. */
  79. #define CC_VEGAS_RATE 0x01000000
  80. static void vegas_ack_received(struct cc_var *ccv, uint16_t ack_type);
  81. static void vegas_cb_destroy(struct cc_var *ccv);
  82. static int vegas_cb_init(struct cc_var *ccv);
  83. static void vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type);
  84. static void vegas_conn_init(struct cc_var *ccv);
  85. static int vegas_mod_init(void);
  86. struct vegas {
  87. int slow_start_toggle;
  88. };
  89. static int32_t ertt_id;
  90. VNET_DEFINE_STATIC(uint32_t, vegas_alpha) = 1;
  91. VNET_DEFINE_STATIC(uint32_t, vegas_beta) = 3;
  92. #define V_vegas_alpha VNET(vegas_alpha)
  93. #define V_vegas_beta VNET(vegas_beta)
  94. static MALLOC_DEFINE(M_VEGAS, "vegas data",
  95. "Per connection data required for the Vegas congestion control algorithm");
  96. struct cc_algo vegas_cc_algo = {
  97. .name = "vegas",
  98. .ack_received = vegas_ack_received,
  99. .cb_destroy = vegas_cb_destroy,
  100. .cb_init = vegas_cb_init,
  101. .cong_signal = vegas_cong_signal,
  102. .conn_init = vegas_conn_init,
  103. .mod_init = vegas_mod_init
  104. };
  105. /*
  106. * The vegas window adjustment is done once every RTT, as indicated by the
  107. * ERTT_NEW_MEASUREMENT flag. This flag is reset once the new measurment data
  108. * has been used.
  109. */
  110. static void
  111. vegas_ack_received(struct cc_var *ccv, uint16_t ack_type)
  112. {
  113. struct ertt *e_t;
  114. struct vegas *vegas_data;
  115. long actual_tx_rate, expected_tx_rate, ndiff;
  116. e_t = khelp_get_osd(CCV(ccv, osd), ertt_id);
  117. vegas_data = ccv->cc_data;
  118. if (e_t->flags & ERTT_NEW_MEASUREMENT) { /* Once per RTT. */
  119. if (e_t->minrtt && e_t->markedpkt_rtt) {
  120. expected_tx_rate = e_t->marked_snd_cwnd / e_t->minrtt;
  121. actual_tx_rate = e_t->bytes_tx_in_marked_rtt /
  122. e_t->markedpkt_rtt;
  123. ndiff = (expected_tx_rate - actual_tx_rate) *
  124. e_t->minrtt / CCV(ccv, t_maxseg);
  125. if (ndiff < V_vegas_alpha) {
  126. if (CCV(ccv, snd_cwnd) <=
  127. CCV(ccv, snd_ssthresh)) {
  128. vegas_data->slow_start_toggle =
  129. vegas_data->slow_start_toggle ?
  130. 0 : 1;
  131. } else {
  132. vegas_data->slow_start_toggle = 0;
  133. CCV(ccv, snd_cwnd) =
  134. min(CCV(ccv, snd_cwnd) +
  135. CCV(ccv, t_maxseg),
  136. TCP_MAXWIN << CCV(ccv, snd_scale));
  137. }
  138. } else if (ndiff > V_vegas_beta) {
  139. /* Rate-based congestion. */
  140. vegas_cong_signal(ccv, CC_VEGAS_RATE);
  141. vegas_data->slow_start_toggle = 0;
  142. }
  143. }
  144. e_t->flags &= ~ERTT_NEW_MEASUREMENT;
  145. }
  146. if (vegas_data->slow_start_toggle)
  147. newreno_cc_algo.ack_received(ccv, ack_type);
  148. }
  149. static void
  150. vegas_cb_destroy(struct cc_var *ccv)
  151. {
  152. free(ccv->cc_data, M_VEGAS);
  153. }
  154. static int
  155. vegas_cb_init(struct cc_var *ccv)
  156. {
  157. struct vegas *vegas_data;
  158. vegas_data = malloc(sizeof(struct vegas), M_VEGAS, M_NOWAIT);
  159. if (vegas_data == NULL)
  160. return (ENOMEM);
  161. vegas_data->slow_start_toggle = 1;
  162. ccv->cc_data = vegas_data;
  163. return (0);
  164. }
  165. /*
  166. * If congestion has been triggered triggered by the Vegas measured rates, it is
  167. * handled here, otherwise it falls back to newreno's congestion handling.
  168. */
  169. static void
  170. vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type)
  171. {
  172. struct vegas *vegas_data;
  173. int presignalrecov;
  174. vegas_data = ccv->cc_data;
  175. if (IN_RECOVERY(CCV(ccv, t_flags)))
  176. presignalrecov = 1;
  177. else
  178. presignalrecov = 0;
  179. switch(signal_type) {
  180. case CC_VEGAS_RATE:
  181. if (!IN_RECOVERY(CCV(ccv, t_flags))) {
  182. CCV(ccv, snd_cwnd) = max(2 * CCV(ccv, t_maxseg),
  183. CCV(ccv, snd_cwnd) - CCV(ccv, t_maxseg));
  184. if (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh))
  185. /* Exit slow start. */
  186. CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
  187. }
  188. break;
  189. default:
  190. newreno_cc_algo.cong_signal(ccv, signal_type);
  191. }
  192. if (IN_RECOVERY(CCV(ccv, t_flags)) && !presignalrecov)
  193. vegas_data->slow_start_toggle =
  194. (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh)) ? 1 : 0;
  195. }
  196. static void
  197. vegas_conn_init(struct cc_var *ccv)
  198. {
  199. struct vegas *vegas_data;
  200. vegas_data = ccv->cc_data;
  201. vegas_data->slow_start_toggle = 1;
  202. }
  203. static int
  204. vegas_mod_init(void)
  205. {
  206. ertt_id = khelp_get_id("ertt");
  207. if (ertt_id <= 0) {
  208. printf("%s: h_ertt module not found\n", __func__);
  209. return (ENOENT);
  210. }
  211. vegas_cc_algo.after_idle = newreno_cc_algo.after_idle;
  212. vegas_cc_algo.post_recovery = newreno_cc_algo.post_recovery;
  213. return (0);
  214. }
  215. static int
  216. vegas_alpha_handler(SYSCTL_HANDLER_ARGS)
  217. {
  218. int error;
  219. uint32_t new;
  220. new = V_vegas_alpha;
  221. error = sysctl_handle_int(oidp, &new, 0, req);
  222. if (error == 0 && req->newptr != NULL) {
  223. if (new == 0 || new > V_vegas_beta)
  224. error = EINVAL;
  225. else
  226. V_vegas_alpha = new;
  227. }
  228. return (error);
  229. }
  230. static int
  231. vegas_beta_handler(SYSCTL_HANDLER_ARGS)
  232. {
  233. int error;
  234. uint32_t new;
  235. new = V_vegas_beta;
  236. error = sysctl_handle_int(oidp, &new, 0, req);
  237. if (error == 0 && req->newptr != NULL) {
  238. if (new == 0 || new < V_vegas_alpha)
  239. error = EINVAL;
  240. else
  241. V_vegas_beta = new;
  242. }
  243. return (error);
  244. }
  245. SYSCTL_DECL(_net_inet_tcp_cc_vegas);
  246. SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, vegas,
  247. CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
  248. "Vegas related settings");
  249. SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, alpha,
  250. CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  251. &VNET_NAME(vegas_alpha), 1, &vegas_alpha_handler, "IU",
  252. "vegas alpha, specified as number of \"buffers\" (0 < alpha < beta)");
  253. SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, beta,
  254. CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  255. &VNET_NAME(vegas_beta), 3, &vegas_beta_handler, "IU",
  256. "vegas beta, specified as number of \"buffers\" (0 < alpha < beta)");
  257. DECLARE_CC_MODULE(vegas, &vegas_cc_algo);
  258. MODULE_VERSION(vegas, 1);
  259. MODULE_DEPEND(vegas, ertt, 1, 1, 1);