onoe.c revision 185522
189320Sdwhite/*- 289320Sdwhite * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting 389320Sdwhite * All rights reserved. 489320Sdwhite * 589320Sdwhite * Redistribution and use in source and binary forms, with or without 689320Sdwhite * modification, are permitted provided that the following conditions 789320Sdwhite * are met: 889320Sdwhite * 1. Redistributions of source code must retain the above copyright 989320Sdwhite * notice, this list of conditions and the following disclaimer, 1089320Sdwhite * without modification. 1189320Sdwhite * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1289320Sdwhite * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 1389320Sdwhite * redistribution must be conditioned upon including a substantially 1489320Sdwhite * similar Disclaimer requirement for further binary redistribution. 1589320Sdwhite * 1689320Sdwhite * NO WARRANTY 1789320Sdwhite * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1889320Sdwhite * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1989320Sdwhite * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 2089320Sdwhite * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2189320Sdwhite * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 2289320Sdwhite * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2389320Sdwhite * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2489320Sdwhite * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 2589320Sdwhite * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2689320Sdwhite * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 2789320Sdwhite * THE POSSIBILITY OF SUCH DAMAGES. 2892596Sru */ 2989320Sdwhite 3092596Sru#include <sys/cdefs.h> 3189320Sdwhite__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/onoe/onoe.c 185522 2008-12-01 16:53:01Z sam $"); 3292596Sru 3389320Sdwhite/* 3489320Sdwhite * Atsushi Onoe's rate control algorithm. 3589320Sdwhite */ 3689320Sdwhite#include "opt_inet.h" 3789320Sdwhite#include "opt_wlan.h" 3889320Sdwhite 3989320Sdwhite#include <sys/param.h> 4089320Sdwhite#include <sys/systm.h> 4192596Sru#include <sys/sysctl.h> 4292596Sru#include <sys/module.h> 4392596Sru#include <sys/kernel.h> 4492596Sru#include <sys/lock.h> 4592596Sru#include <sys/mutex.h> 4692596Sru#include <sys/errno.h> 4792596Sru 4892596Sru#include <machine/bus.h> 4992596Sru#include <machine/resource.h> 5092596Sru#include <sys/bus.h> 5192596Sru 5289320Sdwhite#include <sys/socket.h> 5389320Sdwhite 5489320Sdwhite#include <net/if.h> 5589320Sdwhite#include <net/if_media.h> 5692596Sru#include <net/if_arp.h> 5789320Sdwhite#include <net/ethernet.h> /* XXX for ether_sprintf */ 5892596Sru 5989320Sdwhite#include <net80211/ieee80211_var.h> 6092596Sru 61119893Sru#include <net/bpf.h> 6289320Sdwhite 6389320Sdwhite#ifdef INET 6489320Sdwhite#include <netinet/in.h> 6589320Sdwhite#include <netinet/if_ether.h> 6689320Sdwhite#endif 6789320Sdwhite 6889320Sdwhite#include <dev/ath/if_athvar.h> 6989320Sdwhite#include <dev/ath/ath_rate/onoe/onoe.h> 7089320Sdwhite#include <dev/ath/ath_hal/ah_desc.h> 7189320Sdwhite 7292596Sru/* 7389320Sdwhite * Default parameters for the rate control algorithm. These are 7492596Sru * all tunable with sysctls. The rate controller runs periodically 7589320Sdwhite * (each ath_rateinterval ms) analyzing transmit statistics for each 7692596Sru * neighbor/station (when operating in station mode this is only the AP). 7789320Sdwhite * If transmits look to be working well over a sampling period then 7892596Sru * it gives a "raise rate credit". If transmits look to not be working 7989320Sdwhite * well than it deducts a credit. If the credits cross a threshold then 8092596Sru * the transmit rate is raised. Various error conditions force the 8189320Sdwhite * the transmit rate to be dropped. 8289320Sdwhite * 8389320Sdwhite * The decision to issue/deduct a credit is based on the errors and 8489320Sdwhite * retries accumulated over the sampling period. ath_rate_raise defines 8592596Sru * the percent of retransmits for which a credit is issued/deducted. 8689320Sdwhite * ath_rate_raise_threshold defines the threshold on credits at which 8792596Sru * the transmit rate is increased. 8889320Sdwhite * 8992596Sru * XXX this algorithm is flawed. 9089320Sdwhite */ 9192596Srustatic int ath_rateinterval = 1000; /* rate ctl interval (ms) */ 9289320Sdwhitestatic int ath_rate_raise = 10; /* add credit threshold */ 9389320Sdwhitestatic int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */ 9489320Sdwhite 9589320Sdwhitestatic void ath_rate_update(struct ath_softc *, struct ieee80211_node *, 9689320Sdwhite int rate); 9789320Sdwhitestatic void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); 9889320Sdwhitestatic void ath_rate_ctl(void *, struct ieee80211_node *); 9989320Sdwhite 10089320Sdwhitevoid 10189320Sdwhiteath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 10289320Sdwhite{ 10389320Sdwhite /* NB: assumed to be zero'd by caller */ 10489320Sdwhite} 10592596Sru 10689320Sdwhitevoid 10792596Sruath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 10889320Sdwhite{ 10992596Sru} 11089320Sdwhite 11189320Sdwhitevoid 11292596Sruath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 11389320Sdwhite int shortPreamble, size_t frameLen, 11489320Sdwhite u_int8_t *rix, int *try0, u_int8_t *txrate) 11589320Sdwhite{ 11689320Sdwhite struct onoe_node *on = ATH_NODE_ONOE(an); 11789320Sdwhite 11889320Sdwhite *rix = on->on_tx_rix0; 11989320Sdwhite *try0 = on->on_tx_try0; 120 if (shortPreamble) 121 *txrate = on->on_tx_rate0sp; 122 else 123 *txrate = on->on_tx_rate0; 124} 125 126void 127ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 128 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 129{ 130 struct onoe_node *on = ATH_NODE_ONOE(an); 131 132 ath_hal_setupxtxdesc(sc->sc_ah, ds 133 , on->on_tx_rate1sp, 2 /* series 1 */ 134 , on->on_tx_rate2sp, 2 /* series 2 */ 135 , on->on_tx_rate3sp, 2 /* series 3 */ 136 ); 137} 138 139void 140ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 141 const struct ath_buf *bf) 142{ 143 struct onoe_node *on = ATH_NODE_ONOE(an); 144 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 145 146 if (ts->ts_status == 0) 147 on->on_tx_ok++; 148 else 149 on->on_tx_err++; 150 on->on_tx_retr += ts->ts_shortretry 151 + ts->ts_longretry; 152 if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) { 153 ath_rate_ctl(sc, &an->an_node); 154 on->on_ticks = ticks; 155 } 156} 157 158void 159ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 160{ 161 if (isnew) 162 ath_rate_ctl_start(sc, &an->an_node); 163} 164 165static void 166ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) 167{ 168 struct ath_node *an = ATH_NODE(ni); 169 struct onoe_node *on = ATH_NODE_ONOE(an); 170 struct ieee80211vap *vap = ni->ni_vap; 171 const HAL_RATE_TABLE *rt = sc->sc_currates; 172 u_int8_t rix; 173 174 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 175 176 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni, 177 "%s: set xmit rate to %dM", __func__, 178 ni->ni_rates.rs_nrates > 0 ? 179 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); 180 181 /* 182 * Before associating a node has no rate set setup 183 * so we can't calculate any transmit codes to use. 184 * This is ok since we should never be sending anything 185 * but management frames and those always go at the 186 * lowest hardware rate. 187 */ 188 if (ni->ni_rates.rs_nrates == 0) 189 goto done; 190 on->on_rix = rate; 191 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL; 192 on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate]; 193 on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode; 194 195 on->on_tx_rate0sp = on->on_tx_rate0 | 196 rt->info[on->on_tx_rix0].shortPreamble; 197 if (sc->sc_mrretry) { 198 /* 199 * Hardware supports multi-rate retry; setup two 200 * step-down retry rates and make the lowest rate 201 * be the ``last chance''. We use 4, 2, 2, 2 tries 202 * respectively (4 is set here, the rest are fixed 203 * in the xmit routine). 204 */ 205 on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */ 206 if (--rate >= 0) { 207 rix = sc->sc_rixmap[ 208 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 209 on->on_tx_rate1 = rt->info[rix].rateCode; 210 on->on_tx_rate1sp = on->on_tx_rate1 | 211 rt->info[rix].shortPreamble; 212 } else { 213 on->on_tx_rate1 = on->on_tx_rate1sp = 0; 214 } 215 if (--rate >= 0) { 216 rix = sc->sc_rixmap[ 217 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 218 on->on_tx_rate2 = rt->info[rix].rateCode; 219 on->on_tx_rate2sp = on->on_tx_rate2 | 220 rt->info[rix].shortPreamble; 221 } else { 222 on->on_tx_rate2 = on->on_tx_rate2sp = 0; 223 } 224 if (rate > 0) { 225 /* NB: only do this if we didn't already do it above */ 226 on->on_tx_rate3 = rt->info[0].rateCode; 227 on->on_tx_rate3sp = 228 on->on_tx_rate3 | rt->info[0].shortPreamble; 229 } else { 230 on->on_tx_rate3 = on->on_tx_rate3sp = 0; 231 } 232 } else { 233 on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */ 234 on->on_tx_rate1 = on->on_tx_rate1sp = 0; 235 on->on_tx_rate2 = on->on_tx_rate2sp = 0; 236 on->on_tx_rate3 = on->on_tx_rate3sp = 0; 237 } 238done: 239 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0; 240 241 on->on_interval = ath_rateinterval; 242 if (vap->iv_opmode == IEEE80211_M_STA) 243 on->on_interval /= 2; 244 on->on_interval = (on->on_interval * hz) / 1000; 245} 246 247/* 248 * Set the starting transmit rate for a node. 249 */ 250static void 251ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) 252{ 253#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 254 const struct ieee80211_txparam *tp = ni->ni_txparms; 255 int srate; 256 257 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); 258 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 259 /* 260 * No fixed rate is requested. For 11b start with 261 * the highest negotiated rate; otherwise, for 11g 262 * and 11a, we start "in the middle" at 24Mb or 36Mb. 263 */ 264 srate = ni->ni_rates.rs_nrates - 1; 265 if (sc->sc_curmode != IEEE80211_MODE_11B) { 266 /* 267 * Scan the negotiated rate set to find the 268 * closest rate. 269 */ 270 /* NB: the rate set is assumed sorted */ 271 for (; srate >= 0 && RATE(srate) > 72; srate--) 272 ; 273 } 274 } else { 275 /* 276 * A fixed rate is to be used; ic_fixed_rate is the 277 * IEEE code for this rate (sans basic bit). Convert this 278 * to the index into the negotiated rate set for 279 * the node. We know the rate is there because the 280 * rate set is checked when the station associates. 281 */ 282 /* NB: the rate set is assumed sorted */ 283 srate = ni->ni_rates.rs_nrates - 1; 284 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--) 285 ; 286 } 287 /* 288 * The selected rate may not be available due to races 289 * and mode settings. Also orphaned nodes created in 290 * adhoc mode may not have any rate set so this lookup 291 * can fail. This is not fatal. 292 */ 293 ath_rate_update(sc, ni, srate < 0 ? 0 : srate); 294#undef RATE 295} 296 297/* 298 * Examine and potentially adjust the transmit rate. 299 */ 300static void 301ath_rate_ctl(void *arg, struct ieee80211_node *ni) 302{ 303 struct ath_softc *sc = arg; 304 struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni)); 305 struct ieee80211_rateset *rs = &ni->ni_rates; 306 int dir = 0, nrate, enough; 307 308 /* 309 * Rate control 310 * XXX: very primitive version. 311 */ 312 enough = (on->on_tx_ok + on->on_tx_err >= 10); 313 314 /* no packet reached -> down */ 315 if (on->on_tx_err > 0 && on->on_tx_ok == 0) 316 dir = -1; 317 318 /* all packets needs retry in average -> down */ 319 if (enough && on->on_tx_ok < on->on_tx_retr) 320 dir = -1; 321 322 /* no error and less than rate_raise% of packets need retry -> up */ 323 if (enough && on->on_tx_err == 0 && 324 on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100) 325 dir = 1; 326 327 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 328 "ok %d err %d retr %d upper %d dir %d", 329 on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir); 330 331 nrate = on->on_rix; 332 switch (dir) { 333 case 0: 334 if (enough && on->on_tx_upper > 0) 335 on->on_tx_upper--; 336 break; 337 case -1: 338 if (nrate > 0) { 339 nrate--; 340 sc->sc_stats.ast_rate_drop++; 341 } 342 on->on_tx_upper = 0; 343 break; 344 case 1: 345 /* raise rate if we hit rate_raise_threshold */ 346 if (++on->on_tx_upper < ath_rate_raise_threshold) 347 break; 348 on->on_tx_upper = 0; 349 if (nrate + 1 < rs->rs_nrates) { 350 nrate++; 351 sc->sc_stats.ast_rate_raise++; 352 } 353 break; 354 } 355 356 if (nrate != on->on_rix) { 357 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 358 "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__, 359 ni->ni_txrate / 2, 360 (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2, 361 on->on_tx_ok, on->on_tx_err, on->on_tx_retr); 362 ath_rate_update(sc, ni, nrate); 363 } else if (enough) 364 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0; 365} 366 367static void 368ath_rate_sysctlattach(struct ath_softc *sc) 369{ 370 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 371 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 372 373 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 374 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, 375 "rate control: operation interval (ms)"); 376 /* XXX bounds check values */ 377 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 378 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0, 379 "rate control: retry threshold to credit rate raise (%%)"); 380 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 381 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0, 382 "rate control: # good periods before raising rate"); 383} 384 385struct ath_ratectrl * 386ath_rate_attach(struct ath_softc *sc) 387{ 388 struct onoe_softc *osc; 389 390 osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 391 if (osc == NULL) 392 return NULL; 393 osc->arc.arc_space = sizeof(struct onoe_node); 394 ath_rate_sysctlattach(sc); 395 396 return &osc->arc; 397} 398 399void 400ath_rate_detach(struct ath_ratectrl *arc) 401{ 402 struct onoe_softc *osc = (struct onoe_softc *) arc; 403 404 free(osc, M_DEVBUF); 405} 406 407/* 408 * Module glue. 409 */ 410static int 411onoe_modevent(module_t mod, int type, void *unused) 412{ 413 switch (type) { 414 case MOD_LOAD: 415 if (bootverbose) 416 printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n"); 417 return 0; 418 case MOD_UNLOAD: 419 return 0; 420 } 421 return EINVAL; 422} 423 424static moduledata_t onoe_mod = { 425 "ath_rate", 426 onoe_modevent, 427 0 428}; 429DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 430MODULE_VERSION(ath_rate, 1); 431MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); 432