2 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Atsushi Onoe's rate control algorithm.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/errno.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
50 #include <sys/socket.h>
53 #include <net/if_media.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h> /* XXX for ether_sprintf */
57 #include <net80211/ieee80211_var.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
66 #include <dev/ath/if_athvar.h>
67 #include <dev/ath/ath_rate/onoe/onoe.h>
68 #include <dev/ath/ath_hal/ah_desc.h>
73 ATH_DEBUG_RATE = 0x00000010, /* rate control */
75 #define DPRINTF(sc, _fmt, ...) do { \
76 if (sc->sc_debug & ATH_DEBUG_RATE) \
77 printf(_fmt, __VA_ARGS__); \
80 #define DPRINTF(sc, _fmt, ...)
84 * Default parameters for the rate control algorithm. These are
85 * all tunable with sysctls. The rate controller runs periodically
86 * (each ath_rateinterval ms) analyzing transmit statistics for each
87 * neighbor/station (when operating in station mode this is only the AP).
88 * If transmits look to be working well over a sampling period then
89 * it gives a "raise rate credit". If transmits look to not be working
90 * well than it deducts a credit. If the credits cross a threshold then
91 * the transmit rate is raised. Various error conditions force the
92 * the transmit rate to be dropped.
94 * The decision to issue/deduct a credit is based on the errors and
95 * retries accumulated over the sampling period. ath_rate_raise defines
96 * the percent of retransmits for which a credit is issued/deducted.
97 * ath_rate_raise_threshold defines the threshold on credits at which
98 * the transmit rate is increased.
100 * XXX this algorithm is flawed.
102 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
103 static int ath_rate_raise = 10; /* add credit threshold */
104 static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */
106 static void ath_ratectl(void *);
107 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
109 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
110 static void ath_rate_ctl(void *, struct ieee80211_node *);
113 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
115 /* NB: assumed to be zero'd by caller */
116 ath_rate_update(sc, &an->an_node, 0);
120 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
125 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
126 int shortPreamble, size_t frameLen,
127 u_int8_t *rix, int *try0, u_int8_t *txrate)
129 struct onoe_node *on = ATH_NODE_ONOE(an);
131 *rix = on->on_tx_rix0;
132 *try0 = on->on_tx_try0;
134 *txrate = on->on_tx_rate0sp;
136 *txrate = on->on_tx_rate0;
140 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
141 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
143 struct onoe_node *on = ATH_NODE_ONOE(an);
145 ath_hal_setupxtxdesc(sc->sc_ah, ds
146 , on->on_tx_rate1sp, 2 /* series 1 */
147 , on->on_tx_rate2sp, 2 /* series 2 */
148 , on->on_tx_rate3sp, 2 /* series 3 */
153 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
154 const struct ath_buf *bf)
156 struct onoe_node *on = ATH_NODE_ONOE(an);
157 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
159 if (ts->ts_status == 0)
163 on->on_tx_retr += ts->ts_shortretry
168 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
171 ath_rate_ctl_start(sc, &an->an_node);
175 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
177 struct ath_node *an = ATH_NODE(ni);
178 struct onoe_node *on = ATH_NODE_ONOE(an);
179 const HAL_RATE_TABLE *rt = sc->sc_currates;
182 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
184 DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
185 __func__, ether_sprintf(ni->ni_macaddr),
186 ni->ni_rates.rs_nrates > 0 ?
187 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
189 ni->ni_txrate = rate;
191 * Before associating a node has no rate set setup
192 * so we can't calculate any transmit codes to use.
193 * This is ok since we should never be sending anything
194 * but management frames and those always go at the
195 * lowest hardware rate.
197 if (ni->ni_rates.rs_nrates == 0)
199 on->on_tx_rix0 = sc->sc_rixmap[
200 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
201 on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
203 on->on_tx_rate0sp = on->on_tx_rate0 |
204 rt->info[on->on_tx_rix0].shortPreamble;
205 if (sc->sc_mrretry) {
207 * Hardware supports multi-rate retry; setup two
208 * step-down retry rates and make the lowest rate
209 * be the ``last chance''. We use 4, 2, 2, 2 tries
210 * respectively (4 is set here, the rest are fixed
211 * in the xmit routine).
213 on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */
216 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
217 on->on_tx_rate1 = rt->info[rix].rateCode;
218 on->on_tx_rate1sp = on->on_tx_rate1 |
219 rt->info[rix].shortPreamble;
221 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
225 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
226 on->on_tx_rate2 = rt->info[rix].rateCode;
227 on->on_tx_rate2sp = on->on_tx_rate2 |
228 rt->info[rix].shortPreamble;
230 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
233 /* NB: only do this if we didn't already do it above */
234 on->on_tx_rate3 = rt->info[0].rateCode;
236 on->on_tx_rate3 | rt->info[0].shortPreamble;
238 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
241 on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */
242 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
243 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
244 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
247 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
251 * Set the starting transmit rate for a node.
254 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
256 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
257 struct ieee80211com *ic = &sc->sc_ic;
260 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
261 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
263 * No fixed rate is requested. For 11b start with
264 * the highest negotiated rate; otherwise, for 11g
265 * and 11a, we start "in the middle" at 24Mb or 36Mb.
267 srate = ni->ni_rates.rs_nrates - 1;
268 if (sc->sc_curmode != IEEE80211_MODE_11B) {
270 * Scan the negotiated rate set to find the
273 /* NB: the rate set is assumed sorted */
274 for (; srate >= 0 && RATE(srate) > 72; srate--)
279 * A fixed rate is to be used; ic_fixed_rate is the
280 * IEEE code for this rate (sans basic bit). Convert this
281 * to the index into the negotiated rate set for
282 * the node. We know the rate is there because the
283 * rate set is checked when the station associates.
285 /* NB: the rate set is assumed sorted */
286 srate = ni->ni_rates.rs_nrates - 1;
287 for (; srate >= 0 && RATE(srate) != ic->ic_fixed_rate; srate--)
291 * The selected rate may not be available due to races
292 * and mode settings. Also orphaned nodes created in
293 * adhoc mode may not have any rate set so this lookup
294 * can fail. This is not fatal.
296 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
301 ath_rate_cb(void *arg, struct ieee80211_node *ni)
303 struct ath_softc *sc = arg;
305 ath_rate_update(sc, ni, 0);
309 * Reset the rate control state for each 802.11 state transition.
312 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
314 struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
315 struct ieee80211com *ic = &sc->sc_ic;
316 struct ieee80211_node *ni;
318 if (state == IEEE80211_S_INIT) {
319 callout_stop(&osc->timer);
322 if (ic->ic_opmode == IEEE80211_M_STA) {
324 * Reset local xmit state; this is really only
325 * meaningful when operating in station mode.
328 if (state == IEEE80211_S_RUN) {
329 ath_rate_ctl_start(sc, ni);
331 ath_rate_update(sc, ni, 0);
335 * When operating as a station the node table holds
336 * the AP's that were discovered during scanning.
337 * For any other operating mode we want to reset the
338 * tx rate state of each node.
340 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
341 ath_rate_update(sc, ic->ic_bss, 0);
343 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
344 state == IEEE80211_S_RUN) {
347 * Start the background rate control thread if we
348 * are not configured to use a fixed xmit rate.
350 interval = ath_rateinterval;
351 if (ic->ic_opmode == IEEE80211_M_STA)
353 callout_reset(&osc->timer, (interval * hz) / 1000,
354 ath_ratectl, sc->sc_ifp);
359 * Examine and potentially adjust the transmit rate.
362 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
364 struct ath_softc *sc = arg;
365 struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
366 struct ieee80211_rateset *rs = &ni->ni_rates;
367 int dir = 0, nrate, enough;
371 * XXX: very primitive version.
373 enough = (on->on_tx_ok + on->on_tx_err >= 10);
375 /* no packet reached -> down */
376 if (on->on_tx_err > 0 && on->on_tx_ok == 0)
379 /* all packets needs retry in average -> down */
380 if (enough && on->on_tx_ok < on->on_tx_retr)
383 /* no error and less than rate_raise% of packets need retry -> up */
384 if (enough && on->on_tx_err == 0 &&
385 on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
388 DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
389 ether_sprintf(ni->ni_macaddr),
390 on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
391 on->on_tx_upper, dir);
393 nrate = ni->ni_txrate;
396 if (enough && on->on_tx_upper > 0)
402 sc->sc_stats.ast_rate_drop++;
407 /* raise rate if we hit rate_raise_threshold */
408 if (++on->on_tx_upper < ath_rate_raise_threshold)
411 if (nrate + 1 < rs->rs_nrates) {
413 sc->sc_stats.ast_rate_raise++;
418 if (nrate != ni->ni_txrate) {
419 DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
421 (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
422 (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
423 on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
424 ath_rate_update(sc, ni, nrate);
426 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
430 ath_ratectl(void *arg)
432 struct ifnet *ifp = arg;
433 struct ath_softc *sc = ifp->if_softc;
434 struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
435 struct ieee80211com *ic = &sc->sc_ic;
438 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
439 sc->sc_stats.ast_rate_calls++;
441 if (ic->ic_opmode == IEEE80211_M_STA)
442 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
444 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
446 interval = ath_rateinterval;
447 if (ic->ic_opmode == IEEE80211_M_STA)
449 callout_reset(&osc->timer, (interval * hz) / 1000,
450 ath_ratectl, sc->sc_ifp);
454 ath_rate_sysctlattach(struct ath_softc *sc)
456 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
457 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
459 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
460 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
461 "rate control: operation interval (ms)");
462 /* XXX bounds check values */
463 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
464 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
465 "rate control: retry threshold to credit rate raise (%%)");
466 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
467 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
468 "rate control: # good periods before raising rate");
471 struct ath_ratectrl *
472 ath_rate_attach(struct ath_softc *sc)
474 struct onoe_softc *osc;
476 osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
479 osc->arc.arc_space = sizeof(struct onoe_node);
480 callout_init(&osc->timer, CALLOUT_MPSAFE);
481 ath_rate_sysctlattach(sc);
487 ath_rate_detach(struct ath_ratectrl *arc)
489 struct onoe_softc *osc = (struct onoe_softc *) arc;
491 callout_drain(&osc->timer);