cc_hd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 Hamilton Institute's delay-based congestion control
  42. * algorithm for FreeBSD, based on "A strategy for fair coexistence of loss and
  43. * delay-based congestion control algorithms," by L. Budzisz, R. Stanojevic, R.
  44. * Shorten, and F. Baker, IEEE Commun. Lett., vol. 13, no. 7, pp. 555--557, Jul.
  45. * 2009.
  46. *
  47. * Originally released as part of the NewTCP research project at Swinburne
  48. * University of Technology's Centre for Advanced Internet Architectures,
  49. * Melbourne, Australia, which was made possible in part by a grant from the
  50. * Cisco University Research Program Fund at Community Foundation Silicon
  51. * Valley. More details are available at:
  52. * http://caia.swin.edu.au/urp/newtcp/
  53. */
  54. #include <sys/cdefs.h>
  55. __FBSDID("$FreeBSD$");
  56. #include <sys/param.h>
  57. #include <sys/kernel.h>
  58. #include <sys/khelp.h>
  59. #include <sys/limits.h>
  60. #include <sys/malloc.h>
  61. #include <sys/module.h>
  62. #include <sys/queue.h>
  63. #include <sys/socket.h>
  64. #include <sys/socketvar.h>
  65. #include <sys/sysctl.h>
  66. #include <sys/systm.h>
  67. #include <net/vnet.h>
  68. #include <netinet/tcp.h>
  69. #include <netinet/tcp_seq.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. /* Largest possible number returned by random(). */
  76. #define RANDOM_MAX INT_MAX
  77. static void hd_ack_received(struct cc_var *ccv, uint16_t ack_type);
  78. static int hd_mod_init(void);
  79. static int ertt_id;
  80. VNET_DEFINE_STATIC(uint32_t, hd_qthresh) = 20;
  81. VNET_DEFINE_STATIC(uint32_t, hd_qmin) = 5;
  82. VNET_DEFINE_STATIC(uint32_t, hd_pmax) = 5;
  83. #define V_hd_qthresh VNET(hd_qthresh)
  84. #define V_hd_qmin VNET(hd_qmin)
  85. #define V_hd_pmax VNET(hd_pmax)
  86. struct cc_algo hd_cc_algo = {
  87. .name = "hd",
  88. .ack_received = hd_ack_received,
  89. .mod_init = hd_mod_init
  90. };
  91. /*
  92. * Hamilton backoff function. Returns 1 if we should backoff or 0 otherwise.
  93. */
  94. static __inline int
  95. should_backoff(int qdly, int maxqdly)
  96. {
  97. unsigned long p;
  98. if (qdly < V_hd_qthresh) {
  99. p = (((RANDOM_MAX / 100) * V_hd_pmax) /
  100. (V_hd_qthresh - V_hd_qmin)) * (qdly - V_hd_qmin);
  101. } else {
  102. if (qdly > V_hd_qthresh)
  103. p = (((RANDOM_MAX / 100) * V_hd_pmax) /
  104. (maxqdly - V_hd_qthresh)) * (maxqdly - qdly);
  105. else
  106. p = (RANDOM_MAX / 100) * V_hd_pmax;
  107. }
  108. return (random() < p);
  109. }
  110. /*
  111. * If the ack type is CC_ACK, and the inferred queueing delay is greater than
  112. * the Qmin threshold, cwnd is reduced probabilistically. When backing off due
  113. * to delay, HD behaves like NewReno when an ECN signal is received. HD behaves
  114. * as NewReno in all other circumstances.
  115. */
  116. static void
  117. hd_ack_received(struct cc_var *ccv, uint16_t ack_type)
  118. {
  119. struct ertt *e_t;
  120. int qdly;
  121. if (ack_type == CC_ACK) {
  122. e_t = khelp_get_osd(CCV(ccv, osd), ertt_id);
  123. if (e_t->rtt && e_t->minrtt && V_hd_qthresh > 0) {
  124. qdly = e_t->rtt - e_t->minrtt;
  125. if (qdly > V_hd_qmin &&
  126. !IN_RECOVERY(CCV(ccv, t_flags))) {
  127. /* Probabilistic backoff of cwnd. */
  128. if (should_backoff(qdly,
  129. e_t->maxrtt - e_t->minrtt)) {
  130. /*
  131. * Update cwnd and ssthresh update to
  132. * half cwnd and behave like an ECN (ie
  133. * not a packet loss).
  134. */
  135. newreno_cc_algo.cong_signal(ccv,
  136. CC_ECN);
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. newreno_cc_algo.ack_received(ccv, ack_type); /* As for NewReno. */
  143. }
  144. static int
  145. hd_mod_init(void)
  146. {
  147. ertt_id = khelp_get_id("ertt");
  148. if (ertt_id <= 0) {
  149. printf("%s: h_ertt module not found\n", __func__);
  150. return (ENOENT);
  151. }
  152. hd_cc_algo.after_idle = newreno_cc_algo.after_idle;
  153. hd_cc_algo.cong_signal = newreno_cc_algo.cong_signal;
  154. hd_cc_algo.post_recovery = newreno_cc_algo.post_recovery;
  155. return (0);
  156. }
  157. static int
  158. hd_pmax_handler(SYSCTL_HANDLER_ARGS)
  159. {
  160. int error;
  161. uint32_t new;
  162. new = V_hd_pmax;
  163. error = sysctl_handle_int(oidp, &new, 0, req);
  164. if (error == 0 && req->newptr != NULL) {
  165. if (new == 0 || new > 100)
  166. error = EINVAL;
  167. else
  168. V_hd_pmax = new;
  169. }
  170. return (error);
  171. }
  172. static int
  173. hd_qmin_handler(SYSCTL_HANDLER_ARGS)
  174. {
  175. int error;
  176. uint32_t new;
  177. new = V_hd_qmin;
  178. error = sysctl_handle_int(oidp, &new, 0, req);
  179. if (error == 0 && req->newptr != NULL) {
  180. if (new > V_hd_qthresh)
  181. error = EINVAL;
  182. else
  183. V_hd_qmin = new;
  184. }
  185. return (error);
  186. }
  187. static int
  188. hd_qthresh_handler(SYSCTL_HANDLER_ARGS)
  189. {
  190. int error;
  191. uint32_t new;
  192. new = V_hd_qthresh;
  193. error = sysctl_handle_int(oidp, &new, 0, req);
  194. if (error == 0 && req->newptr != NULL) {
  195. if (new == 0 || new < V_hd_qmin)
  196. error = EINVAL;
  197. else
  198. V_hd_qthresh = new;
  199. }
  200. return (error);
  201. }
  202. SYSCTL_DECL(_net_inet_tcp_cc_hd);
  203. SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, hd, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
  204. "Hamilton delay-based congestion control related settings");
  205. SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_threshold,
  206. CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  207. &VNET_NAME(hd_qthresh), 20, &hd_qthresh_handler, "IU",
  208. "queueing congestion threshold (qth) in ticks");
  209. SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, pmax,
  210. CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  211. &VNET_NAME(hd_pmax), 5, &hd_pmax_handler, "IU",
  212. "per packet maximum backoff probability as a percentage");
  213. SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_min,
  214. CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  215. &VNET_NAME(hd_qmin), 5, &hd_qmin_handler, "IU",
  216. "minimum queueing delay threshold (qmin) in ticks");
  217. DECLARE_CC_MODULE(hd, &hd_cc_algo);
  218. MODULE_VERSION(hd, 1);
  219. MODULE_DEPEND(hd, ertt, 1, 1, 1);