1 /*- 2 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Atsushi Onoe's rate control algorithm. 35 */ 36 #include "opt_ath.h" 37 #include "opt_inet.h" 38 #include "opt_wlan.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/sysctl.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/errno.h> 47 48 #include <sys/bus.h> 49 50 #include <sys/socket.h> 51 52 #include <net/if.h> 53 #include <net/if_media.h> 54 #include <net/if_arp.h> 55 #include <net/ethernet.h> /* XXX for ether_sprintf */ 56 57 #include <netproto/802_11/ieee80211_var.h> 58 59 #include <net/bpf.h> 60 61 #ifdef INET 62 #include <netinet/in.h> 63 #include <netinet/if_ether.h> 64 #endif 65 66 #include <dev/netif/ath/ath/if_athvar.h> 67 #include <dev/netif/ath/ath_rate/onoe/onoe.h> 68 #include <dev/netif/ath/ath_hal/ah_desc.h> 69 70 /* 71 * Default parameters for the rate control algorithm. These are 72 * all tunable with sysctls. The rate controller runs periodically 73 * (each ath_rateinterval ms) analyzing transmit statistics for each 74 * neighbor/station (when operating in station mode this is only the AP). 75 * If transmits look to be working well over a sampling period then 76 * it gives a "raise rate credit". If transmits look to not be working 77 * well than it deducts a credit. If the credits cross a threshold then 78 * the transmit rate is raised. Various error conditions force the 79 * the transmit rate to be dropped. 80 * 81 * The decision to issue/deduct a credit is based on the errors and 82 * retries accumulated over the sampling period. ath_rate_raise defines 83 * the percent of retransmits for which a credit is issued/deducted. 84 * ath_rate_raise_threshold defines the threshold on credits at which 85 * the transmit rate is increased. 86 * 87 * XXX this algorithm is flawed. 88 */ 89 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ 90 static int ath_rate_raise = 10; /* add credit threshold */ 91 static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */ 92 93 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, 94 int rate); 95 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); 96 static void ath_rate_ctl(void *, struct ieee80211_node *); 97 98 void 99 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 100 { 101 /* NB: assumed to be zero'd by caller */ 102 } 103 104 void 105 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 106 { 107 } 108 109 void 110 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 111 int shortPreamble, size_t frameLen, 112 u_int8_t *rix, int *try0, u_int8_t *txrate) 113 { 114 struct onoe_node *on = ATH_NODE_ONOE(an); 115 116 *rix = on->on_tx_rix0; 117 *try0 = on->on_tx_try0; 118 if (shortPreamble) 119 *txrate = on->on_tx_rate0sp; 120 else 121 *txrate = on->on_tx_rate0; 122 } 123 124 /* 125 * Get the TX rates. 126 * 127 * The short preamble bits aren't set here; the caller should augment 128 * the returned rate with the relevant preamble rate flag. 129 */ 130 void 131 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an, 132 uint8_t rix0, struct ath_rc_series *rc) 133 { 134 struct onoe_node *on = ATH_NODE_ONOE(an); 135 136 rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0; 137 138 rc[0].rix = on->on_tx_rate0; 139 rc[1].rix = on->on_tx_rate1; 140 rc[2].rix = on->on_tx_rate2; 141 rc[3].rix = on->on_tx_rate3; 142 143 rc[0].tries = on->on_tx_try0; 144 rc[1].tries = 2; 145 rc[2].tries = 2; 146 rc[3].tries = 2; 147 } 148 149 void 150 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 151 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 152 { 153 struct onoe_node *on = ATH_NODE_ONOE(an); 154 155 ath_hal_setupxtxdesc(sc->sc_ah, ds 156 , on->on_tx_rate1sp, 2 /* series 1 */ 157 , on->on_tx_rate2sp, 2 /* series 2 */ 158 , on->on_tx_rate3sp, 2 /* series 3 */ 159 ); 160 } 161 162 void 163 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 164 const struct ath_rc_series *rc, const struct ath_tx_status *ts, 165 int frame_size, int nframes, int nbad) 166 { 167 struct onoe_node *on = ATH_NODE_ONOE(an); 168 169 if (ts->ts_status == 0) 170 on->on_tx_ok++; 171 else 172 on->on_tx_err++; 173 on->on_tx_retr += ts->ts_shortretry 174 + ts->ts_longretry; 175 if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) { 176 ath_rate_ctl(sc, &an->an_node); 177 on->on_ticks = ticks; 178 } 179 } 180 181 void 182 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 183 { 184 if (isnew) 185 ath_rate_ctl_start(sc, &an->an_node); 186 } 187 188 static void 189 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) 190 { 191 struct ath_node *an = ATH_NODE(ni); 192 struct onoe_node *on = ATH_NODE_ONOE(an); 193 struct ieee80211vap *vap = ni->ni_vap; 194 const HAL_RATE_TABLE *rt = sc->sc_currates; 195 u_int8_t rix; 196 197 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 198 199 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni, 200 "%s: set xmit rate to %dM", __func__, 201 ni->ni_rates.rs_nrates > 0 ? 202 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); 203 204 /* 205 * Before associating a node has no rate set setup 206 * so we can't calculate any transmit codes to use. 207 * This is ok since we should never be sending anything 208 * but management frames and those always go at the 209 * lowest hardware rate. 210 */ 211 if (ni->ni_rates.rs_nrates == 0) 212 goto done; 213 on->on_rix = rate; 214 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL; 215 on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate]; 216 on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode; 217 218 on->on_tx_rate0sp = on->on_tx_rate0 | 219 rt->info[on->on_tx_rix0].shortPreamble; 220 if (sc->sc_mrretry) { 221 /* 222 * Hardware supports multi-rate retry; setup two 223 * step-down retry rates and make the lowest rate 224 * be the ``last chance''. We use 4, 2, 2, 2 tries 225 * respectively (4 is set here, the rest are fixed 226 * in the xmit routine). 227 */ 228 on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */ 229 if (--rate >= 0) { 230 rix = sc->sc_rixmap[ 231 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 232 on->on_tx_rate1 = rt->info[rix].rateCode; 233 on->on_tx_rate1sp = on->on_tx_rate1 | 234 rt->info[rix].shortPreamble; 235 } else { 236 on->on_tx_rate1 = on->on_tx_rate1sp = 0; 237 } 238 if (--rate >= 0) { 239 rix = sc->sc_rixmap[ 240 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 241 on->on_tx_rate2 = rt->info[rix].rateCode; 242 on->on_tx_rate2sp = on->on_tx_rate2 | 243 rt->info[rix].shortPreamble; 244 } else { 245 on->on_tx_rate2 = on->on_tx_rate2sp = 0; 246 } 247 if (rate > 0) { 248 /* NB: only do this if we didn't already do it above */ 249 on->on_tx_rate3 = rt->info[0].rateCode; 250 on->on_tx_rate3sp = 251 on->on_tx_rate3 | rt->info[0].shortPreamble; 252 } else { 253 on->on_tx_rate3 = on->on_tx_rate3sp = 0; 254 } 255 } else { 256 on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */ 257 on->on_tx_rate1 = on->on_tx_rate1sp = 0; 258 on->on_tx_rate2 = on->on_tx_rate2sp = 0; 259 on->on_tx_rate3 = on->on_tx_rate3sp = 0; 260 } 261 done: 262 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0; 263 264 on->on_interval = ath_rateinterval; 265 if (vap->iv_opmode == IEEE80211_M_STA) 266 on->on_interval /= 2; 267 on->on_interval = (on->on_interval * hz) / 1000; 268 } 269 270 /* 271 * Set the starting transmit rate for a node. 272 */ 273 static void 274 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) 275 { 276 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 277 const struct ieee80211_txparam *tp = ni->ni_txparms; 278 int srate; 279 280 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); 281 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 282 /* 283 * No fixed rate is requested. For 11b start with 284 * the highest negotiated rate; otherwise, for 11g 285 * and 11a, we start "in the middle" at 24Mb or 36Mb. 286 */ 287 srate = ni->ni_rates.rs_nrates - 1; 288 if (sc->sc_curmode != IEEE80211_MODE_11B) { 289 /* 290 * Scan the negotiated rate set to find the 291 * closest rate. 292 */ 293 /* NB: the rate set is assumed sorted */ 294 for (; srate >= 0 && RATE(srate) > 72; srate--) 295 ; 296 } 297 } else { 298 /* 299 * A fixed rate is to be used; ic_fixed_rate is the 300 * IEEE code for this rate (sans basic bit). Convert this 301 * to the index into the negotiated rate set for 302 * the node. We know the rate is there because the 303 * rate set is checked when the station associates. 304 */ 305 /* NB: the rate set is assumed sorted */ 306 srate = ni->ni_rates.rs_nrates - 1; 307 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--) 308 ; 309 } 310 /* 311 * The selected rate may not be available due to races 312 * and mode settings. Also orphaned nodes created in 313 * adhoc mode may not have any rate set so this lookup 314 * can fail. This is not fatal. 315 */ 316 ath_rate_update(sc, ni, srate < 0 ? 0 : srate); 317 #undef RATE 318 } 319 320 /* 321 * Examine and potentially adjust the transmit rate. 322 */ 323 static void 324 ath_rate_ctl(void *arg, struct ieee80211_node *ni) 325 { 326 struct ath_softc *sc = arg; 327 struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni)); 328 struct ieee80211_rateset *rs = &ni->ni_rates; 329 int dir = 0, nrate, enough; 330 331 /* 332 * Rate control 333 * XXX: very primitive version. 334 */ 335 enough = (on->on_tx_ok + on->on_tx_err >= 10); 336 337 /* no packet reached -> down */ 338 if (on->on_tx_err > 0 && on->on_tx_ok == 0) 339 dir = -1; 340 341 /* all packets needs retry in average -> down */ 342 if (enough && on->on_tx_ok < on->on_tx_retr) 343 dir = -1; 344 345 /* no error and less than rate_raise% of packets need retry -> up */ 346 if (enough && on->on_tx_err == 0 && 347 on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100) 348 dir = 1; 349 350 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 351 "ok %d err %d retr %d upper %d dir %d", 352 on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir); 353 354 nrate = on->on_rix; 355 switch (dir) { 356 case 0: 357 if (enough && on->on_tx_upper > 0) 358 on->on_tx_upper--; 359 break; 360 case -1: 361 if (nrate > 0) { 362 nrate--; 363 sc->sc_stats.ast_rate_drop++; 364 } 365 on->on_tx_upper = 0; 366 break; 367 case 1: 368 /* raise rate if we hit rate_raise_threshold */ 369 if (++on->on_tx_upper < ath_rate_raise_threshold) 370 break; 371 on->on_tx_upper = 0; 372 if (nrate + 1 < rs->rs_nrates) { 373 nrate++; 374 sc->sc_stats.ast_rate_raise++; 375 } 376 break; 377 } 378 379 if (nrate != on->on_rix) { 380 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 381 "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__, 382 ni->ni_txrate / 2, 383 (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2, 384 on->on_tx_ok, on->on_tx_err, on->on_tx_retr); 385 ath_rate_update(sc, ni, nrate); 386 } else if (enough) 387 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0; 388 } 389 390 static void 391 ath_rate_sysctlattach(struct ath_softc *sc) 392 { 393 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 394 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 395 396 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 397 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, 398 "rate control: operation interval (ms)"); 399 /* XXX bounds check values */ 400 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 401 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0, 402 "rate control: retry threshold to credit rate raise (%%)"); 403 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 404 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0, 405 "rate control: # good periods before raising rate"); 406 } 407 408 static int 409 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an, 410 struct ath_rateioctl *re) 411 { 412 413 return (EINVAL); 414 } 415 416 struct ath_ratectrl * 417 ath_rate_attach(struct ath_softc *sc) 418 { 419 struct onoe_softc *osc; 420 421 osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_INTWAIT|M_ZERO); 422 if (osc == NULL) 423 return NULL; 424 osc->arc.arc_space = sizeof(struct onoe_node); 425 ath_rate_sysctlattach(sc); 426 427 return &osc->arc; 428 } 429 430 void 431 ath_rate_detach(struct ath_ratectrl *arc) 432 { 433 struct onoe_softc *osc = (struct onoe_softc *) arc; 434 435 kfree(osc, M_DEVBUF); 436 } 437 438 /* 439 * Module glue. 440 */ 441 static int 442 onoe_modevent(module_t mod, int type, void *unused) 443 { 444 int error; 445 446 wlan_serialize_enter(); 447 448 switch (type) { 449 case MOD_LOAD: 450 if (bootverbose) { 451 kprintf("ath_rate: <Atsushi Onoe's rate " 452 "control algorithm>\n"); 453 } 454 error = 0; 455 break; 456 case MOD_UNLOAD: 457 error = 0; 458 break; 459 default: 460 error = EINVAL; 461 break; 462 } 463 wlan_serialize_exit(); 464 465 return error; 466 } 467 468 static moduledata_t onoe_mod = { 469 "ath_rate", 470 onoe_modevent, 471 0 472 }; 473 474 DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 475 MODULE_VERSION(ath_rate, 1); 476 MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1); 477 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); 478