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