Home | History | Annotate | Line # | Download | only in ic
athrate-onoe.c revision 1.7
      1 /*	$NetBSD: athrate-onoe.c,v 1.7 2006/02/05 06:03:26 xtraeme Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer,
     12  *    without modification.
     13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     15  *    redistribution must be conditioned upon including a substantially
     16  *    similar Disclaimer requirement for further binary redistribution.
     17  * 3. Neither the names of the above-listed copyright holders nor the names
     18  *    of any contributors may be used to endorse or promote products derived
     19  *    from this software without specific prior written permission.
     20  *
     21  * Alternatively, this software may be distributed under the terms of the
     22  * GNU General Public License ("GPL") version 2 as published by the Free
     23  * Software Foundation.
     24  *
     25  * NO WARRANTY
     26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     36  * THE POSSIBILITY OF SUCH DAMAGES.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifdef __FreeBSD__
     41 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
     42 #endif
     43 #ifdef __NetBSD__
     44 __KERNEL_RCSID(0, "$NetBSD: athrate-onoe.c,v 1.7 2006/02/05 06:03:26 xtraeme Exp $");
     45 #endif
     46 
     47 /*
     48  * Atsushi Onoe's rate control algorithm.
     49  */
     50 #include "opt_inet.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/sysctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/lock.h>
     57 #include <sys/errno.h>
     58 #include <sys/device.h>
     59 
     60 #include <machine/bus.h>
     61 
     62 #include <sys/socket.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_media.h>
     66 #include <net/if_arp.h>
     67 #include <net/if_ether.h>		/* XXX for ether_sprintf */
     68 
     69 #include <net80211/ieee80211_var.h>
     70 
     71 #include <net/bpf.h>
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #endif
     76 
     77 #include <dev/ic/ath_netbsd.h>
     78 #include <dev/ic/athvar.h>
     79 #include <dev/ic/athrate-onoe.h>
     80 #include <contrib/dev/ic/athhal_desc.h>
     81 
     82 #define	ONOE_DEBUG
     83 #ifdef ONOE_DEBUG
     84 enum {
     85 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
     86 };
     87 #define	DPRINTF(sc, _fmt, ...) do {				\
     88 	if (sc->sc_debug & ATH_DEBUG_RATE)			\
     89 		printf(_fmt, __VA_ARGS__);			\
     90 } while (0)
     91 #else
     92 #define	DPRINTF(sc, _fmt, ...)
     93 #endif
     94 
     95 /*
     96  * Default parameters for the rate control algorithm.  These are
     97  * all tunable with sysctls.  The rate controller runs periodically
     98  * (each ath_rateinterval ms) analyzing transmit statistics for each
     99  * neighbor/station (when operating in station mode this is only the AP).
    100  * If transmits look to be working well over a sampling period then
    101  * it gives a "raise rate credit".  If transmits look to not be working
    102  * well than it deducts a credit.  If the credits cross a threshold then
    103  * the transmit rate is raised.  Various error conditions force the
    104  * the transmit rate to be dropped.
    105  *
    106  * The decision to issue/deduct a credit is based on the errors and
    107  * retries accumulated over the sampling period.  ath_rate_raise defines
    108  * the percent of retransmits for which a credit is issued/deducted.
    109  * ath_rate_raise_threshold defines the threshold on credits at which
    110  * the transmit rate is increased.
    111  *
    112  * XXX this algorithm is flawed.
    113  */
    114 static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
    115 static	int ath_rate_raise = 10;		/* add credit threshold */
    116 static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
    117 
    118 static void	ath_ratectl(void *);
    119 static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
    120 			int rate);
    121 static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
    122 static void	ath_rate_ctl(void *, struct ieee80211_node *);
    123 
    124 void
    125 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
    126 {
    127 	/* NB: assumed to be zero'd by caller */
    128 	ath_rate_update(sc, &an->an_node, 0);
    129 }
    130 
    131 void
    132 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
    133 {
    134 }
    135 
    136 void
    137 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
    138 	int shortPreamble, size_t frameLen,
    139 	u_int8_t *rix, int *try0, u_int8_t *txrate)
    140 {
    141 	struct onoe_node *on = ATH_NODE_ONOE(an);
    142 
    143 	*rix = on->on_tx_rix0;
    144 	*try0 = on->on_tx_try0;
    145 	if (shortPreamble)
    146 		*txrate = on->on_tx_rate0sp;
    147 	else
    148 		*txrate = on->on_tx_rate0;
    149 }
    150 
    151 void
    152 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
    153 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
    154 {
    155 	struct onoe_node *on = ATH_NODE_ONOE(an);
    156 
    157 	ath_hal_setupxtxdesc(sc->sc_ah, ds
    158 		, on->on_tx_rate1sp, 2	/* series 1 */
    159 		, on->on_tx_rate2sp, 2	/* series 2 */
    160 		, on->on_tx_rate3sp, 2	/* series 3 */
    161 	);
    162 }
    163 
    164 void
    165 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
    166 	const struct ath_desc *ds, const struct ath_desc *ds0)
    167 {
    168 	struct onoe_node *on = ATH_NODE_ONOE(an);
    169 
    170 	if (ds->ds_txstat.ts_status == 0)
    171 		on->on_tx_ok++;
    172 	else
    173 		on->on_tx_err++;
    174 	on->on_tx_retr += ds->ds_txstat.ts_shortretry
    175 			+ ds->ds_txstat.ts_longretry;
    176 }
    177 
    178 void
    179 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
    180 {
    181 	if (isnew)
    182 		ath_rate_ctl_start(sc, &an->an_node);
    183 }
    184 
    185 static void
    186 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
    187 {
    188 	struct ath_node *an = ATH_NODE(ni);
    189 	struct onoe_node *on = ATH_NODE_ONOE(an);
    190 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    191 	u_int8_t rix;
    192 
    193 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
    194 
    195 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
    196 	    __func__, ether_sprintf(ni->ni_macaddr),
    197 	    ni->ni_rates.rs_nrates > 0 ?
    198 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
    199 
    200 	ni->ni_txrate = rate;
    201 	/* XXX management/control frames always go at the lowest speed */
    202 	an->an_tx_mgtrate = rt->info[0].rateCode;
    203 	an->an_tx_mgtratesp = an->an_tx_mgtrate | rt->info[0].shortPreamble;
    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_tx_rix0 = sc->sc_rixmap[
    214 		ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
    215 	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
    216 
    217 	on->on_tx_rate0sp = on->on_tx_rate0 |
    218 		rt->info[on->on_tx_rix0].shortPreamble;
    219 	if (sc->sc_mrretry) {
    220 		/*
    221 		 * Hardware supports multi-rate retry; setup two
    222 		 * step-down retry rates and make the lowest rate
    223 		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
    224 		 * respectively (4 is set here, the rest are fixed
    225 		 * in the xmit routine).
    226 		 */
    227 		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
    228 		if (--rate >= 0) {
    229 			rix = sc->sc_rixmap[
    230 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    231 			on->on_tx_rate1 = rt->info[rix].rateCode;
    232 			on->on_tx_rate1sp = on->on_tx_rate1 |
    233 				rt->info[rix].shortPreamble;
    234 		} else {
    235 			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
    236 		}
    237 		if (--rate >= 0) {
    238 			rix = sc->sc_rixmap[
    239 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    240 			on->on_tx_rate2 = rt->info[rix].rateCode;
    241 			on->on_tx_rate2sp = on->on_tx_rate2 |
    242 				rt->info[rix].shortPreamble;
    243 		} else {
    244 			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
    245 		}
    246 		if (rate > 0) {
    247 			/* NB: only do this if we didn't already do it above */
    248 			on->on_tx_rate3 = rt->info[0].rateCode;
    249 			on->on_tx_rate3sp =
    250 				an->an_tx_mgtrate | rt->info[0].shortPreamble;
    251 		} else {
    252 			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
    253 		}
    254 	} else {
    255 		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
    256 		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
    257 		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
    258 		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
    259 	}
    260 done:
    261 	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
    262 }
    263 
    264 /*
    265  * Set the starting transmit rate for a node.
    266  */
    267 static void
    268 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
    269 {
    270 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
    271 	struct ieee80211com *ic = &sc->sc_ic;
    272 	int srate;
    273 
    274 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
    275 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
    276 		/*
    277 		 * No fixed rate is requested. For 11b start with
    278 		 * the highest negotiated rate; otherwise, for 11g
    279 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
    280 		 */
    281 		srate = ni->ni_rates.rs_nrates - 1;
    282 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
    283 			/*
    284 			 * Scan the negotiated rate set to find the
    285 			 * closest rate.
    286 			 */
    287 			/* NB: the rate set is assumed sorted */
    288 			for (; srate >= 0 && RATE(srate) > 72; srate--)
    289 				;
    290 			KASSERT(srate >= 0, ("bogus rate set"));
    291 		}
    292 	} else {
    293 		/*
    294 		 * A fixed rate is to be used; ic_fixed_rate is an
    295 		 * index into the supported rate set.  Convert this
    296 		 * to the index into the negotiated rate set for
    297 		 * the node.  We know the rate is there because the
    298 		 * rate set is checked when the station associates.
    299 		 */
    300 		const struct ieee80211_rateset *rs =
    301 			&ic->ic_sup_rates[ic->ic_curmode];
    302 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    303 		/* NB: the rate set is assumed sorted */
    304 		srate = ni->ni_rates.rs_nrates - 1;
    305 		for (; srate >= 0 && RATE(srate) != r; srate--)
    306 			;
    307 		KASSERT(srate >= 0,
    308 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
    309 	}
    310 	ath_rate_update(sc, ni, srate);
    311 #undef RATE
    312 }
    313 
    314 static void
    315 ath_rate_cb(void *arg, struct ieee80211_node *ni)
    316 {
    317 	struct ath_softc *sc = arg;
    318 
    319 	ath_rate_update(sc, ni, 0);
    320 }
    321 
    322 /*
    323  * Reset the rate control state for each 802.11 state transition.
    324  */
    325 void
    326 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
    327 {
    328 	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
    329 	struct ieee80211com *ic = &sc->sc_ic;
    330 	struct ieee80211_node *ni;
    331 
    332 	if (state == IEEE80211_S_INIT) {
    333 		callout_stop(&osc->timer);
    334 		return;
    335 	}
    336 	if (ic->ic_opmode == IEEE80211_M_STA) {
    337 		/*
    338 		 * Reset local xmit state; this is really only
    339 		 * meaningful when operating in station mode.
    340 		 */
    341 		ni = ic->ic_bss;
    342 		if (state == IEEE80211_S_RUN) {
    343 			ath_rate_ctl_start(sc, ni);
    344 		} else {
    345 			ath_rate_update(sc, ni, 0);
    346 		}
    347 	} else {
    348 		/*
    349 		 * When operating as a station the node table holds
    350 		 * the AP's that were discovered during scanning.
    351 		 * For any other operating mode we want to reset the
    352 		 * tx rate state of each node.
    353 		 */
    354 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
    355 		ath_rate_update(sc, ic->ic_bss, 0);
    356 	}
    357 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
    358 	    state == IEEE80211_S_RUN) {
    359 		int interval;
    360 		/*
    361 		 * Start the background rate control thread if we
    362 		 * are not configured to use a fixed xmit rate.
    363 		 */
    364 		interval = ath_rateinterval;
    365 		if (ic->ic_opmode == IEEE80211_M_STA)
    366 			interval /= 2;
    367 		callout_reset(&osc->timer, (interval * hz) / 1000,
    368 			ath_ratectl, &sc->sc_if);
    369 	}
    370 }
    371 
    372 /*
    373  * Examine and potentially adjust the transmit rate.
    374  */
    375 static void
    376 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
    377 {
    378 	struct ath_softc *sc = arg;
    379 	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
    380 	struct ieee80211_rateset *rs = &ni->ni_rates;
    381 	int dir = 0, nrate, enough;
    382 
    383 	/*
    384 	 * Rate control
    385 	 * XXX: very primitive version.
    386 	 */
    387 	enough = (on->on_tx_ok + on->on_tx_err >= 10);
    388 
    389 	/* no packet reached -> down */
    390 	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
    391 		dir = -1;
    392 
    393 	/* all packets needs retry in average -> down */
    394 	if (enough && on->on_tx_ok < on->on_tx_retr)
    395 		dir = -1;
    396 
    397 	/* no error and less than rate_raise% of packets need retry -> up */
    398 	if (enough && on->on_tx_err == 0 &&
    399 	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
    400 		dir = 1;
    401 
    402 	DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
    403 		ether_sprintf(ni->ni_macaddr),
    404 		on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
    405 		on->on_tx_upper, dir);
    406 
    407 	nrate = ni->ni_txrate;
    408 	switch (dir) {
    409 	case 0:
    410 		if (enough && on->on_tx_upper > 0)
    411 			on->on_tx_upper--;
    412 		break;
    413 	case -1:
    414 		if (nrate > 0) {
    415 			nrate--;
    416 			sc->sc_stats.ast_rate_drop++;
    417 		}
    418 		on->on_tx_upper = 0;
    419 		break;
    420 	case 1:
    421 		/* raise rate if we hit rate_raise_threshold */
    422 		if (++on->on_tx_upper < ath_rate_raise_threshold)
    423 			break;
    424 		on->on_tx_upper = 0;
    425 		if (nrate + 1 < rs->rs_nrates) {
    426 			nrate++;
    427 			sc->sc_stats.ast_rate_raise++;
    428 		}
    429 		break;
    430 	}
    431 
    432 	if (nrate != ni->ni_txrate) {
    433 		DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
    434 		    __func__,
    435 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
    436 		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
    437 		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
    438 		ath_rate_update(sc, ni, nrate);
    439 	} else if (enough)
    440 		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
    441 }
    442 
    443 static void
    444 ath_ratectl(void *arg)
    445 {
    446 	struct ifnet *ifp = arg;
    447 	struct ath_softc *sc = ifp->if_softc;
    448 	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
    449 	struct ieee80211com *ic = &sc->sc_ic;
    450 	int interval;
    451 
    452 	if (ifp->if_flags & IFF_RUNNING) {
    453 		sc->sc_stats.ast_rate_calls++;
    454 
    455 		if (ic->ic_opmode == IEEE80211_M_STA)
    456 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
    457 		else
    458 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
    459 	}
    460 	interval = ath_rateinterval;
    461 	if (ic->ic_opmode == IEEE80211_M_STA)
    462 		interval /= 2;
    463 	callout_reset(&osc->timer, (interval * hz) / 1000,
    464 		ath_ratectl, &sc->sc_if);
    465 }
    466 
    467 static void
    468 ath_rate_sysctlattach(struct ath_softc *sc)
    469 {
    470 	struct sysctllog **clog = &sc->sc_sysctllog;
    471 	const struct sysctlnode *cnode, *rnode;
    472 
    473 	if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
    474 		return;
    475 
    476 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_interval",
    477 	    "rate control: operation interval (ms)", rateinterval);
    478 	/* XXX bounds check values */
    479 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise",
    480 	    "rate control: retry threshold to credit rate raise (%%)",
    481 	    rate_raise);
    482 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise_threshold",
    483 	    "rate control: # good periods before raising rate",
    484 	    rate_raise_threshold);
    485 }
    486 
    487 struct ath_ratectrl *
    488 ath_rate_attach(struct ath_softc *sc)
    489 {
    490 	struct onoe_softc *osc;
    491 
    492 	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
    493 	if (osc == NULL)
    494 		return NULL;
    495 	osc->arc.arc_space = sizeof(struct onoe_node);
    496 	ATH_CALLOUT_INIT(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
    497 	ath_rate_sysctlattach(sc);
    498 
    499 	return &osc->arc;
    500 }
    501 
    502 void
    503 ath_rate_detach(struct ath_ratectrl *arc)
    504 {
    505 	struct onoe_softc *osc = (struct onoe_softc *) arc;
    506 
    507 	callout_stop(&osc->timer);
    508 	free(osc, M_DEVBUF);
    509 }
    510