diff options
Diffstat (limited to 'sys/dev/ath/ath_rate')
| -rw-r--r-- | sys/dev/ath/ath_rate/amrr/amrr.c | 545 | ||||
| -rw-r--r-- | sys/dev/ath/ath_rate/amrr/amrr.h | 77 | ||||
| -rw-r--r-- | sys/dev/ath/ath_rate/onoe/onoe.c | 526 | ||||
| -rw-r--r-- | sys/dev/ath/ath_rate/onoe/onoe.h | 69 | ||||
| -rw-r--r-- | sys/dev/ath/ath_rate/sample/sample.c | 836 | ||||
| -rw-r--r-- | sys/dev/ath/ath_rate/sample/sample.h | 286 |
6 files changed, 2339 insertions, 0 deletions
diff --git a/sys/dev/ath/ath_rate/amrr/amrr.c b/sys/dev/ath/ath_rate/amrr/amrr.c new file mode 100644 index 000000000000..abae54967fbe --- /dev/null +++ b/sys/dev/ath/ath_rate/amrr/amrr.c @@ -0,0 +1,545 @@ +/*- + * Copyright (c) 2004 INRIA + * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * AMRR rate control. See: + * http://www-sop.inria.fr/rapports/sophia/RR-5208.html + * "IEEE 802.11 Rate Adaptation: A Practical Approach" by + * Mathieu Lacage, Hossein Manshaei, Thierry Turletti + */ +#include "opt_inet.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/sysctl.h> +#include <sys/module.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/errno.h> + +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/bus.h> + +#include <sys/socket.h> + +#include <net/if.h> +#include <net/if_media.h> +#include <net/if_arp.h> +#include <net/ethernet.h> /* XXX for ether_sprintf */ + +#include <net80211/ieee80211_var.h> + +#include <net/bpf.h> + +#ifdef INET +#include <netinet/in.h> +#include <netinet/if_ether.h> +#endif + +#include <dev/ath/if_athvar.h> +#include <dev/ath/ath_rate/amrr/amrr.h> +#include <contrib/dev/ath/ah_desc.h> + +#define AMRR_DEBUG +#ifdef AMRR_DEBUG +#define DPRINTF(sc, _fmt, ...) do { \ + if (sc->sc_debug & 0x10) \ + printf(_fmt, __VA_ARGS__); \ +} while (0) +#else +#define DPRINTF(sc, _fmt, ...) +#endif + +static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ +static int ath_rate_max_success_threshold = 10; +static int ath_rate_min_success_threshold = 1; + +static void ath_ratectl(void *); +static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, + int rate); +static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); +static void ath_rate_ctl(void *, struct ieee80211_node *); + +void +ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) +{ + /* NB: assumed to be zero'd by caller */ + ath_rate_update(sc, &an->an_node, 0); +} + +void +ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) +{ +} + +void +ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, + int shortPreamble, size_t frameLen, + u_int8_t *rix, int *try0, u_int8_t *txrate) +{ + struct amrr_node *amn = ATH_NODE_AMRR(an); + + *rix = amn->amn_tx_rix0; + *try0 = amn->amn_tx_try0; + if (shortPreamble) + *txrate = amn->amn_tx_rate0sp; + else + *txrate = amn->amn_tx_rate0; +} + +void +ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, + struct ath_desc *ds, int shortPreamble, u_int8_t rix) +{ + struct amrr_node *amn = ATH_NODE_AMRR(an); + + ath_hal_setupxtxdesc(sc->sc_ah, ds + , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */ + , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */ + , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */ + ); +} + +void +ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, + const struct ath_desc *ds, const struct ath_desc *ds0) +{ + struct amrr_node *amn = ATH_NODE_AMRR(an); + int sr = ds->ds_txstat.ts_shortretry; + int lr = ds->ds_txstat.ts_longretry; + int retry_count = sr + lr; + + amn->amn_tx_try0_cnt++; + if (retry_count == 1) { + amn->amn_tx_try1_cnt++; + } else if (retry_count == 2) { + amn->amn_tx_try1_cnt++; + amn->amn_tx_try2_cnt++; + } else if (retry_count == 3) { + amn->amn_tx_try1_cnt++; + amn->amn_tx_try2_cnt++; + amn->amn_tx_try3_cnt++; + } else if (retry_count > 3) { + amn->amn_tx_try1_cnt++; + amn->amn_tx_try2_cnt++; + amn->amn_tx_try3_cnt++; + amn->amn_tx_failure_cnt++; + } +} + +void +ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) +{ + if (isnew) + ath_rate_ctl_start(sc, &an->an_node); +} + +static void +node_reset (struct amrr_node *amn) +{ + amn->amn_tx_try0_cnt = 0; + amn->amn_tx_try1_cnt = 0; + amn->amn_tx_try2_cnt = 0; + amn->amn_tx_try3_cnt = 0; + amn->amn_tx_failure_cnt = 0; + amn->amn_success = 0; + amn->amn_recovery = 0; + amn->amn_success_threshold = ath_rate_min_success_threshold; +} + + +/** + * The code below assumes that we are dealing with hardware multi rate retry + * I have no idea what will happen if you try to use this module with another + * type of hardware. Your machine might catch fire or it might work with + * horrible performance... + */ +static void +ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) +{ + struct ath_node *an = ATH_NODE(ni); + struct amrr_node *amn = ATH_NODE_AMRR(an); + const HAL_RATE_TABLE *rt = sc->sc_currates; + u_int8_t rix; + + KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); + + DPRINTF(sc, "%s: set xmit rate for %s to %dM\n", + __func__, ether_sprintf(ni->ni_macaddr), + ni->ni_rates.rs_nrates > 0 ? + (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); + + ni->ni_txrate = rate; + /* + * Before associating a node has no rate set setup + * so we can't calculate any transmit codes to use. + * This is ok since we should never be sending anything + * but management frames and those always go at the + * lowest hardware rate. + */ + if (ni->ni_rates.rs_nrates > 0) { + amn->amn_tx_rix0 = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL]; + amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode; + amn->amn_tx_rate0sp = amn->amn_tx_rate0 | + rt->info[amn->amn_tx_rix0].shortPreamble; + if (sc->sc_mrretry) { + amn->amn_tx_try0 = 1; + amn->amn_tx_try1 = 1; + amn->amn_tx_try2 = 1; + amn->amn_tx_try3 = 1; + if (--rate >= 0) { + rix = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; + amn->amn_tx_rate1 = rt->info[rix].rateCode; + amn->amn_tx_rate1sp = amn->amn_tx_rate1 | + rt->info[rix].shortPreamble; + } else { + amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; + } + if (--rate >= 0) { + rix = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; + amn->amn_tx_rate2 = rt->info[rix].rateCode; + amn->amn_tx_rate2sp = amn->amn_tx_rate2 | + rt->info[rix].shortPreamble; + } else { + amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; + } + if (rate > 0) { + /* NB: only do this if we didn't already do it above */ + amn->amn_tx_rate3 = rt->info[0].rateCode; + amn->amn_tx_rate3sp = + amn->amn_tx_rate3 | rt->info[0].shortPreamble; + } else { + amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; + } + } else { + amn->amn_tx_try0 = ATH_TXMAXTRY; + /* theorically, these statements are useless because + * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY + */ + amn->amn_tx_try1 = 0; + amn->amn_tx_try2 = 0; + amn->amn_tx_try3 = 0; + amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; + amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; + amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; + } + } + node_reset (amn); +} + +/* + * Set the starting transmit rate for a node. + */ +static void +ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) +{ +#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) + struct ieee80211com *ic = &sc->sc_ic; + int srate; + + KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); + if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { + /* + * No fixed rate is requested. For 11b start with + * the highest negotiated rate; otherwise, for 11g + * and 11a, we start "in the middle" at 24Mb or 36Mb. + */ + srate = ni->ni_rates.rs_nrates - 1; + if (sc->sc_curmode != IEEE80211_MODE_11B) { + /* + * Scan the negotiated rate set to find the + * closest rate. + */ + /* NB: the rate set is assumed sorted */ + for (; srate >= 0 && RATE(srate) > 72; srate--) + ; + KASSERT(srate >= 0, ("bogus rate set")); + } + } else { + /* + * A fixed rate is to be used; ic_fixed_rate is an + * index into the supported rate set. Convert this + * to the index into the negotiated rate set for + * the node. We know the rate is there because the + * rate set is checked when the station associates. + */ + const struct ieee80211_rateset *rs = + &ic->ic_sup_rates[ic->ic_curmode]; + int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; + /* NB: the rate set is assumed sorted */ + srate = ni->ni_rates.rs_nrates - 1; + for (; srate >= 0 && RATE(srate) != r; srate--) + ; + KASSERT(srate >= 0, + ("fixed rate %d not in rate set", ic->ic_fixed_rate)); + } + ath_rate_update(sc, ni, srate); +#undef RATE +} + +static void +ath_rate_cb(void *arg, struct ieee80211_node *ni) +{ + struct ath_softc *sc = arg; + + ath_rate_update(sc, ni, 0); +} + +/* + * Reset the rate control state for each 802.11 state transition. + */ +void +ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state) +{ + struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc; + struct ieee80211com *ic = &sc->sc_ic; + struct ieee80211_node *ni; + + if (state == IEEE80211_S_INIT) { + callout_stop(&asc->timer); + return; + } + if (ic->ic_opmode == IEEE80211_M_STA) { + /* + * Reset local xmit state; this is really only + * meaningful when operating in station mode. + */ + ni = ic->ic_bss; + if (state == IEEE80211_S_RUN) { + ath_rate_ctl_start(sc, ni); + } else { + ath_rate_update(sc, ni, 0); + } + } else { + /* + * When operating as a station the node table holds + * the AP's that were discovered during scanning. + * For any other operating mode we want to reset the + * tx rate state of each node. + */ + ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc); + ath_rate_update(sc, ic->ic_bss, 0); + } + if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE && + state == IEEE80211_S_RUN) { + int interval; + /* + * Start the background rate control thread if we + * are not configured to use a fixed xmit rate. + */ + interval = ath_rateinterval; + if (ic->ic_opmode == IEEE80211_M_STA) + interval /= 2; + callout_reset(&asc->timer, (interval * hz) / 1000, + ath_ratectl, sc->sc_ifp); + } +} + +/* + * Examine and potentially adjust the transmit rate. + */ +static void +ath_rate_ctl(void *arg, struct ieee80211_node *ni) +{ + struct ath_softc *sc = arg; + struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni)); + int old_rate; + +#define is_success(amn) \ +(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10)) +#define is_enough(amn) \ +(amn->amn_tx_try0_cnt > 10) +#define is_failure(amn) \ +(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3)) +#define is_max_rate(ni) \ +((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates) +#define is_min_rate(ni) \ +(ni->ni_txrate == 0) + + old_rate = ni->ni_txrate; + + DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n", + amn->amn_tx_try0_cnt, + amn->amn_tx_try1_cnt, + amn->amn_tx_try2_cnt, + amn->amn_tx_try3_cnt, + amn->amn_success_threshold); + if (is_success (amn) && is_enough (amn)) { + amn->amn_success++; + if (amn->amn_success == amn->amn_success_threshold && + !is_max_rate (ni)) { + amn->amn_recovery = 1; + amn->amn_success = 0; + ni->ni_txrate++; + DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate); + } else { + amn->amn_recovery = 0; + } + } else if (is_failure (amn)) { + amn->amn_success = 0; + if (!is_min_rate (ni)) { + if (amn->amn_recovery) { + /* recovery failure. */ + amn->amn_success_threshold *= 2; + amn->amn_success_threshold = min (amn->amn_success_threshold, + (u_int)ath_rate_max_success_threshold); + DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold); + } else { + /* simple failure. */ + amn->amn_success_threshold = ath_rate_min_success_threshold; + DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold); + } + amn->amn_recovery = 0; + ni->ni_txrate--; + } else { + amn->amn_recovery = 0; + } + + } + if (is_enough (amn) || old_rate != ni->ni_txrate) { + /* reset counters. */ + amn->amn_tx_try0_cnt = 0; + amn->amn_tx_try1_cnt = 0; + amn->amn_tx_try2_cnt = 0; + amn->amn_tx_try3_cnt = 0; + amn->amn_tx_failure_cnt = 0; + } + if (old_rate != ni->ni_txrate) { + ath_rate_update(sc, ni, ni->ni_txrate); + } +} + +static void +ath_ratectl(void *arg) +{ + struct ifnet *ifp = arg; + struct ath_softc *sc = ifp->if_softc; + struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc; + struct ieee80211com *ic = &sc->sc_ic; + int interval; + + if (ifp->if_drv_flags & IFF_DRV_RUNNING) { + sc->sc_stats.ast_rate_calls++; + + if (ic->ic_opmode == IEEE80211_M_STA) + ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */ + else + ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc); + } + interval = ath_rateinterval; + if (ic->ic_opmode == IEEE80211_M_STA) + interval /= 2; + callout_reset(&asc->timer, (interval * hz) / 1000, + ath_ratectl, sc->sc_ifp); +} + +static void +ath_rate_sysctlattach(struct ath_softc *sc) +{ + struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); + struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); + + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, + "rate control: operation interval (ms)"); + /* XXX bounds check values */ + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "max_sucess_threshold", CTLFLAG_RW, + &ath_rate_max_success_threshold, 0, ""); + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "min_sucess_threshold", CTLFLAG_RW, + &ath_rate_min_success_threshold, 0, ""); +} + +struct ath_ratectrl * +ath_rate_attach(struct ath_softc *sc) +{ + struct amrr_softc *asc; + + asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO); + if (asc == NULL) + return NULL; + asc->arc.arc_space = sizeof(struct amrr_node); + callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0); + ath_rate_sysctlattach(sc); + + return &asc->arc; +} + +void +ath_rate_detach(struct ath_ratectrl *arc) +{ + struct amrr_softc *asc = (struct amrr_softc *) arc; + + callout_drain(&asc->timer); + free(asc, M_DEVBUF); +} + +/* + * Module glue. + */ +static int +amrr_modevent(module_t mod, int type, void *unused) +{ + switch (type) { + case MOD_LOAD: + if (bootverbose) + printf("ath_rate: <AMRR rate control algorithm> version 0.1\n"); + return 0; + case MOD_UNLOAD: + return 0; + } + return EINVAL; +} + +static moduledata_t amrr_mod = { + "ath_rate", + amrr_modevent, + 0 +}; +DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); +MODULE_VERSION(ath_rate, 1); +MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); diff --git a/sys/dev/ath/ath_rate/amrr/amrr.h b/sys/dev/ath/ath_rate/amrr/amrr.h new file mode 100644 index 000000000000..cb5d135975bf --- /dev/null +++ b/sys/dev/ath/ath_rate/amrr/amrr.h @@ -0,0 +1,77 @@ +/*- + * Copyright (c) 2004 INRIA + * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + * + * $FreeBSD$ + */ + +#ifndef _DEV_ATH_RATE_AMRR_H +#define _DEV_ATH_RATE_AMRR_H + +/* per-device state */ +struct amrr_softc { + struct ath_ratectrl arc; /* base state */ + struct callout timer; /* periodic timer */ +}; + +/* per-node state */ +struct amrr_node { + /* AMRR statistics for this node */ + u_int amn_tx_try0_cnt; + u_int amn_tx_try1_cnt; + u_int amn_tx_try2_cnt; + u_int amn_tx_try3_cnt; + u_int amn_tx_failure_cnt; + /* AMRR algorithm state for this node */ + u_int amn_success_threshold; + u_int amn_success; + u_int amn_recovery; + /* rate index et al. */ + u_int8_t amn_tx_rix0; /* series 0 rate index */ + u_int8_t amn_tx_rate0; /* series 0 h/w rate */ + u_int8_t amn_tx_rate1; /* series 1 h/w rate */ + u_int8_t amn_tx_rate2; /* series 2 h/w rate */ + u_int8_t amn_tx_rate3; /* series 3 h/w rate */ + u_int8_t amn_tx_rate0sp; /* series 0 short preamble h/w rate */ + u_int8_t amn_tx_rate1sp; /* series 1 short preamble h/w rate */ + u_int8_t amn_tx_rate2sp; /* series 2 short preamble h/w rate */ + u_int8_t amn_tx_rate3sp; /* series 3 short preamble h/w rate */ + u_int8_t amn_tx_try0; /* series 0 try count */ + u_int amn_tx_try1; /* series 1 try count */ + u_int amn_tx_try2; /* series 2 try count */ + u_int amn_tx_try3; /* series 3 try count */ +}; +#define ATH_NODE_AMRR(an) ((struct amrr_node *)&an[1]) +#endif /* _DEV_ATH_RATE_AMRR_H */ diff --git a/sys/dev/ath/ath_rate/onoe/onoe.c b/sys/dev/ath/ath_rate/onoe/onoe.c new file mode 100644 index 000000000000..dc4fada185bc --- /dev/null +++ b/sys/dev/ath/ath_rate/onoe/onoe.c @@ -0,0 +1,526 @@ +/*- + * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * Atsushi Onoe's rate control algorithm. + */ +#include "opt_inet.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/sysctl.h> +#include <sys/module.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/errno.h> + +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/bus.h> + +#include <sys/socket.h> + +#include <net/if.h> +#include <net/if_media.h> +#include <net/if_arp.h> +#include <net/ethernet.h> /* XXX for ether_sprintf */ + +#include <net80211/ieee80211_var.h> + +#include <net/bpf.h> + +#ifdef INET +#include <netinet/in.h> +#include <netinet/if_ether.h> +#endif + +#include <dev/ath/if_athvar.h> +#include <dev/ath/ath_rate/onoe/onoe.h> +#include <contrib/dev/ath/ah_desc.h> + +#define ONOE_DEBUG +#ifdef ONOE_DEBUG +enum { + ATH_DEBUG_RATE = 0x00000010, /* rate control */ +}; +#define DPRINTF(sc, _fmt, ...) do { \ + if (sc->sc_debug & ATH_DEBUG_RATE) \ + printf(_fmt, __VA_ARGS__); \ +} while (0) +#else +#define DPRINTF(sc, _fmt, ...) +#endif + +/* + * Default parameters for the rate control algorithm. These are + * all tunable with sysctls. The rate controller runs periodically + * (each ath_rateinterval ms) analyzing transmit statistics for each + * neighbor/station (when operating in station mode this is only the AP). + * If transmits look to be working well over a sampling period then + * it gives a "raise rate credit". If transmits look to not be working + * well than it deducts a credit. If the credits cross a threshold then + * the transmit rate is raised. Various error conditions force the + * the transmit rate to be dropped. + * + * The decision to issue/deduct a credit is based on the errors and + * retries accumulated over the sampling period. ath_rate_raise defines + * the percent of retransmits for which a credit is issued/deducted. + * ath_rate_raise_threshold defines the threshold on credits at which + * the transmit rate is increased. + * + * XXX this algorithm is flawed. + */ +static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ +static int ath_rate_raise = 10; /* add credit threshold */ +static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */ + +static void ath_ratectl(void *); +static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, + int rate); +static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); +static void ath_rate_ctl(void *, struct ieee80211_node *); + +void +ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) +{ + /* NB: assumed to be zero'd by caller */ + ath_rate_update(sc, &an->an_node, 0); +} + +void +ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) +{ +} + +void +ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, + int shortPreamble, size_t frameLen, + u_int8_t *rix, int *try0, u_int8_t *txrate) +{ + struct onoe_node *on = ATH_NODE_ONOE(an); + + *rix = on->on_tx_rix0; + *try0 = on->on_tx_try0; + if (shortPreamble) + *txrate = on->on_tx_rate0sp; + else + *txrate = on->on_tx_rate0; +} + +void +ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, + struct ath_desc *ds, int shortPreamble, u_int8_t rix) +{ + struct onoe_node *on = ATH_NODE_ONOE(an); + + ath_hal_setupxtxdesc(sc->sc_ah, ds + , on->on_tx_rate1sp, 2 /* series 1 */ + , on->on_tx_rate2sp, 2 /* series 2 */ + , on->on_tx_rate3sp, 2 /* series 3 */ + ); +} + +void +ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, + const struct ath_desc *ds, const struct ath_desc *ds0) +{ + struct onoe_node *on = ATH_NODE_ONOE(an); + + if (ds->ds_txstat.ts_status == 0) + on->on_tx_ok++; + else + on->on_tx_err++; + on->on_tx_retr += ds->ds_txstat.ts_shortretry + + ds->ds_txstat.ts_longretry; +} + +void +ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) +{ + if (isnew) + ath_rate_ctl_start(sc, &an->an_node); +} + +static void +ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) +{ + struct ath_node *an = ATH_NODE(ni); + struct onoe_node *on = ATH_NODE_ONOE(an); + const HAL_RATE_TABLE *rt = sc->sc_currates; + u_int8_t rix; + + KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); + + DPRINTF(sc, "%s: set xmit rate for %s to %dM\n", + __func__, ether_sprintf(ni->ni_macaddr), + ni->ni_rates.rs_nrates > 0 ? + (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); + + ni->ni_txrate = rate; + /* + * Before associating a node has no rate set setup + * so we can't calculate any transmit codes to use. + * This is ok since we should never be sending anything + * but management frames and those always go at the + * lowest hardware rate. + */ + if (ni->ni_rates.rs_nrates == 0) + goto done; + on->on_tx_rix0 = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL]; + on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode; + + on->on_tx_rate0sp = on->on_tx_rate0 | + rt->info[on->on_tx_rix0].shortPreamble; + if (sc->sc_mrretry) { + /* + * Hardware supports multi-rate retry; setup two + * step-down retry rates and make the lowest rate + * be the ``last chance''. We use 4, 2, 2, 2 tries + * respectively (4 is set here, the rest are fixed + * in the xmit routine). + */ + on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */ + if (--rate >= 0) { + rix = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; + on->on_tx_rate1 = rt->info[rix].rateCode; + on->on_tx_rate1sp = on->on_tx_rate1 | + rt->info[rix].shortPreamble; + } else { + on->on_tx_rate1 = on->on_tx_rate1sp = 0; + } + if (--rate >= 0) { + rix = sc->sc_rixmap[ + ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; + on->on_tx_rate2 = rt->info[rix].rateCode; + on->on_tx_rate2sp = on->on_tx_rate2 | + rt->info[rix].shortPreamble; + } else { + on->on_tx_rate2 = on->on_tx_rate2sp = 0; + } + if (rate > 0) { + /* NB: only do this if we didn't already do it above */ + on->on_tx_rate3 = rt->info[0].rateCode; + on->on_tx_rate3sp = + on->on_tx_rate3 | rt->info[0].shortPreamble; + } else { + on->on_tx_rate3 = on->on_tx_rate3sp = 0; + } + } else { + on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */ + on->on_tx_rate1 = on->on_tx_rate1sp = 0; + on->on_tx_rate2 = on->on_tx_rate2sp = 0; + on->on_tx_rate3 = on->on_tx_rate3sp = 0; + } +done: + on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0; +} + +/* + * Set the starting transmit rate for a node. + */ +static void +ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) +{ +#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) + struct ieee80211com *ic = &sc->sc_ic; + int srate; + + KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); + if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { + /* + * No fixed rate is requested. For 11b start with + * the highest negotiated rate; otherwise, for 11g + * and 11a, we start "in the middle" at 24Mb or 36Mb. + */ + srate = ni->ni_rates.rs_nrates - 1; + if (sc->sc_curmode != IEEE80211_MODE_11B) { + /* + * Scan the negotiated rate set to find the + * closest rate. + */ + /* NB: the rate set is assumed sorted */ + for (; srate >= 0 && RATE(srate) > 72; srate--) + ; + KASSERT(srate >= 0, ("bogus rate set")); + } + } else { + /* + * A fixed rate is to be used; ic_fixed_rate is an + * index into the supported rate set. Convert this + * to the index into the negotiated rate set for + * the node. We know the rate is there because the + * rate set is checked when the station associates. + */ + const struct ieee80211_rateset *rs = + &ic->ic_sup_rates[ic->ic_curmode]; + int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; + /* NB: the rate set is assumed sorted */ + srate = ni->ni_rates.rs_nrates - 1; + for (; srate >= 0 && RATE(srate) != r; srate--) + ; + KASSERT(srate >= 0, + ("fixed rate %d not in rate set", ic->ic_fixed_rate)); + } + ath_rate_update(sc, ni, srate); +#undef RATE +} + +static void +ath_rate_cb(void *arg, struct ieee80211_node *ni) +{ + struct ath_softc *sc = arg; + + ath_rate_update(sc, ni, 0); +} + +/* + * Reset the rate control state for each 802.11 state transition. + */ +void +ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state) +{ + struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc; + struct ieee80211com *ic = &sc->sc_ic; + struct ieee80211_node *ni; + + if (state == IEEE80211_S_INIT) { + callout_stop(&osc->timer); + return; + } + if (ic->ic_opmode == IEEE80211_M_STA) { + /* + * Reset local xmit state; this is really only + * meaningful when operating in station mode. + */ + ni = ic->ic_bss; + if (state == IEEE80211_S_RUN) { + ath_rate_ctl_start(sc, ni); + } else { + ath_rate_update(sc, ni, 0); + } + } else { + /* + * When operating as a station the node table holds + * the AP's that were discovered during scanning. + * For any other operating mode we want to reset the + * tx rate state of each node. + */ + ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc); + ath_rate_update(sc, ic->ic_bss, 0); + } + if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE && + state == IEEE80211_S_RUN) { + int interval; + /* + * Start the background rate control thread if we + * are not configured to use a fixed xmit rate. + */ + interval = ath_rateinterval; + if (ic->ic_opmode == IEEE80211_M_STA) + interval /= 2; + callout_reset(&osc->timer, (interval * hz) / 1000, + ath_ratectl, sc->sc_ifp); + } +} + +/* + * Examine and potentially adjust the transmit rate. + */ +static void +ath_rate_ctl(void *arg, struct ieee80211_node *ni) +{ + struct ath_softc *sc = arg; + struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni)); + struct ieee80211_rateset *rs = &ni->ni_rates; + int dir = 0, nrate, enough; + + /* + * Rate control + * XXX: very primitive version. + */ + enough = (on->on_tx_ok + on->on_tx_err >= 10); + + /* no packet reached -> down */ + if (on->on_tx_err > 0 && on->on_tx_ok == 0) + dir = -1; + + /* all packets needs retry in average -> down */ + if (enough && on->on_tx_ok < on->on_tx_retr) + dir = -1; + + /* no error and less than rate_raise% of packets need retry -> up */ + if (enough && on->on_tx_err == 0 && + on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100) + dir = 1; + + DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n", + ether_sprintf(ni->ni_macaddr), + on->on_tx_ok, on->on_tx_err, on->on_tx_retr, + on->on_tx_upper, dir); + + nrate = ni->ni_txrate; + switch (dir) { + case 0: + if (enough && on->on_tx_upper > 0) + on->on_tx_upper--; + break; + case -1: + if (nrate > 0) { + nrate--; + sc->sc_stats.ast_rate_drop++; + } + on->on_tx_upper = 0; + break; + case 1: + /* raise rate if we hit rate_raise_threshold */ + if (++on->on_tx_upper < ath_rate_raise_threshold) + break; + on->on_tx_upper = 0; + if (nrate + 1 < rs->rs_nrates) { + nrate++; + sc->sc_stats.ast_rate_raise++; + } + break; + } + + if (nrate != ni->ni_txrate) { + DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n", + __func__, + (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2, + (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2, + on->on_tx_ok, on->on_tx_err, on->on_tx_retr); + ath_rate_update(sc, ni, nrate); + } else if (enough) + on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0; +} + +static void +ath_ratectl(void *arg) +{ + struct ifnet *ifp = arg; + struct ath_softc *sc = ifp->if_softc; + struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc; + struct ieee80211com *ic = &sc->sc_ic; + int interval; + + if (ifp->if_drv_flags & IFF_DRV_RUNNING) { + sc->sc_stats.ast_rate_calls++; + + if (ic->ic_opmode == IEEE80211_M_STA) + ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */ + else + ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc); + } + interval = ath_rateinterval; + if (ic->ic_opmode == IEEE80211_M_STA) + interval /= 2; + callout_reset(&osc->timer, (interval * hz) / 1000, + ath_ratectl, sc->sc_ifp); +} + +static void +ath_rate_sysctlattach(struct ath_softc *sc) +{ + struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); + struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); + + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, + "rate control: operation interval (ms)"); + /* XXX bounds check values */ + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0, + "rate control: retry threshold to credit rate raise (%%)"); + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0, + "rate control: # good periods before raising rate"); +} + +struct ath_ratectrl * +ath_rate_attach(struct ath_softc *sc) +{ + struct onoe_softc *osc; + + osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO); + if (osc == NULL) + return NULL; + osc->arc.arc_space = sizeof(struct onoe_node); + callout_init(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0); + ath_rate_sysctlattach(sc); + + return &osc->arc; +} + +void +ath_rate_detach(struct ath_ratectrl *arc) +{ + struct onoe_softc *osc = (struct onoe_softc *) arc; + + callout_drain(&osc->timer); + free(osc, M_DEVBUF); +} + +/* + * Module glue. + */ +static int +onoe_modevent(module_t mod, int type, void *unused) +{ + switch (type) { + case MOD_LOAD: + if (bootverbose) + printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n"); + return 0; + case MOD_UNLOAD: + return 0; + } + return EINVAL; +} + +static moduledata_t onoe_mod = { + "ath_rate", + onoe_modevent, + 0 +}; +DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); +MODULE_VERSION(ath_rate, 1); +MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); diff --git a/sys/dev/ath/ath_rate/onoe/onoe.h b/sys/dev/ath/ath_rate/onoe/onoe.h new file mode 100644 index 000000000000..9d0661c23e7f --- /dev/null +++ b/sys/dev/ath/ath_rate/onoe/onoe.h @@ -0,0 +1,69 @@ +/*- + * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + * + * $FreeBSD$ + */ + +/* + * Defintions for the Atheros Wireless LAN controller driver. + */ +#ifndef _DEV_ATH_RATE_ONOE_H +#define _DEV_ATH_RATE_ONOE_H + +/* per-device state */ +struct onoe_softc { + struct ath_ratectrl arc; /* base state */ + struct callout timer; /* periodic timer */ +}; + +/* per-node state */ +struct onoe_node { + u_int on_tx_ok; /* tx ok pkt */ + u_int on_tx_err; /* tx !ok pkt */ + u_int on_tx_retr; /* tx retry count */ + int on_tx_upper; /* tx upper rate req cnt */ + u_int8_t on_tx_rix0; /* series 0 rate index */ + u_int8_t on_tx_try0; /* series 0 try count */ + u_int8_t on_tx_rate0; /* series 0 h/w rate */ + u_int8_t on_tx_rate1; /* series 1 h/w rate */ + u_int8_t on_tx_rate2; /* series 2 h/w rate */ + u_int8_t on_tx_rate3; /* series 3 h/w rate */ + u_int8_t on_tx_rate0sp; /* series 0 short preamble h/w rate */ + u_int8_t on_tx_rate1sp; /* series 1 short preamble h/w rate */ + u_int8_t on_tx_rate2sp; /* series 2 short preamble h/w rate */ + u_int8_t on_tx_rate3sp; /* series 3 short preamble h/w rate */ +}; +#define ATH_NODE_ONOE(an) ((struct onoe_node *)&an[1]) +#endif /* _DEV_ATH_RATE_ONOE_H */ diff --git a/sys/dev/ath/ath_rate/sample/sample.c b/sys/dev/ath/ath_rate/sample/sample.c new file mode 100644 index 000000000000..2bdf1ef48bd5 --- /dev/null +++ b/sys/dev/ath/ath_rate/sample/sample.c @@ -0,0 +1,836 @@ +/*- + * Copyright (c) 2005 John Bicket + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * John Bicket's SampleRate control algorithm. + */ +#include "opt_inet.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/sysctl.h> +#include <sys/module.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/errno.h> + +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/bus.h> + +#include <sys/socket.h> + +#include <net/if.h> +#include <net/if_media.h> +#include <net/if_arp.h> +#include <net/ethernet.h> /* XXX for ether_sprintf */ + +#include <net80211/ieee80211_var.h> + +#include <net/bpf.h> + +#ifdef INET +#include <netinet/in.h> +#include <netinet/if_ether.h> +#endif + +#include <dev/ath/if_athvar.h> +#include <dev/ath/ath_rate/sample/sample.h> +#include <contrib/dev/ath/ah_desc.h> + +#define SAMPLE_DEBUG +#ifdef SAMPLE_DEBUG +enum { + ATH_DEBUG_RATE = 0x00000010 /* rate control */ +}; +#define DPRINTF(sc, _fmt, ...) do { \ + if (sc->sc_debug & ATH_DEBUG_RATE) \ + printf(_fmt, __VA_ARGS__); \ +} while (0) +#else +#define DPRINTF(sc, _fmt, ...) +#endif + +/* + * This file is an implementation of the SampleRate algorithm + * in "Bit-rate Selection in Wireless Networks" + * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps) + * + * SampleRate chooses the bit-rate it predicts will provide the most + * throughput based on estimates of the expected per-packet + * transmission time for each bit-rate. SampleRate periodically sends + * packets at bit-rates other than the current one to estimate when + * another bit-rate will provide better performance. SampleRate + * switches to another bit-rate when its estimated per-packet + * transmission time becomes smaller than the current bit-rate's. + * SampleRate reduces the number of bit-rates it must sample by + * eliminating those that could not perform better than the one + * currently being used. SampleRate also stops probing at a bit-rate + * if it experiences several successive losses. + * + * The difference between the algorithm in the thesis and the one in this + * file is that the one in this file uses a ewma instead of a window. + * + * Also, this implementation tracks the average transmission time for + * a few different packet sizes independently for each link. + */ + +#define STALE_FAILURE_TIMEOUT_MS 10000 +#define MIN_SWITCH_MS 1000 + +static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *); + +static __inline int +size_to_bin(int size) +{ + int x = 0; + for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) { + if (size <= packet_size_bins[x]) { + return x; + } + } + return NUM_PACKET_SIZE_BINS-1; +} +static __inline int +bin_to_size(int index) { + return packet_size_bins[index]; +} + +static __inline int +rate_to_ndx(struct sample_node *sn, int rate) { + int x = 0; + for (x = 0; x < sn->num_rates; x++) { + if (sn->rates[x].rate == rate) { + return x; + } + } + return -1; +} + +void +ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) +{ + DPRINTF(sc, "%s:\n", __func__); + /* NB: assumed to be zero'd by caller */ +} + +void +ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) +{ + DPRINTF(sc, "%s:\n", __func__); +} + + +/* + * returns the ndx with the lowest average_tx_time, + * or -1 if all the average_tx_times are 0. + */ +static __inline int best_rate_ndx(struct sample_node *sn, int size_bin, + int require_acked_before) +{ + int x = 0; + int best_rate_ndx = 0; + int best_rate_tt = 0; + for (x = 0; x < sn->num_rates; x++) { + int tt = sn->stats[size_bin][x].average_tx_time; + if (tt <= 0 || (require_acked_before && + !sn->stats[size_bin][x].packets_acked)) { + continue; + } + + /* 9 megabits never works better than 12 */ + if (sn->rates[x].rate == 18) + continue; + + /* don't use a bit-rate that has been failing */ + if (sn->stats[size_bin][x].successive_failures > 3) + continue; + + if (!best_rate_tt || best_rate_tt > tt) { + best_rate_tt = tt; + best_rate_ndx = x; + } + } + return (best_rate_tt) ? best_rate_ndx : -1; +} + +/* + * pick a good "random" bit-rate to sample other than the current one + */ +static __inline int +pick_sample_ndx(struct sample_node *sn, int size_bin) +{ + int x = 0; + int current_ndx = 0; + unsigned current_tt = 0; + + current_ndx = sn->current_rate[size_bin]; + if (current_ndx < 0) { + /* no successes yet, send at the lowest bit-rate */ + return 0; + } + + current_tt = sn->stats[size_bin][current_ndx].average_tx_time; + + for (x = 0; x < sn->num_rates; x++) { + int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates; + + /* don't sample the current bit-rate */ + if (ndx == current_ndx) + continue; + + /* this bit-rate is always worse than the current one */ + if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt) + continue; + + /* rarely sample bit-rates that fail a lot */ + if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) && + sn->stats[size_bin][ndx].successive_failures > 3) + continue; + + /* don't sample more than 2 indexes higher + * for rates higher than 11 megabits + */ + if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2) + continue; + + /* 9 megabits never works better than 12 */ + if (sn->rates[ndx].rate == 18) + continue; + + /* if we're using 11 megabits, only sample up to 12 megabits + */ + if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1) + continue; + + sn->last_sample_ndx[size_bin] = ndx; + return ndx; + } + return current_ndx; +} + +void +ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, + int shortPreamble, size_t frameLen, + u_int8_t *rix, int *try0, u_int8_t *txrate) +{ + struct sample_node *sn = ATH_NODE_SAMPLE(an); + struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); + struct ieee80211com *ic = &sc->sc_ic; + int ndx, size_bin, mrr, best_ndx, change_rates; + unsigned average_tx_time; + + mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); + size_bin = size_to_bin(frameLen); + best_ndx = best_rate_ndx(sn, size_bin, !mrr); + + if (best_ndx >= 0) { + average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time; + } else { + average_tx_time = 0; + } + + if (sn->static_rate_ndx != -1) { + ndx = sn->static_rate_ndx; + *try0 = ATH_TXMAXTRY; + } else { + *try0 = mrr ? 2 : ATH_TXMAXTRY; + + if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) { + /* + * we want to limit the time measuring the performance + * of other bit-rates to ath_sample_rate% of the + * total transmission time. + */ + ndx = pick_sample_ndx(sn, size_bin); + if (ndx != sn->current_rate[size_bin]) { + sn->current_sample_ndx[size_bin] = ndx; + } else { + sn->current_sample_ndx[size_bin] = -1; + } + sn->packets_since_sample[size_bin] = 0; + + } else { + change_rates = 0; + if (!sn->packets_sent[size_bin] || best_ndx == -1) { + /* no packet has been sent successfully yet */ + for (ndx = sn->num_rates-1; ndx > 0; ndx--) { + /* + * pick the highest rate <= 36 Mbps + * that hasn't failed. + */ + if (sn->rates[ndx].rate <= 72 && + sn->stats[size_bin][ndx].successive_failures == 0) { + break; + } + } + change_rates = 1; + best_ndx = ndx; + } else if (sn->packets_sent[size_bin] < 20) { + /* let the bit-rate switch quickly during the first few packets */ + change_rates = 1; + } else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) { + /* 2 seconds have gone by */ + change_rates = 1; + } else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) { + /* the current bit-rate is twice as slow as the best one */ + change_rates = 1; + } + + sn->packets_since_sample[size_bin]++; + + if (change_rates) { + if (best_ndx != sn->current_rate[size_bin]) { + DPRINTF(sc, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n", + __func__, + ether_sprintf(an->an_node.ni_macaddr), + packet_size_bins[size_bin], + sn->rates[sn->current_rate[size_bin]].rate, + sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time, + sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time, + sn->rates[best_ndx].rate, + sn->stats[size_bin][best_ndx].average_tx_time, + sn->stats[size_bin][best_ndx].perfect_tx_time, + sn->packets_since_switch[size_bin], + mrr); + } + sn->packets_since_switch[size_bin] = 0; + sn->current_rate[size_bin] = best_ndx; + sn->ticks_since_switch[size_bin] = ticks; + } + ndx = sn->current_rate[size_bin]; + sn->packets_since_switch[size_bin]++; + if (size_bin == 0) { + /* + * set the visible txrate for this node + * to the rate of small packets + */ + an->an_node.ni_txrate = ndx; + } + } + } + + KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx)); + + *rix = sn->rates[ndx].rix; + if (shortPreamble) { + *txrate = sn->rates[ndx].shortPreambleRateCode; + } else { + *txrate = sn->rates[ndx].rateCode; + } + sn->packets_sent[size_bin]++; +} + +void +ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, + struct ath_desc *ds, int shortPreamble, u_int8_t rix) +{ + struct sample_node *sn = ATH_NODE_SAMPLE(an); + int rateCode = -1; + int frame_size = 0; + int size_bin = 0; + int ndx = 0; + + size_bin = size_to_bin(frame_size); // TODO: it's correct that frame_size alway 0 ? + ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */ + + if (!sn->stats[size_bin][ndx].packets_acked) { + ndx = 0; /* use the lowest bit-rate */ + } + + if (shortPreamble) { + rateCode = sn->rates[ndx].shortPreambleRateCode; + } else { + rateCode = sn->rates[ndx].rateCode; + } + ath_hal_setupxtxdesc(sc->sc_ah, ds + , rateCode, 3 /* series 1 */ + , sn->rates[0].rateCode, 3 /* series 2 */ + , 0, 0 /* series 3 */ + ); +} + +static void +update_stats(struct ath_softc *sc, struct ath_node *an, + int frame_size, + int ndx0, int tries0, + int ndx1, int tries1, + int ndx2, int tries2, + int ndx3, int tries3, + int short_tries, int tries, int status) +{ + struct sample_node *sn = ATH_NODE_SAMPLE(an); + struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); + int tt = 0; + int tries_so_far = 0; + int size_bin = 0; + int size = 0; + int rate = 0; + + size_bin = size_to_bin(frame_size); + size = bin_to_size(size_bin); + rate = sn->rates[ndx0].rate; + + tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix, + short_tries-1, + MIN(tries0, tries) - 1); + tries_so_far += tries0; + if (tries1 && tries0 < tries) { + tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix, + short_tries-1, + MIN(tries1 + tries_so_far, tries) - tries_so_far - 1); + } + tries_so_far += tries1; + + if (tries2 && tries0 + tries1 < tries) { + tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix, + short_tries-1, + MIN(tries2 + tries_so_far, tries) - tries_so_far - 1); + } + + tries_so_far += tries2; + + if (tries3 && tries0 + tries1 + tries2 < tries) { + tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix, + short_tries-1, + MIN(tries3 + tries_so_far, tries) - tries_so_far - 1); + } + if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) { + /* just average the first few packets */ + int avg_tx = sn->stats[size_bin][ndx0].average_tx_time; + int packets = sn->stats[size_bin][ndx0].total_packets; + sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1); + } else { + /* use a ewma */ + sn->stats[size_bin][ndx0].average_tx_time = + ((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) + + (tt * (100 - ssc->ath_smoothing_rate))) / 100; + } + + if (status) { + int y; + sn->stats[size_bin][ndx0].successive_failures++; + for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) { + /* also say larger packets failed since we + * assume if a small packet fails at a lower + * bit-rate then a larger one will also. + */ + sn->stats[y][ndx0].successive_failures++; + sn->stats[y][ndx0].last_tx = ticks; + sn->stats[y][ndx0].tries += tries; + sn->stats[y][ndx0].total_packets++; + } + } else { + sn->stats[size_bin][ndx0].packets_acked++; + sn->stats[size_bin][ndx0].successive_failures = 0; + } + sn->stats[size_bin][ndx0].tries += tries; + sn->stats[size_bin][ndx0].last_tx = ticks; + sn->stats[size_bin][ndx0].total_packets++; + + + if (ndx0 == sn->current_sample_ndx[size_bin]) { + DPRINTF(sc, "%s: %s size %d sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d) status %d\n", + __func__, ether_sprintf(an->an_node.ni_macaddr), + size, rate, short_tries, tries, tt, + sn->stats[size_bin][ndx0].average_tx_time, + sn->stats[size_bin][ndx0].perfect_tx_time, + status); + sn->sample_tt[size_bin] = tt; + sn->current_sample_ndx[size_bin] = -1; + } +} + +void +ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, + const struct ath_desc *ds, const struct ath_desc *ds0) +{ + struct ieee80211com *ic = &sc->sc_ic; + struct sample_node *sn = ATH_NODE_SAMPLE(an); + const struct ar5212_desc *ads = (const struct ar5212_desc *)&ds->ds_ctl0; + int final_rate, short_tries, long_tries, frame_size; + int ndx = -1; + int mrr; + + final_rate = sc->sc_hwmap[ds->ds_txstat.ts_rate &~ HAL_TXSTAT_ALTRATE].ieeerate; + short_tries = ds->ds_txstat.ts_shortretry + 1; + long_tries = ds->ds_txstat.ts_longretry + 1; + frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */ + if (frame_size == 0) /* NB: should not happen */ + frame_size = 1500; + + if (sn->num_rates <= 0) { + DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d " + "no rates yet\n", + __func__, ether_sprintf(an->an_node.ni_macaddr), + bin_to_size(size_to_bin(frame_size)), + ds->ds_txstat.ts_status, + short_tries, long_tries); + return; + } + + mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); + + if (sc->sc_mrretry && ds->ds_txstat.ts_status) { + /* this packet failed */ + DPRINTF(sc, "%s: %s size %d rate/try %d/%d %d/%d %d/%d %d/%d status %s retries (%d/%d)\n", + __func__, + ether_sprintf(an->an_node.ni_macaddr), + bin_to_size(size_to_bin(frame_size)), + sc->sc_hwmap[ads->xmit_rate0].ieeerate, + ads->xmit_tries0, + sc->sc_hwmap[ads->xmit_rate1].ieeerate, + ads->xmit_tries1, + sc->sc_hwmap[ads->xmit_rate2].ieeerate, + ads->xmit_tries2, + sc->sc_hwmap[ads->xmit_rate3].ieeerate, + ads->xmit_tries3, + ds->ds_txstat.ts_status ? "FAIL" : "OK", + short_tries, + long_tries); + } + + if (!mrr || !(ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)) { + /* only one rate was used */ + ndx = rate_to_ndx(sn, final_rate); + DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d/%d\n", + __func__, ether_sprintf(an->an_node.ni_macaddr), + bin_to_size(size_to_bin(frame_size)), + ds->ds_txstat.ts_status, + ndx, short_tries, long_tries); + if (ndx >= 0 && ndx < sn->num_rates) { + update_stats(sc, an, frame_size, + ndx, long_tries, + 0, 0, + 0, 0, + 0, 0, + short_tries, long_tries, ds->ds_txstat.ts_status); + } + } else { + int rate0, tries0, ndx0; + int rate1, tries1, ndx1; + int rate2, tries2, ndx2; + int rate3, tries3, ndx3; + int finalTSIdx = ads->final_ts_index; + + /* + * Process intermediate rates that failed. + */ + + rate0 = sc->sc_hwmap[ads->xmit_rate0].ieeerate; + tries0 = ads->xmit_tries0; + ndx0 = rate_to_ndx(sn, rate0); + + rate1 = sc->sc_hwmap[ads->xmit_rate1].ieeerate; + tries1 = ads->xmit_tries1; + ndx1 = rate_to_ndx(sn, rate1); + + rate2 = sc->sc_hwmap[ads->xmit_rate2].ieeerate; + tries2 = ads->xmit_tries2; + ndx2 = rate_to_ndx(sn, rate2); + + rate3 = sc->sc_hwmap[ads->xmit_rate3].ieeerate; + tries3 = ads->xmit_tries3; + ndx3 = rate_to_ndx(sn, rate3); + +#if 1 + DPRINTF(sc, "%s: %s size %d finaltsidx %d tries %d status %d rate/try %d/%d %d/%d %d/%d %d/%d\n", + __func__, ether_sprintf(an->an_node.ni_macaddr), + bin_to_size(size_to_bin(frame_size)), + finalTSIdx, + long_tries, + ds->ds_txstat.ts_status, + rate0, tries0, + rate1, tries1, + rate2, tries2, + rate3, tries3); +#endif + + if (tries0) { + update_stats(sc, an, frame_size, + ndx0, tries0, + ndx1, tries1, + ndx2, tries2, + ndx3, tries3, + short_tries, ds->ds_txstat.ts_longretry + 1, + long_tries > tries0); + } + + if (tries1 && finalTSIdx > 0) { + update_stats(sc, an, frame_size, + ndx1, tries1, + ndx2, tries2, + ndx3, tries3, + 0, 0, + short_tries, ds->ds_txstat.ts_longretry + 1 - tries0, + ds->ds_txstat.ts_status); + } + + if (tries2 && finalTSIdx > 1) { + update_stats(sc, an, frame_size, + ndx2, tries2, + ndx3, tries3, + 0, 0, + 0, 0, + short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1, + ds->ds_txstat.ts_status); + } + + if (tries3 && finalTSIdx > 2) { + update_stats(sc, an, frame_size, + ndx3, tries3, + 0, 0, + 0, 0, + 0, 0, + short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1 - tries2, + ds->ds_txstat.ts_status); + } + } +} + +void +ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) +{ + DPRINTF(sc, "%s: %s isnew %d\n", __func__, + ether_sprintf(an->an_node.ni_macaddr), isnew); + if (isnew) + ath_rate_ctl_reset(sc, &an->an_node); +} + +/* + * Initialize the tables for a node. + */ +static void +ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni) +{ +#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) + struct ieee80211com *ic = &sc->sc_ic; + struct ath_node *an = ATH_NODE(ni); + struct sample_node *sn = ATH_NODE_SAMPLE(an); + const HAL_RATE_TABLE *rt = sc->sc_currates; + int x, y, srate; + + KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); + sn->static_rate_ndx = -1; + if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { + /* + * A fixed rate is to be used; ic_fixed_rate is an + * index into the supported rate set. Convert this + * to the index into the negotiated rate set for + * the node. + */ + const struct ieee80211_rateset *rs = + &ic->ic_sup_rates[ic->ic_curmode]; + int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; + /* NB: the rate set is assumed sorted */ + srate = ni->ni_rates.rs_nrates - 1; + for (; srate >= 0 && RATE(srate) != r; srate--) + ; + KASSERT(srate >= 0, + ("fixed rate %d not in rate set", ic->ic_fixed_rate)); + sn->static_rate_ndx = srate; + } + + DPRINTF(sc, "%s: %s size 1600 rate/tt", __func__, ether_sprintf(ni->ni_macaddr)); + + sn->num_rates = ni->ni_rates.rs_nrates; + for (x = 0; x < ni->ni_rates.rs_nrates; x++) { + sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL; + sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate]; + if (sn->rates[x].rix == 0xff) { + DPRINTF(sc, "%s: ignore bogus rix at %d\n", + __func__, x); + continue; + } + sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode; + sn->rates[x].shortPreambleRateCode = + rt->info[sn->rates[x].rix].rateCode | + rt->info[sn->rates[x].rix].shortPreamble; + + DPRINTF(sc, " %d/%d", sn->rates[x].rate, + calc_usecs_unicast_packet(sc, 1600, sn->rates[x].rix, + 0,0)); + } + DPRINTF(sc, "%s\n", ""); + + /* set the visible bit-rate to the lowest one available */ + ni->ni_txrate = 0; + sn->num_rates = ni->ni_rates.rs_nrates; + + for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { + int size = bin_to_size(y); + int ndx = 0; + sn->packets_sent[y] = 0; + sn->current_sample_ndx[y] = -1; + sn->last_sample_ndx[y] = 0; + + for (x = 0; x < ni->ni_rates.rs_nrates; x++) { + sn->stats[y][x].successive_failures = 0; + sn->stats[y][x].tries = 0; + sn->stats[y][x].total_packets = 0; + sn->stats[y][x].packets_acked = 0; + sn->stats[y][x].last_tx = 0; + + sn->stats[y][x].perfect_tx_time = + calc_usecs_unicast_packet(sc, size, + sn->rates[x].rix, + 0, 0); + sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time; + } + + /* set the initial rate */ + for (ndx = sn->num_rates-1; ndx > 0; ndx--) { + if (sn->rates[ndx].rate <= 72) { + break; + } + } + sn->current_rate[y] = ndx; + } + + DPRINTF(sc, "%s: %s %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n", + __func__, ether_sprintf(ni->ni_macaddr), + sn->num_rates, + sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "", + sn->stats[1][0].perfect_tx_time, + sn->rates[sn->num_rates-1].rate/2, + sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "", + sn->stats[1][sn->num_rates-1].perfect_tx_time + ); + + if (sn->static_rate_ndx != -1) + ni->ni_txrate = sn->static_rate_ndx; + else + ni->ni_txrate = sn->current_rate[0]; +#undef RATE +} + +static void +rate_cb(void *arg, struct ieee80211_node *ni) +{ + struct ath_softc *sc = arg; + + ath_rate_newassoc(sc, ATH_NODE(ni), 1); +} + +/* + * Reset the rate control state for each 802.11 state transition. + */ +void +ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state) +{ + struct ieee80211com *ic = &sc->sc_ic; + + if (state == IEEE80211_S_RUN) { + if (ic->ic_opmode != IEEE80211_M_STA) { + /* + * Sync rates for associated stations and neighbors. + */ + ieee80211_iterate_nodes(&ic->ic_sta, rate_cb, sc); + } + ath_rate_newassoc(sc, ATH_NODE(ic->ic_bss), 1); + } +} + +static void +ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc) +{ + struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); + struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); + + /* XXX bounds check [0..100] */ + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "smoothing_rate", CTLFLAG_RW, &osc->ath_smoothing_rate, 0, + "rate control: retry threshold to credit rate raise (%%)"); + /* XXX bounds check [2..100] */ + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "sample_rate", CTLFLAG_RW, &osc->ath_sample_rate,0, + "rate control: # good periods before raising rate"); +} + +struct ath_ratectrl * +ath_rate_attach(struct ath_softc *sc) +{ + struct sample_softc *osc; + + DPRINTF(sc, "%s:\n", __func__); + osc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO); + if (osc == NULL) + return NULL; + osc->arc.arc_space = sizeof(struct sample_node); + osc->ath_smoothing_rate = 95; /* ewma percentage (out of 100) */ + osc->ath_sample_rate = 10; /* send a different bit-rate 1/X packets */ + ath_rate_sysctlattach(sc, osc); + return &osc->arc; +} + +void +ath_rate_detach(struct ath_ratectrl *arc) +{ + struct sample_softc *osc = (struct sample_softc *) arc; + + free(osc, M_DEVBUF); +} + +/* + * Module glue. + */ +static int +sample_modevent(module_t mod, int type, void *unused) +{ + switch (type) { + case MOD_LOAD: + if (bootverbose) + printf("ath_rate: version 1.2 <SampleRate bit-rate selection algorithm>\n"); + return 0; + case MOD_UNLOAD: + return 0; + } + return EINVAL; +} + +static moduledata_t sample_mod = { + "ath_rate", + sample_modevent, + 0 +}; +DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); +MODULE_VERSION(ath_rate, 1); +MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1); /* Atheros HAL */ +MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); diff --git a/sys/dev/ath/ath_rate/sample/sample.h b/sys/dev/ath/ath_rate/sample/sample.h new file mode 100644 index 000000000000..c5a4a23c5c0d --- /dev/null +++ b/sys/dev/ath/ath_rate/sample/sample.h @@ -0,0 +1,286 @@ +/*- + * Copyright (c) 2005 John Bicket + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any + * redistribution must be conditioned upon including a substantially + * similar Disclaimer requirement for further binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGES. + * + * $FreeBSD$ + */ + +/* + * Defintions for the Atheros Wireless LAN controller driver. + */ +#ifndef _DEV_ATH_RATE_SAMPLE_H +#define _DEV_ATH_RATE_SAMPLE_H + +/* per-device state */ +struct sample_softc { + struct ath_ratectrl arc; /* base state */ + int ath_smoothing_rate; /* ewma percentage (out of 100) */ + int ath_sample_rate; /* send a different bit-rate 1/X packets */ +}; +#define ATH_SOFTC_SAMPLE(sc) ((struct sample_softc *)sc->sc_rc) + +struct rate_info { + int rate; + int rix; + int rateCode; + int shortPreambleRateCode; +}; + + +struct rate_stats { + unsigned average_tx_time; + int successive_failures; + int tries; + int total_packets; + int packets_acked; + unsigned perfect_tx_time; /* transmit time for 0 retries */ + int last_tx; +}; + +/* + * for now, we track performance for three different packet + * size buckets + */ +#define NUM_PACKET_SIZE_BINS 3 +static int packet_size_bins[NUM_PACKET_SIZE_BINS] = {250, 1600, 3000}; + +/* per-node state */ +struct sample_node { + int static_rate_ndx; + int num_rates; + + struct rate_info rates[IEEE80211_RATE_MAXSIZE]; + + struct rate_stats stats[NUM_PACKET_SIZE_BINS][IEEE80211_RATE_MAXSIZE]; + int last_sample_ndx[NUM_PACKET_SIZE_BINS]; + + int current_sample_ndx[NUM_PACKET_SIZE_BINS]; + int packets_sent[NUM_PACKET_SIZE_BINS]; + + int current_rate[NUM_PACKET_SIZE_BINS]; + int packets_since_switch[NUM_PACKET_SIZE_BINS]; + unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS]; + + int packets_since_sample[NUM_PACKET_SIZE_BINS]; + unsigned sample_tt[NUM_PACKET_SIZE_BINS]; +}; +#define ATH_NODE_SAMPLE(an) ((struct sample_node *)&an[1]) + +#ifndef MIN +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif +#ifndef MAX +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +#endif + +#define WIFI_CW_MIN 31 +#define WIFI_CW_MAX 1023 + +struct ar5212_desc { + /* + * tx_control_0 + */ + u_int32_t frame_len:12; + u_int32_t reserved_12_15:4; + u_int32_t xmit_power:6; + u_int32_t rts_cts_enable:1; + u_int32_t veol:1; + u_int32_t clear_dest_mask:1; + u_int32_t ant_mode_xmit:4; + u_int32_t inter_req:1; + u_int32_t encrypt_key_valid:1; + u_int32_t cts_enable:1; + + /* + * tx_control_1 + */ + u_int32_t buf_len:12; + u_int32_t more:1; + u_int32_t encrypt_key_index:7; + u_int32_t frame_type:4; + u_int32_t no_ack:1; + u_int32_t comp_proc:2; + u_int32_t comp_iv_len:2; + u_int32_t comp_icv_len:2; + u_int32_t reserved_31:1; + + /* + * tx_control_2 + */ + u_int32_t rts_duration:15; + u_int32_t duration_update_enable:1; + u_int32_t xmit_tries0:4; + u_int32_t xmit_tries1:4; + u_int32_t xmit_tries2:4; + u_int32_t xmit_tries3:4; + + /* + * tx_control_3 + */ + u_int32_t xmit_rate0:5; + u_int32_t xmit_rate1:5; + u_int32_t xmit_rate2:5; + u_int32_t xmit_rate3:5; + u_int32_t rts_cts_rate:5; + u_int32_t reserved_25_31:7; + + /* + * tx_status_0 + */ + u_int32_t frame_xmit_ok:1; + u_int32_t excessive_retries:1; + u_int32_t fifo_underrun:1; + u_int32_t filtered:1; + u_int32_t rts_fail_count:4; + u_int32_t data_fail_count:4; + u_int32_t virt_coll_count:4; + u_int32_t send_timestamp:16; + + /* + * tx_status_1 + */ + u_int32_t done:1; + u_int32_t seq_num:12; + u_int32_t ack_sig_strength:8; + u_int32_t final_ts_index:2; + u_int32_t comp_success:1; + u_int32_t xmit_antenna:1; + u_int32_t reserved_25_31_x:7; +} __packed; + +/* + * Calculate the transmit duration of a frame. + */ +static unsigned calc_usecs_unicast_packet(struct ath_softc *sc, + int length, + int rix, int short_retries, int long_retries) { + const HAL_RATE_TABLE *rt = sc->sc_currates; + int rts, cts; + + unsigned t_slot = 20; + unsigned t_difs = 50; + unsigned t_sifs = 10; + struct ieee80211com *ic = &sc->sc_ic; + int tt = 0; + int x = 0; + int cw = WIFI_CW_MIN; + int cix = rt->info[rix].controlRate; + + KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); + + if (!rt->info[rix].rateKbps) { + printf("rix %d (%d) bad ratekbps %d mode %u\n", + rix, rt->info[rix].dot11Rate, + rt->info[rix].rateKbps, + sc->sc_curmode); + + return 0; + } + /* + * XXX getting mac/phy level timings should be fixed for turbo + * rates, and there is probably a way to get this from the + * hal... + */ + switch (rt->info[rix].phy) { + case IEEE80211_T_OFDM: + t_slot = 9; + t_sifs = 16; + t_difs = 28; + /* fall through */ + case IEEE80211_T_TURBO: + t_slot = 9; + t_sifs = 8; + t_difs = 28; + break; + case IEEE80211_T_DS: + /* fall through to default */ + default: + /* pg 205 ieee.802.11.pdf */ + t_slot = 20; + t_difs = 50; + t_sifs = 10; + } + + rts = cts = 0; + + if ((ic->ic_flags & IEEE80211_F_USEPROT) && + rt->info[rix].phy == IEEE80211_T_OFDM) { + if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) + rts = 1; + else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) + cts = 1; + + cix = rt->info[sc->sc_protrix].controlRate; + + } + + if (0 /*length > ic->ic_rtsthreshold */) { + rts = 1; + } + + if (rts || cts) { + int ctsrate = rt->info[cix].rateCode; + int ctsduration = 0; + + if (!rt->info[cix].rateKbps) { + printf("cix %d (%d) bad ratekbps %d mode %u\n", + cix, rt->info[cix].dot11Rate, + rt->info[cix].rateKbps, + sc->sc_curmode); + return 0; + } + + ctsrate |= rt->info[cix].shortPreamble; + if (rts) /* SIFS + CTS */ + ctsduration += rt->info[cix].spAckDuration; + + ctsduration += ath_hal_computetxtime(sc->sc_ah, + rt, length, rix, AH_TRUE); + + if (cts) /* SIFS + ACK */ + ctsduration += rt->info[cix].spAckDuration; + + tt += (short_retries + 1) * ctsduration; + } + tt += t_difs; + tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration); + tt += (long_retries+1)*ath_hal_computetxtime(sc->sc_ah, rt, length, + rix, AH_TRUE); + for (x = 0; x <= short_retries + long_retries; x++) { + cw = MIN(WIFI_CW_MAX, (cw + 1) * 2); + tt += (t_slot * cw/2); + } + return tt; +} +#endif /* _DEV_ATH_RATE_SAMPLE_H */ |
