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