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