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