amrr.c revision 185522
1/*- 2 * Copyright (c) 2004 INRIA 3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 14 * redistribution must be conditioned upon including a substantially 15 * similar Disclaimer requirement for further binary redistribution. 16 * 3. Neither the names of the above-listed copyright holders nor the names 17 * of any contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * NO WARRANTY 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 29 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 30 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 33 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 35 * THE POSSIBILITY OF SUCH DAMAGES. 36 * 37 */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/amrr/amrr.c 185522 2008-12-01 16:53:01Z sam $"); 41 42/* 43 * AMRR rate control. See: 44 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html 45 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by 46 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti 47 */ 48#include "opt_inet.h" 49#include "opt_wlan.h" 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/sysctl.h> 54#include <sys/module.h> 55#include <sys/kernel.h> 56#include <sys/lock.h> 57#include <sys/mutex.h> 58#include <sys/errno.h> 59 60#include <machine/bus.h> 61#include <machine/resource.h> 62#include <sys/bus.h> 63 64#include <sys/socket.h> 65 66#include <net/if.h> 67#include <net/if_media.h> 68#include <net/if_arp.h> 69 70#include <net80211/ieee80211_var.h> 71 72#include <net/bpf.h> 73 74#ifdef INET 75#include <netinet/in.h> 76#include <netinet/if_ether.h> 77#endif 78 79#include <dev/ath/if_athvar.h> 80#include <dev/ath/ath_rate/amrr/amrr.h> 81#include <dev/ath/ath_hal/ah_desc.h> 82 83static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ 84static int ath_rate_max_success_threshold = 10; 85static int ath_rate_min_success_threshold = 1; 86 87static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, 88 int rate); 89static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); 90static void ath_rate_ctl(void *, struct ieee80211_node *); 91 92void 93ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 94{ 95 /* NB: assumed to be zero'd by caller */ 96} 97 98void 99ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 100{ 101} 102 103void 104ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 105 int shortPreamble, size_t frameLen, 106 u_int8_t *rix, int *try0, u_int8_t *txrate) 107{ 108 struct amrr_node *amn = ATH_NODE_AMRR(an); 109 110 *rix = amn->amn_tx_rix0; 111 *try0 = amn->amn_tx_try0; 112 if (shortPreamble) 113 *txrate = amn->amn_tx_rate0sp; 114 else 115 *txrate = amn->amn_tx_rate0; 116} 117 118void 119ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 120 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 121{ 122 struct amrr_node *amn = ATH_NODE_AMRR(an); 123 124 ath_hal_setupxtxdesc(sc->sc_ah, ds 125 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */ 126 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */ 127 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */ 128 ); 129} 130 131void 132ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 133 const struct ath_buf *bf) 134{ 135 struct amrr_node *amn = ATH_NODE_AMRR(an); 136 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 137 int sr = ts->ts_shortretry; 138 int lr = ts->ts_longretry; 139 int retry_count = sr + lr; 140 141 amn->amn_tx_try0_cnt++; 142 if (retry_count == 1) { 143 amn->amn_tx_try1_cnt++; 144 } else if (retry_count == 2) { 145 amn->amn_tx_try1_cnt++; 146 amn->amn_tx_try2_cnt++; 147 } else if (retry_count == 3) { 148 amn->amn_tx_try1_cnt++; 149 amn->amn_tx_try2_cnt++; 150 amn->amn_tx_try3_cnt++; 151 } else if (retry_count > 3) { 152 amn->amn_tx_try1_cnt++; 153 amn->amn_tx_try2_cnt++; 154 amn->amn_tx_try3_cnt++; 155 amn->amn_tx_failure_cnt++; 156 } 157 if (amn->amn_interval != 0 && 158 ticks - amn->amn_ticks > amn->amn_interval) { 159 ath_rate_ctl(sc, &an->an_node); 160 amn->amn_ticks = ticks; 161 } 162} 163 164void 165ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 166{ 167 if (isnew) 168 ath_rate_ctl_start(sc, &an->an_node); 169} 170 171static void 172node_reset(struct amrr_node *amn) 173{ 174 amn->amn_tx_try0_cnt = 0; 175 amn->amn_tx_try1_cnt = 0; 176 amn->amn_tx_try2_cnt = 0; 177 amn->amn_tx_try3_cnt = 0; 178 amn->amn_tx_failure_cnt = 0; 179 amn->amn_success = 0; 180 amn->amn_recovery = 0; 181 amn->amn_success_threshold = ath_rate_min_success_threshold; 182} 183 184 185/** 186 * The code below assumes that we are dealing with hardware multi rate retry 187 * I have no idea what will happen if you try to use this module with another 188 * type of hardware. Your machine might catch fire or it might work with 189 * horrible performance... 190 */ 191static void 192ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) 193{ 194 struct ath_node *an = ATH_NODE(ni); 195 struct amrr_node *amn = ATH_NODE_AMRR(an); 196 struct ieee80211vap *vap = ni->ni_vap; 197 const HAL_RATE_TABLE *rt = sc->sc_currates; 198 u_int8_t rix; 199 200 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 201 202 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni, 203 "%s: set xmit rate to %dM", __func__, 204 ni->ni_rates.rs_nrates > 0 ? 205 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); 206 207 amn->amn_rix = rate; 208 /* 209 * Before associating a node has no rate set setup 210 * so we can't calculate any transmit codes to use. 211 * This is ok since we should never be sending anything 212 * but management frames and those always go at the 213 * lowest hardware rate. 214 */ 215 if (ni->ni_rates.rs_nrates > 0) { 216 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL; 217 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate]; 218 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode; 219 amn->amn_tx_rate0sp = amn->amn_tx_rate0 | 220 rt->info[amn->amn_tx_rix0].shortPreamble; 221 if (sc->sc_mrretry) { 222 amn->amn_tx_try0 = 1; 223 amn->amn_tx_try1 = 1; 224 amn->amn_tx_try2 = 1; 225 amn->amn_tx_try3 = 1; 226 if (--rate >= 0) { 227 rix = sc->sc_rixmap[ 228 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 229 amn->amn_tx_rate1 = rt->info[rix].rateCode; 230 amn->amn_tx_rate1sp = amn->amn_tx_rate1 | 231 rt->info[rix].shortPreamble; 232 } else { 233 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 234 } 235 if (--rate >= 0) { 236 rix = sc->sc_rixmap[ 237 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 238 amn->amn_tx_rate2 = rt->info[rix].rateCode; 239 amn->amn_tx_rate2sp = amn->amn_tx_rate2 | 240 rt->info[rix].shortPreamble; 241 } else { 242 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 243 } 244 if (rate > 0) { 245 /* NB: only do this if we didn't already do it above */ 246 amn->amn_tx_rate3 = rt->info[0].rateCode; 247 amn->amn_tx_rate3sp = 248 amn->amn_tx_rate3 | rt->info[0].shortPreamble; 249 } else { 250 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 251 } 252 } else { 253 amn->amn_tx_try0 = ATH_TXMAXTRY; 254 /* theorically, these statements are useless because 255 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY 256 */ 257 amn->amn_tx_try1 = 0; 258 amn->amn_tx_try2 = 0; 259 amn->amn_tx_try3 = 0; 260 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 261 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 262 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 263 } 264 } 265 node_reset(amn); 266 267 amn->amn_interval = ath_rateinterval; 268 if (vap->iv_opmode == IEEE80211_M_STA) 269 amn->amn_interval /= 2; 270 amn->amn_interval = (amn->amn_interval * hz) / 1000; 271} 272 273/* 274 * Set the starting transmit rate for a node. 275 */ 276static void 277ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) 278{ 279#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 280 const struct ieee80211_txparam *tp = ni->ni_txparms; 281 int srate; 282 283 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); 284 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 285 /* 286 * No fixed rate is requested. For 11b start with 287 * the highest negotiated rate; otherwise, for 11g 288 * and 11a, we start "in the middle" at 24Mb or 36Mb. 289 */ 290 srate = ni->ni_rates.rs_nrates - 1; 291 if (sc->sc_curmode != IEEE80211_MODE_11B) { 292 /* 293 * Scan the negotiated rate set to find the 294 * closest rate. 295 */ 296 /* NB: the rate set is assumed sorted */ 297 for (; srate >= 0 && RATE(srate) > 72; srate--) 298 ; 299 } 300 } else { 301 /* 302 * A fixed rate is to be used; ic_fixed_rate is the 303 * IEEE code for this rate (sans basic bit). Convert this 304 * to the index into the negotiated rate set for 305 * the node. We know the rate is there because the 306 * rate set is checked when the station associates. 307 */ 308 /* NB: the rate set is assumed sorted */ 309 srate = ni->ni_rates.rs_nrates - 1; 310 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--) 311 ; 312 } 313 /* 314 * The selected rate may not be available due to races 315 * and mode settings. Also orphaned nodes created in 316 * adhoc mode may not have any rate set so this lookup 317 * can fail. This is not fatal. 318 */ 319 ath_rate_update(sc, ni, srate < 0 ? 0 : srate); 320#undef RATE 321} 322 323/* 324 * Examine and potentially adjust the transmit rate. 325 */ 326static void 327ath_rate_ctl(void *arg, struct ieee80211_node *ni) 328{ 329 struct ath_softc *sc = arg; 330 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni)); 331 int rix; 332 333#define is_success(amn) \ 334(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10)) 335#define is_enough(amn) \ 336(amn->amn_tx_try0_cnt > 10) 337#define is_failure(amn) \ 338(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3)) 339 340 rix = amn->amn_rix; 341 342 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 343 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d", 344 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt, 345 amn->amn_tx_try3_cnt, amn->amn_success_threshold); 346 if (is_success (amn) && is_enough (amn)) { 347 amn->amn_success++; 348 if (amn->amn_success == amn->amn_success_threshold && 349 rix + 1 < ni->ni_rates.rs_nrates) { 350 amn->amn_recovery = 1; 351 amn->amn_success = 0; 352 rix++; 353 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 354 "increase rate to %d", rix); 355 } else { 356 amn->amn_recovery = 0; 357 } 358 } else if (is_failure (amn)) { 359 amn->amn_success = 0; 360 if (rix > 0) { 361 if (amn->amn_recovery) { 362 /* recovery failure. */ 363 amn->amn_success_threshold *= 2; 364 amn->amn_success_threshold = min (amn->amn_success_threshold, 365 (u_int)ath_rate_max_success_threshold); 366 IEEE80211_NOTE(ni->ni_vap, 367 IEEE80211_MSG_RATECTL, ni, 368 "decrease rate recovery thr: %d", 369 amn->amn_success_threshold); 370 } else { 371 /* simple failure. */ 372 amn->amn_success_threshold = ath_rate_min_success_threshold; 373 IEEE80211_NOTE(ni->ni_vap, 374 IEEE80211_MSG_RATECTL, ni, 375 "decrease rate normal thr: %d", 376 amn->amn_success_threshold); 377 } 378 amn->amn_recovery = 0; 379 rix--; 380 } else { 381 amn->amn_recovery = 0; 382 } 383 384 } 385 if (is_enough (amn) || rix != amn->amn_rix) { 386 /* reset counters. */ 387 amn->amn_tx_try0_cnt = 0; 388 amn->amn_tx_try1_cnt = 0; 389 amn->amn_tx_try2_cnt = 0; 390 amn->amn_tx_try3_cnt = 0; 391 amn->amn_tx_failure_cnt = 0; 392 } 393 if (rix != amn->amn_rix) { 394 ath_rate_update(sc, ni, rix); 395 } 396} 397 398static void 399ath_rate_sysctlattach(struct ath_softc *sc) 400{ 401 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 402 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 403 404 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 405 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, 406 "rate control: operation interval (ms)"); 407 /* XXX bounds check values */ 408 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 409 "max_sucess_threshold", CTLFLAG_RW, 410 &ath_rate_max_success_threshold, 0, ""); 411 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 412 "min_sucess_threshold", CTLFLAG_RW, 413 &ath_rate_min_success_threshold, 0, ""); 414} 415 416struct ath_ratectrl * 417ath_rate_attach(struct ath_softc *sc) 418{ 419 struct amrr_softc *asc; 420 421 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 422 if (asc == NULL) 423 return NULL; 424 asc->arc.arc_space = sizeof(struct amrr_node); 425 ath_rate_sysctlattach(sc); 426 427 return &asc->arc; 428} 429 430void 431ath_rate_detach(struct ath_ratectrl *arc) 432{ 433 struct amrr_softc *asc = (struct amrr_softc *) arc; 434 435 free(asc, M_DEVBUF); 436} 437 438/* 439 * Module glue. 440 */ 441static int 442amrr_modevent(module_t mod, int type, void *unused) 443{ 444 switch (type) { 445 case MOD_LOAD: 446 if (bootverbose) 447 printf("ath_rate: <AMRR rate control algorithm> version 0.1\n"); 448 return 0; 449 case MOD_UNLOAD: 450 return 0; 451 } 452 return EINVAL; 453} 454 455static moduledata_t amrr_mod = { 456 "ath_rate", 457 amrr_modevent, 458 0 459}; 460DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 461MODULE_VERSION(ath_rate, 1); 462MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); 463