Home | History | Annotate | Line # | Download | only in ic
athrate-amrr.c revision 1.9.8.1
      1 /*	$NetBSD: athrate-amrr.c,v 1.9.8.1 2008/01/08 22:11:02 bouyer Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004 INRIA
      5  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer,
     13  *    without modification.
     14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     15  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     16  *    redistribution must be conditioned upon including a substantially
     17  *    similar Disclaimer requirement for further binary redistribution.
     18  * 3. Neither the names of the above-listed copyright holders nor the names
     19  *    of any contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * Alternatively, this software may be distributed under the terms of the
     23  * GNU General Public License ("GPL") version 2 as published by the Free
     24  * Software Foundation.
     25  *
     26  * NO WARRANTY
     27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     29  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     30  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     31  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     32  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     35  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     37  * THE POSSIBILITY OF SUCH DAMAGES.
     38  *
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #ifdef __FreeBSD__
     43 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
     44 #endif
     45 #ifdef __NetBSD__
     46 __KERNEL_RCSID(0, "$NetBSD: athrate-amrr.c,v 1.9.8.1 2008/01/08 22:11:02 bouyer Exp $");
     47 #endif
     48 
     49 /*
     50  * AMRR rate control. See:
     51  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
     52  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
     53  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
     54  */
     55 #include "opt_inet.h"
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/kernel.h>
     61 #include <sys/errno.h>
     62 #include <sys/bus.h>
     63 #include <sys/socket.h>
     64 
     65 #include <net/if.h>
     66 #include <net/if_media.h>
     67 #include <net/if_arp.h>
     68 #include <net/if_ether.h>		/* XXX for ether_sprintf */
     69 
     70 #include <net80211/ieee80211_var.h>
     71 
     72 #include <net/bpf.h>
     73 
     74 #ifdef INET
     75 #include <netinet/in.h>
     76 #endif
     77 
     78 #include <dev/ic/athvar.h>
     79 #include <dev/ic/athrate-amrr.h>
     80 #include <contrib/dev/ath/ah_desc.h>
     81 
     82 #define	AMRR_DEBUG
     83 #ifdef AMRR_DEBUG
     84 #define	DPRINTF(sc, _fmt, ...) do {					\
     85 	if (sc->sc_debug & 0x10)					\
     86 		printf(_fmt, __VA_ARGS__);				\
     87 } while (0)
     88 #else
     89 #define	DPRINTF(sc, _fmt, ...)
     90 #endif
     91 
     92 static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
     93 static	int ath_rate_max_success_threshold = 10;
     94 static	int ath_rate_min_success_threshold = 1;
     95 
     96 static void	ath_ratectl(void *);
     97 static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
     98 			int rate);
     99 static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
    100 static void	ath_rate_ctl(void *, struct ieee80211_node *);
    101 
    102 void
    103 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
    104 {
    105 	/* NB: assumed to be zero'd by caller */
    106 	ath_rate_update(sc, &an->an_node, 0);
    107 }
    108 
    109 void
    110 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
    111 {
    112 }
    113 
    114 void
    115 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
    116 	int shortPreamble, size_t frameLen,
    117 	u_int8_t *rix, int *try0, u_int8_t *txrate)
    118 {
    119 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    120 
    121 	*rix = amn->amn_tx_rix0;
    122 	*try0 = amn->amn_tx_try0;
    123 	if (shortPreamble)
    124 		*txrate = amn->amn_tx_rate0sp;
    125 	else
    126 		*txrate = amn->amn_tx_rate0;
    127 }
    128 
    129 void
    130 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
    131 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
    132 {
    133 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    134 
    135 	ath_hal_setupxtxdesc(sc->sc_ah, ds
    136 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
    137 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
    138 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
    139 	);
    140 }
    141 
    142 void
    143 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
    144 	const struct ath_desc *ds, const struct ath_desc *ds0)
    145 {
    146 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    147 	int sr = ds->ds_txstat.ts_shortretry;
    148 	int lr = ds->ds_txstat.ts_longretry;
    149 	int retry_count = sr + lr;
    150 
    151 	amn->amn_tx_try0_cnt++;
    152 	if (retry_count == 1) {
    153 		amn->amn_tx_try1_cnt++;
    154 	} else if (retry_count == 2) {
    155 		amn->amn_tx_try1_cnt++;
    156 		amn->amn_tx_try2_cnt++;
    157 	} else if (retry_count == 3) {
    158 		amn->amn_tx_try1_cnt++;
    159 		amn->amn_tx_try2_cnt++;
    160 		amn->amn_tx_try3_cnt++;
    161 	} else if (retry_count > 3) {
    162 		amn->amn_tx_try1_cnt++;
    163 		amn->amn_tx_try2_cnt++;
    164 		amn->amn_tx_try3_cnt++;
    165 		amn->amn_tx_failure_cnt++;
    166 	}
    167 }
    168 
    169 void
    170 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
    171 {
    172 	if (isnew)
    173 		ath_rate_ctl_start(sc, &an->an_node);
    174 }
    175 
    176 static void
    177 node_reset (struct amrr_node *amn)
    178 {
    179 	amn->amn_tx_try0_cnt = 0;
    180 	amn->amn_tx_try1_cnt = 0;
    181 	amn->amn_tx_try2_cnt = 0;
    182 	amn->amn_tx_try3_cnt = 0;
    183 	amn->amn_tx_failure_cnt = 0;
    184   	amn->amn_success = 0;
    185   	amn->amn_recovery = 0;
    186   	amn->amn_success_threshold = ath_rate_min_success_threshold;
    187 }
    188 
    189 
    190 /**
    191  * The code below assumes that we are dealing with hardware multi rate retry
    192  * I have no idea what will happen if you try to use this module with another
    193  * type of hardware. Your machine might catch fire or it might work with
    194  * horrible performance...
    195  */
    196 static void
    197 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
    198 {
    199 	struct ath_node *an = ATH_NODE(ni);
    200 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    201 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    202 	u_int8_t rix;
    203 
    204 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
    205 
    206 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
    207 	    __func__, ether_sprintf(ni->ni_macaddr),
    208 	    ni->ni_rates.rs_nrates > 0 ?
    209 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
    210 
    211 	ni->ni_txrate = rate;
    212 	/*
    213 	 * Before associating a node has no rate set setup
    214 	 * so we can't calculate any transmit codes to use.
    215 	 * This is ok since we should never be sending anything
    216 	 * but management frames and those always go at the
    217 	 * lowest hardware rate.
    218 	 */
    219 	if (ni->ni_rates.rs_nrates > 0) {
    220 		amn->amn_tx_rix0 = sc->sc_rixmap[
    221 					       ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
    222 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
    223 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
    224 			rt->info[amn->amn_tx_rix0].shortPreamble;
    225 		if (sc->sc_mrretry) {
    226 			amn->amn_tx_try0 = 1;
    227 			amn->amn_tx_try1 = 1;
    228 			amn->amn_tx_try2 = 1;
    229 			amn->amn_tx_try3 = 1;
    230 			if (--rate >= 0) {
    231 				rix = sc->sc_rixmap[
    232 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    233 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
    234 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
    235 					rt->info[rix].shortPreamble;
    236 			} else {
    237 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
    238 			}
    239 			if (--rate >= 0) {
    240 				rix = sc->sc_rixmap[
    241 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    242 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
    243 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
    244 					rt->info[rix].shortPreamble;
    245 			} else {
    246 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
    247 			}
    248 			if (rate > 0) {
    249 				/* NB: only do this if we didn't already do it above */
    250 				amn->amn_tx_rate3 = rt->info[0].rateCode;
    251 				amn->amn_tx_rate3sp =
    252 					an->an_tx_rate3 | rt->info[0].shortPreamble;
    253 			} else {
    254 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
    255 			}
    256 		} else {
    257 			amn->amn_tx_try0 = ATH_TXMAXTRY;
    258 			/* theorically, these statements are useless because
    259 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
    260 			 */
    261 			amn->amn_tx_try1 = 0;
    262 			amn->amn_tx_try2 = 0;
    263 			amn->amn_tx_try3 = 0;
    264 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
    265 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
    266 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
    267 		}
    268 	}
    269 	node_reset (amn);
    270 }
    271 
    272 /*
    273  * Set the starting transmit rate for a node.
    274  */
    275 static void
    276 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
    277 {
    278 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
    279 	struct ieee80211com *ic = &sc->sc_ic;
    280 	int srate;
    281 
    282 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
    283 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
    284 		/*
    285 		 * No fixed rate is requested. For 11b start with
    286 		 * the highest negotiated rate; otherwise, for 11g
    287 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
    288 		 */
    289 		srate = ni->ni_rates.rs_nrates - 1;
    290 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
    291 			/*
    292 			 * Scan the negotiated rate set to find the
    293 			 * closest rate.
    294 			 */
    295 			/* NB: the rate set is assumed sorted */
    296 			for (; srate >= 0 && RATE(srate) > 72; srate--)
    297 				;
    298 			KASSERT(srate >= 0, ("bogus rate set"));
    299 		}
    300 	} else {
    301 		/*
    302 		 * A fixed rate is to be used; ic_fixed_rate is an
    303 		 * index into the supported rate set.  Convert this
    304 		 * to the index into the negotiated rate set for
    305 		 * the node.  We know the rate is there because the
    306 		 * rate set is checked when the station associates.
    307 		 */
    308 		const struct ieee80211_rateset *rs =
    309 			&ic->ic_sup_rates[ic->ic_curmode];
    310 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    311 		/* NB: the rate set is assumed sorted */
    312 		srate = ni->ni_rates.rs_nrates - 1;
    313 		for (; srate >= 0 && RATE(srate) != r; srate--)
    314 			;
    315 		KASSERT(srate >= 0,
    316 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
    317 	}
    318 	ath_rate_update(sc, ni, srate);
    319 #undef RATE
    320 }
    321 
    322 static void
    323 ath_rate_cb(void *arg, struct ieee80211_node *ni)
    324 {
    325 	struct ath_softc *sc = arg;
    326 
    327 	ath_rate_update(sc, ni, 0);
    328 }
    329 
    330 /*
    331  * Reset the rate control state for each 802.11 state transition.
    332  */
    333 void
    334 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
    335 {
    336 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
    337 	struct ieee80211com *ic = &sc->sc_ic;
    338 	struct ieee80211_node *ni;
    339 
    340 	if (state == IEEE80211_S_INIT) {
    341 		callout_stop(&asc->timer);
    342 		return;
    343 	}
    344 	if (ic->ic_opmode == IEEE80211_M_STA) {
    345 		/*
    346 		 * Reset local xmit state; this is really only
    347 		 * meaningful when operating in station mode.
    348 		 */
    349 		ni = ic->ic_bss;
    350 		if (state == IEEE80211_S_RUN) {
    351 			ath_rate_ctl_start(sc, ni);
    352 		} else {
    353 			ath_rate_update(sc, ni, 0);
    354 		}
    355 	} else {
    356 		/*
    357 		 * When operating as a station the node table holds
    358 		 * the AP's that were discovered during scanning.
    359 		 * For any other operating mode we want to reset the
    360 		 * tx rate state of each node.
    361 		 */
    362 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
    363 		ath_rate_update(sc, ic->ic_bss, 0);
    364 	}
    365 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
    366 	    state == IEEE80211_S_RUN) {
    367 		int interval;
    368 		/*
    369 		 * Start the background rate control thread if we
    370 		 * are not configured to use a fixed xmit rate.
    371 		 */
    372 		interval = ath_rateinterval;
    373 		if (ic->ic_opmode == IEEE80211_M_STA)
    374 			interval /= 2;
    375 		callout_reset(&asc->timer, (interval * hz) / 1000,
    376 			ath_ratectl, &sc->sc_if);
    377 	}
    378 }
    379 
    380 /*
    381  * Examine and potentially adjust the transmit rate.
    382  */
    383 static void
    384 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
    385 {
    386 	struct ath_softc *sc = arg;
    387 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
    388 	int old_rate;
    389 
    390 #define is_success(amn) \
    391 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
    392 #define is_enough(amn) \
    393 (amn->amn_tx_try0_cnt > 10)
    394 #define is_failure(amn) \
    395 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
    396 #define is_max_rate(ni) \
    397 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
    398 #define is_min_rate(ni) \
    399 (ni->ni_txrate == 0)
    400 
    401 	old_rate = ni->ni_txrate;
    402 
    403   	DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
    404 		 amn->amn_tx_try0_cnt,
    405 		 amn->amn_tx_try1_cnt,
    406 		 amn->amn_tx_try2_cnt,
    407 		 amn->amn_tx_try3_cnt,
    408 		 amn->amn_success_threshold);
    409   	if (is_success (amn) && is_enough (amn)) {
    410 		amn->amn_success++;
    411 		if (amn->amn_success == amn->amn_success_threshold &&
    412   		    !is_max_rate (ni)) {
    413   			amn->amn_recovery = 1;
    414   			amn->amn_success = 0;
    415   			ni->ni_txrate++;
    416 			DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
    417   		} else {
    418 			amn->amn_recovery = 0;
    419 		}
    420   	} else if (is_failure (amn)) {
    421   		amn->amn_success = 0;
    422   		if (!is_min_rate (ni)) {
    423   			if (amn->amn_recovery) {
    424   				/* recovery failure. */
    425   				amn->amn_success_threshold *= 2;
    426   				amn->amn_success_threshold = min (amn->amn_success_threshold,
    427 								  (u_int)ath_rate_max_success_threshold);
    428  				DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
    429   			} else {
    430   				/* simple failure. */
    431  				amn->amn_success_threshold = ath_rate_min_success_threshold;
    432  				DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
    433   			}
    434 			amn->amn_recovery = 0;
    435   			ni->ni_txrate--;
    436    		} else {
    437 			amn->amn_recovery = 0;
    438 		}
    439 
    440    	}
    441 	if (is_enough (amn) || old_rate != ni->ni_txrate) {
    442 		/* reset counters. */
    443 		amn->amn_tx_try0_cnt = 0;
    444 		amn->amn_tx_try1_cnt = 0;
    445 		amn->amn_tx_try2_cnt = 0;
    446 		amn->amn_tx_try3_cnt = 0;
    447 		amn->amn_tx_failure_cnt = 0;
    448 	}
    449 	if (old_rate != ni->ni_txrate) {
    450 		ath_rate_update(sc, ni, ni->ni_txrate);
    451 	}
    452 }
    453 
    454 static void
    455 ath_ratectl(void *arg)
    456 {
    457 	struct ifnet *ifp = arg;
    458 	struct ath_softc *sc = ifp->if_softc;
    459 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
    460 	struct ieee80211com *ic = &sc->sc_ic;
    461 	int interval;
    462 
    463 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
    464 		sc->sc_stats.ast_rate_calls++;
    465 
    466 		if (ic->ic_opmode == IEEE80211_M_STA)
    467 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
    468 		else
    469 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
    470 	}
    471 	interval = ath_rateinterval;
    472 	if (ic->ic_opmode == IEEE80211_M_STA)
    473 		interval /= 2;
    474 	callout_reset(&asc->timer, (interval * hz) / 1000,
    475 		ath_ratectl, &sc->sc_if);
    476 }
    477 
    478 static void
    479 ath_rate_sysctlattach(struct ath_softc *sc)
    480 {
    481 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
    482 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
    483 
    484 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    485 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
    486 		"rate control: operation interval (ms)");
    487 	/* XXX bounds check values */
    488 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    489 		"max_sucess_threshold", CTLFLAG_RW,
    490 		&ath_rate_max_success_threshold, 0, "");
    491 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    492 		"min_sucess_threshold", CTLFLAG_RW,
    493 		&ath_rate_min_success_threshold, 0, "");
    494 }
    495 
    496 struct ath_ratectrl *
    497 ath_rate_attach(struct ath_softc *sc)
    498 {
    499 	struct amrr_softc *asc;
    500 
    501 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
    502 	if (asc == NULL)
    503 		return NULL;
    504 	asc->arc.arc_space = sizeof(struct amrr_node);
    505 	callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
    506 	ath_rate_sysctlattach(sc);
    507 
    508 	return &asc->arc;
    509 }
    510 
    511 void
    512 ath_rate_detach(struct ath_ratectrl *arc)
    513 {
    514 	struct amrr_softc *asc = (struct amrr_softc *) arc;
    515 
    516 	callout_drain(&asc->timer);
    517 	free(asc, M_DEVBUF);
    518 }
    519