PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/net80211/ieee80211_amrr.c

https://github.com/blacklion/GEOM-Events
C | 317 lines | 234 code | 37 blank | 46 comment | 26 complexity | e63d01542706521b43ef09ab3d4a0df1 MD5 | raw file
  1. /* $OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $ */
  2. /*-
  3. * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
  4. * Copyright (c) 2006
  5. * Damien Bergamini <damien.bergamini@free.fr>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <sys/cdefs.h>
  20. __FBSDID("$FreeBSD$");
  21. /*-
  22. * Naive implementation of the Adaptive Multi Rate Retry algorithm:
  23. *
  24. * "IEEE 802.11 Rate Adaptation: A Practical Approach"
  25. * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
  26. * INRIA Sophia - Projet Planete
  27. * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
  28. */
  29. #include "opt_wlan.h"
  30. #include <sys/param.h>
  31. #include <sys/kernel.h>
  32. #include <sys/module.h>
  33. #include <sys/socket.h>
  34. #include <sys/sysctl.h>
  35. #include <net/if.h>
  36. #include <net/if_media.h>
  37. #ifdef INET
  38. #include <netinet/in.h>
  39. #include <netinet/if_ether.h>
  40. #endif
  41. #include <net80211/ieee80211_var.h>
  42. #include <net80211/ieee80211_amrr.h>
  43. #include <net80211/ieee80211_ratectl.h>
  44. #define is_success(amn) \
  45. ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
  46. #define is_failure(amn) \
  47. ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
  48. #define is_enough(amn) \
  49. ((amn)->amn_txcnt > 10)
  50. static void amrr_setinterval(const struct ieee80211vap *, int);
  51. static void amrr_init(struct ieee80211vap *);
  52. static void amrr_deinit(struct ieee80211vap *);
  53. static void amrr_node_init(struct ieee80211_node *);
  54. static void amrr_node_deinit(struct ieee80211_node *);
  55. static int amrr_update(struct ieee80211_amrr *,
  56. struct ieee80211_amrr_node *, struct ieee80211_node *);
  57. static int amrr_rate(struct ieee80211_node *, void *, uint32_t);
  58. static void amrr_tx_complete(const struct ieee80211vap *,
  59. const struct ieee80211_node *, int,
  60. void *, void *);
  61. static void amrr_tx_update(const struct ieee80211vap *vap,
  62. const struct ieee80211_node *, void *, void *, void *);
  63. static void amrr_sysctlattach(struct ieee80211vap *,
  64. struct sysctl_ctx_list *, struct sysctl_oid *);
  65. /* number of references from net80211 layer */
  66. static int nrefs = 0;
  67. static const struct ieee80211_ratectl amrr = {
  68. .ir_name = "amrr",
  69. .ir_attach = NULL,
  70. .ir_detach = NULL,
  71. .ir_init = amrr_init,
  72. .ir_deinit = amrr_deinit,
  73. .ir_node_init = amrr_node_init,
  74. .ir_node_deinit = amrr_node_deinit,
  75. .ir_rate = amrr_rate,
  76. .ir_tx_complete = amrr_tx_complete,
  77. .ir_tx_update = amrr_tx_update,
  78. .ir_setinterval = amrr_setinterval,
  79. };
  80. IEEE80211_RATECTL_MODULE(amrr, 1);
  81. IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
  82. static void
  83. amrr_setinterval(const struct ieee80211vap *vap, int msecs)
  84. {
  85. struct ieee80211_amrr *amrr = vap->iv_rs;
  86. int t;
  87. if (msecs < 100)
  88. msecs = 100;
  89. t = msecs_to_ticks(msecs);
  90. amrr->amrr_interval = (t < 1) ? 1 : t;
  91. }
  92. static void
  93. amrr_init(struct ieee80211vap *vap)
  94. {
  95. struct ieee80211_amrr *amrr;
  96. KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
  97. amrr = vap->iv_rs = malloc(sizeof(struct ieee80211_amrr),
  98. M_80211_RATECTL, M_NOWAIT|M_ZERO);
  99. if (amrr == NULL) {
  100. if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
  101. return;
  102. }
  103. amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
  104. amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
  105. amrr_setinterval(vap, 500 /* ms */);
  106. amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
  107. }
  108. static void
  109. amrr_deinit(struct ieee80211vap *vap)
  110. {
  111. free(vap->iv_rs, M_80211_RATECTL);
  112. }
  113. static void
  114. amrr_node_init(struct ieee80211_node *ni)
  115. {
  116. const struct ieee80211_rateset *rs = &ni->ni_rates;
  117. struct ieee80211vap *vap = ni->ni_vap;
  118. struct ieee80211_amrr *amrr = vap->iv_rs;
  119. struct ieee80211_amrr_node *amn;
  120. if (ni->ni_rctls == NULL) {
  121. ni->ni_rctls = amn = malloc(sizeof(struct ieee80211_amrr_node),
  122. M_80211_RATECTL, M_NOWAIT|M_ZERO);
  123. if (amn == NULL) {
  124. if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
  125. "structure\n");
  126. return;
  127. }
  128. } else
  129. amn = ni->ni_rctls;
  130. amn->amn_amrr = amrr;
  131. amn->amn_success = 0;
  132. amn->amn_recovery = 0;
  133. amn->amn_txcnt = amn->amn_retrycnt = 0;
  134. amn->amn_success_threshold = amrr->amrr_min_success_threshold;
  135. /* pick initial rate */
  136. for (amn->amn_rix = rs->rs_nrates - 1;
  137. amn->amn_rix > 0 && (rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) > 72;
  138. amn->amn_rix--)
  139. ;
  140. ni->ni_txrate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
  141. amn->amn_ticks = ticks;
  142. IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  143. "AMRR initial rate %d", ni->ni_txrate);
  144. }
  145. static void
  146. amrr_node_deinit(struct ieee80211_node *ni)
  147. {
  148. free(ni->ni_rctls, M_80211_RATECTL);
  149. }
  150. static int
  151. amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
  152. struct ieee80211_node *ni)
  153. {
  154. int rix = amn->amn_rix;
  155. KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
  156. if (is_success(amn)) {
  157. amn->amn_success++;
  158. if (amn->amn_success >= amn->amn_success_threshold &&
  159. rix + 1 < ni->ni_rates.rs_nrates) {
  160. amn->amn_recovery = 1;
  161. amn->amn_success = 0;
  162. rix++;
  163. IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  164. "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
  165. ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
  166. amn->amn_txcnt, amn->amn_retrycnt);
  167. } else {
  168. amn->amn_recovery = 0;
  169. }
  170. } else if (is_failure(amn)) {
  171. amn->amn_success = 0;
  172. if (rix > 0) {
  173. if (amn->amn_recovery) {
  174. amn->amn_success_threshold *= 2;
  175. if (amn->amn_success_threshold >
  176. amrr->amrr_max_success_threshold)
  177. amn->amn_success_threshold =
  178. amrr->amrr_max_success_threshold;
  179. } else {
  180. amn->amn_success_threshold =
  181. amrr->amrr_min_success_threshold;
  182. }
  183. rix--;
  184. IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  185. "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
  186. ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
  187. amn->amn_txcnt, amn->amn_retrycnt);
  188. }
  189. amn->amn_recovery = 0;
  190. }
  191. /* reset counters */
  192. amn->amn_txcnt = 0;
  193. amn->amn_retrycnt = 0;
  194. return rix;
  195. }
  196. /*
  197. * Return the rate index to use in sending a data frame.
  198. * Update our internal state if it's been long enough.
  199. * If the rate changes we also update ni_txrate to match.
  200. */
  201. static int
  202. amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
  203. {
  204. struct ieee80211_amrr_node *amn = ni->ni_rctls;
  205. struct ieee80211_amrr *amrr = amn->amn_amrr;
  206. int rix;
  207. if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
  208. rix = amrr_update(amrr, amn, ni);
  209. if (rix != amn->amn_rix) {
  210. /* update public rate */
  211. ni->ni_txrate =
  212. ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
  213. amn->amn_rix = rix;
  214. }
  215. amn->amn_ticks = ticks;
  216. } else
  217. rix = amn->amn_rix;
  218. return rix;
  219. }
  220. /*
  221. * Update statistics with tx complete status. Ok is non-zero
  222. * if the packet is known to be ACK'd. Retries has the number
  223. * retransmissions (i.e. xmit attempts - 1).
  224. */
  225. static void
  226. amrr_tx_complete(const struct ieee80211vap *vap,
  227. const struct ieee80211_node *ni, int ok,
  228. void *arg1, void *arg2 __unused)
  229. {
  230. struct ieee80211_amrr_node *amn = ni->ni_rctls;
  231. int retries = *(int *)arg1;
  232. amn->amn_txcnt++;
  233. if (ok)
  234. amn->amn_success++;
  235. amn->amn_retrycnt += retries;
  236. }
  237. /*
  238. * Set tx count/retry statistics explicitly. Intended for
  239. * drivers that poll the device for statistics maintained
  240. * in the device.
  241. */
  242. static void
  243. amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
  244. void *arg1, void *arg2, void *arg3)
  245. {
  246. struct ieee80211_amrr_node *amn = ni->ni_rctls;
  247. int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
  248. amn->amn_txcnt = txcnt;
  249. amn->amn_success = success;
  250. amn->amn_retrycnt = retrycnt;
  251. }
  252. static int
  253. amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
  254. {
  255. struct ieee80211vap *vap = arg1;
  256. struct ieee80211_amrr *amrr = vap->iv_rs;
  257. int msecs = ticks_to_msecs(amrr->amrr_interval);
  258. int error;
  259. error = sysctl_handle_int(oidp, &msecs, 0, req);
  260. if (error || !req->newptr)
  261. return error;
  262. amrr_setinterval(vap, msecs);
  263. return 0;
  264. }
  265. static void
  266. amrr_sysctlattach(struct ieee80211vap *vap,
  267. struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
  268. {
  269. struct ieee80211_amrr *amrr = vap->iv_rs;
  270. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  271. "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
  272. 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
  273. /* XXX bounds check values */
  274. SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  275. "amrr_max_sucess_threshold", CTLFLAG_RW,
  276. &amrr->amrr_max_success_threshold, 0, "");
  277. SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  278. "amrr_min_sucess_threshold", CTLFLAG_RW,
  279. &amrr->amrr_min_success_threshold, 0, "");
  280. }