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 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 /*- 25 * Naive implementation of the Adaptive Multi Rate Retry algorithm: 26 * 27 * "IEEE 802.11 Rate Adaptation: A Practical Approach" 28 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti 29 * INRIA Sophia - Projet Planete 30 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html 31 */ 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/socket.h> 38 #include <sys/sysctl.h> 39 40 #include <net/if.h> 41 #include <net/if_media.h> 42 43 #ifdef INET 44 #include <netinet/in.h> 45 #include <netinet/if_ether.h> 46 #endif 47 48 #include <net80211/ieee80211_var.h> 49 #include <net80211/ieee80211_ht.h> 50 #include <net80211/ieee80211_amrr.h> 51 #include <net80211/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 amrr = vap->iv_rs = malloc(sizeof(struct ieee80211_amrr), 115 M_80211_RATECTL, M_NOWAIT|M_ZERO); 116 if (amrr == NULL) { 117 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n"); 118 return; 119 } 120 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD; 121 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD; 122 amrr_setinterval(vap, 500 /* ms */); 123 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid); 124 } 125 126 static void 127 amrr_deinit(struct ieee80211vap *vap) 128 { 129 free(vap->iv_rs, M_80211_RATECTL); 130 } 131 132 static int 133 amrr_node_is_11n(struct ieee80211_node *ni) 134 { 135 136 if (ni->ni_chan == NULL) 137 return (0); 138 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 139 return (0); 140 return (IEEE80211_IS_CHAN_HT(ni->ni_chan)); 141 } 142 143 static void 144 amrr_node_init(struct ieee80211_node *ni) 145 { 146 const struct ieee80211_rateset *rs = NULL; 147 struct ieee80211vap *vap = ni->ni_vap; 148 struct ieee80211_amrr *amrr = vap->iv_rs; 149 struct ieee80211_amrr_node *amn; 150 uint8_t rate; 151 152 if (ni->ni_rctls == NULL) { 153 ni->ni_rctls = amn = malloc(sizeof(struct ieee80211_amrr_node), 154 M_80211_RATECTL, M_NOWAIT|M_ZERO); 155 if (amn == NULL) { 156 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl " 157 "structure\n"); 158 return; 159 } 160 } else 161 amn = ni->ni_rctls; 162 amn->amn_amrr = amrr; 163 amn->amn_success = 0; 164 amn->amn_recovery = 0; 165 amn->amn_txcnt = amn->amn_retrycnt = 0; 166 amn->amn_success_threshold = amrr->amrr_min_success_threshold; 167 168 /* 11n or not? Pick the right rateset */ 169 if (amrr_node_is_11n(ni)) { 170 /* XXX ew */ 171 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 172 "%s: 11n node", __func__); 173 rs = (struct ieee80211_rateset *) &ni->ni_htrates; 174 } else { 175 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 176 "%s: non-11n node", __func__); 177 rs = &ni->ni_rates; 178 } 179 180 /* Initial rate - lowest */ 181 rate = rs->rs_rates[0]; 182 183 /* XXX clear the basic rate flag if it's not 11n */ 184 if (! amrr_node_is_11n(ni)) 185 rate &= IEEE80211_RATE_VAL; 186 187 /* pick initial rate from the rateset - HT or otherwise */ 188 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0; 189 amn->amn_rix--) { 190 /* legacy - anything < 36mbit, stop searching */ 191 /* 11n - stop at MCS4 / MCS12 / MCS28 */ 192 if (amrr_node_is_11n(ni) && 193 (rs->rs_rates[amn->amn_rix] & 0x7) < 4) 194 break; 195 else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72) 196 break; 197 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL; 198 } 199 200 /* if the rate is an 11n rate, ensure the MCS bit is set */ 201 if (amrr_node_is_11n(ni)) 202 rate |= IEEE80211_RATE_MCS; 203 204 /* Assign initial rate from the rateset */ 205 ni->ni_txrate = rate; 206 amn->amn_ticks = ticks; 207 208 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 209 "AMRR: nrates=%d, initial rate %d", 210 rs->rs_nrates, 211 rate); 212 } 213 214 static void 215 amrr_node_deinit(struct ieee80211_node *ni) 216 { 217 free(ni->ni_rctls, M_80211_RATECTL); 218 } 219 220 static int 221 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn, 222 struct ieee80211_node *ni) 223 { 224 int rix = amn->amn_rix; 225 const struct ieee80211_rateset *rs = NULL; 226 227 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt)); 228 229 /* 11n or not? Pick the right rateset */ 230 if (amrr_node_is_11n(ni)) { 231 /* XXX ew */ 232 rs = (struct ieee80211_rateset *) &ni->ni_htrates; 233 } else { 234 rs = &ni->ni_rates; 235 } 236 237 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 238 "AMRR: current rate %d, txcnt=%d, retrycnt=%d", 239 rs->rs_rates[rix] & IEEE80211_RATE_VAL, 240 amn->amn_txcnt, 241 amn->amn_retrycnt); 242 243 /* 244 * XXX This is totally bogus for 11n, as although high MCS 245 * rates for each stream may be failing, the next stream 246 * should be checked. 247 * 248 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to 249 * MCS23, we should skip 6/7 and try 8 onwards. 250 */ 251 if (is_success(amn)) { 252 amn->amn_success++; 253 if (amn->amn_success >= amn->amn_success_threshold && 254 rix + 1 < rs->rs_nrates) { 255 amn->amn_recovery = 1; 256 amn->amn_success = 0; 257 rix++; 258 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 259 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)", 260 rs->rs_rates[rix] & IEEE80211_RATE_VAL, 261 amn->amn_txcnt, amn->amn_retrycnt); 262 } else { 263 amn->amn_recovery = 0; 264 } 265 } else if (is_failure(amn)) { 266 amn->amn_success = 0; 267 if (rix > 0) { 268 if (amn->amn_recovery) { 269 amn->amn_success_threshold *= 2; 270 if (amn->amn_success_threshold > 271 amrr->amrr_max_success_threshold) 272 amn->amn_success_threshold = 273 amrr->amrr_max_success_threshold; 274 } else { 275 amn->amn_success_threshold = 276 amrr->amrr_min_success_threshold; 277 } 278 rix--; 279 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 280 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)", 281 rs->rs_rates[rix] & IEEE80211_RATE_VAL, 282 amn->amn_txcnt, amn->amn_retrycnt); 283 } 284 amn->amn_recovery = 0; 285 } 286 287 /* reset counters */ 288 amn->amn_txcnt = 0; 289 amn->amn_retrycnt = 0; 290 291 return rix; 292 } 293 294 /* 295 * Return the rate index to use in sending a data frame. 296 * Update our internal state if it's been long enough. 297 * If the rate changes we also update ni_txrate to match. 298 */ 299 static int 300 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused) 301 { 302 struct ieee80211_amrr_node *amn = ni->ni_rctls; 303 struct ieee80211_amrr *amrr = amn->amn_amrr; 304 const struct ieee80211_rateset *rs = NULL; 305 int rix; 306 307 /* 11n or not? Pick the right rateset */ 308 if (amrr_node_is_11n(ni)) { 309 /* XXX ew */ 310 rs = (struct ieee80211_rateset *) &ni->ni_htrates; 311 } else { 312 rs = &ni->ni_rates; 313 } 314 315 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) { 316 rix = amrr_update(amrr, amn, ni); 317 if (rix != amn->amn_rix) { 318 /* update public rate */ 319 ni->ni_txrate = rs->rs_rates[rix]; 320 /* XXX strip basic rate flag from txrate, if non-11n */ 321 if (amrr_node_is_11n(ni)) 322 ni->ni_txrate |= IEEE80211_RATE_MCS; 323 else 324 ni->ni_txrate &= IEEE80211_RATE_VAL; 325 amn->amn_rix = rix; 326 } 327 amn->amn_ticks = ticks; 328 } else 329 rix = amn->amn_rix; 330 return rix; 331 } 332 333 /* 334 * Update statistics with tx complete status. Ok is non-zero 335 * if the packet is known to be ACK'd. Retries has the number 336 * retransmissions (i.e. xmit attempts - 1). 337 */ 338 static void 339 amrr_tx_complete(const struct ieee80211vap *vap, 340 const struct ieee80211_node *ni, int ok, 341 void *arg1, void *arg2 __unused) 342 { 343 struct ieee80211_amrr_node *amn = ni->ni_rctls; 344 int retries = *(int *)arg1; 345 346 amn->amn_txcnt++; 347 if (ok) 348 amn->amn_success++; 349 amn->amn_retrycnt += retries; 350 } 351 352 /* 353 * Set tx count/retry statistics explicitly. Intended for 354 * drivers that poll the device for statistics maintained 355 * in the device. 356 */ 357 static void 358 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni, 359 void *arg1, void *arg2, void *arg3) 360 { 361 struct ieee80211_amrr_node *amn = ni->ni_rctls; 362 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3; 363 364 amn->amn_txcnt = txcnt; 365 amn->amn_success = success; 366 amn->amn_retrycnt = retrycnt; 367 } 368 369 static int 370 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS) 371 { 372 struct ieee80211vap *vap = arg1; 373 struct ieee80211_amrr *amrr = vap->iv_rs; 374 int msecs = ticks_to_msecs(amrr->amrr_interval); 375 int error; 376 377 error = sysctl_handle_int(oidp, &msecs, 0, req); 378 if (error || !req->newptr) 379 return error; 380 amrr_setinterval(vap, msecs); 381 return 0; 382 } 383 384 static void 385 amrr_sysctlattach(struct ieee80211vap *vap, 386 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree) 387 { 388 struct ieee80211_amrr *amrr = vap->iv_rs; 389 390 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 391 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap, 392 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)"); 393 /* XXX bounds check values */ 394 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 395 "amrr_max_sucess_threshold", CTLFLAG_RW, 396 &amrr->amrr_max_success_threshold, 0, ""); 397 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 398 "amrr_min_sucess_threshold", CTLFLAG_RW, 399 &amrr->amrr_min_success_threshold, 0, ""); 400 } 401