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.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/errno.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
51 #include <sys/socket.h>
54 #include <net/if_media.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h> /* XXX for ether_sprintf */
58 #include <net80211/ieee80211_var.h>
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
67 #include <dev/ath/if_athvar.h>
68 #include <dev/ath/ath_rate/onoe/onoe.h>
69 #include <dev/ath/ath_hal/ah_desc.h>
72 * Default parameters for the rate control algorithm. These are
73 * all tunable with sysctls. The rate controller runs periodically
74 * (each ath_rateinterval ms) analyzing transmit statistics for each
75 * neighbor/station (when operating in station mode this is only the AP).
76 * If transmits look to be working well over a sampling period then
77 * it gives a "raise rate credit". If transmits look to not be working
78 * well than it deducts a credit. If the credits cross a threshold then
79 * the transmit rate is raised. Various error conditions force the
80 * the transmit rate to be dropped.
82 * The decision to issue/deduct a credit is based on the errors and
83 * retries accumulated over the sampling period. ath_rate_raise defines
84 * the percent of retransmits for which a credit is issued/deducted.
85 * ath_rate_raise_threshold defines the threshold on credits at which
86 * the transmit rate is increased.
88 * XXX this algorithm is flawed.
90 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
91 static int ath_rate_raise = 10; /* add credit threshold */
92 static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */
94 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
96 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
97 static void ath_rate_ctl(void *, struct ieee80211_node *);
100 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
102 /* NB: assumed to be zero'd by caller */
106 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
111 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
112 int shortPreamble, size_t frameLen,
113 u_int8_t *rix, int *try0, u_int8_t *txrate)
115 struct onoe_node *on = ATH_NODE_ONOE(an);
117 *rix = on->on_tx_rix0;
118 *try0 = on->on_tx_try0;
120 *txrate = on->on_tx_rate0sp;
122 *txrate = on->on_tx_rate0;
126 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
127 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
129 struct onoe_node *on = ATH_NODE_ONOE(an);
131 ath_hal_setupxtxdesc(sc->sc_ah, ds
132 , on->on_tx_rate1sp, 2 /* series 1 */
133 , on->on_tx_rate2sp, 2 /* series 2 */
134 , on->on_tx_rate3sp, 2 /* series 3 */
139 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
140 const struct ath_buf *bf)
142 struct onoe_node *on = ATH_NODE_ONOE(an);
143 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
145 if (ts->ts_status == 0)
149 on->on_tx_retr += ts->ts_shortretry
151 if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
152 ath_rate_ctl(sc, &an->an_node);
153 on->on_ticks = ticks;
158 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
161 ath_rate_ctl_start(sc, &an->an_node);
165 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
167 struct ath_node *an = ATH_NODE(ni);
168 struct onoe_node *on = ATH_NODE_ONOE(an);
169 struct ieee80211vap *vap = ni->ni_vap;
170 const HAL_RATE_TABLE *rt = sc->sc_currates;
173 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
175 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
176 "%s: set xmit rate to %dM", __func__,
177 ni->ni_rates.rs_nrates > 0 ?
178 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
181 * Before associating a node has no rate set setup
182 * so we can't calculate any transmit codes to use.
183 * This is ok since we should never be sending anything
184 * but management frames and those always go at the
185 * lowest hardware rate.
187 if (ni->ni_rates.rs_nrates == 0)
190 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
191 on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
192 on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
194 on->on_tx_rate0sp = on->on_tx_rate0 |
195 rt->info[on->on_tx_rix0].shortPreamble;
196 if (sc->sc_mrretry) {
198 * Hardware supports multi-rate retry; setup two
199 * step-down retry rates and make the lowest rate
200 * be the ``last chance''. We use 4, 2, 2, 2 tries
201 * respectively (4 is set here, the rest are fixed
202 * in the xmit routine).
204 on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */
207 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
208 on->on_tx_rate1 = rt->info[rix].rateCode;
209 on->on_tx_rate1sp = on->on_tx_rate1 |
210 rt->info[rix].shortPreamble;
212 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
216 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
217 on->on_tx_rate2 = rt->info[rix].rateCode;
218 on->on_tx_rate2sp = on->on_tx_rate2 |
219 rt->info[rix].shortPreamble;
221 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
224 /* NB: only do this if we didn't already do it above */
225 on->on_tx_rate3 = rt->info[0].rateCode;
227 on->on_tx_rate3 | rt->info[0].shortPreamble;
229 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
232 on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */
233 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
234 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
235 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
238 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
240 on->on_interval = ath_rateinterval;
241 if (vap->iv_opmode == IEEE80211_M_STA)
242 on->on_interval /= 2;
243 on->on_interval = (on->on_interval * hz) / 1000;
247 * Set the starting transmit rate for a node.
250 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
252 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
253 const struct ieee80211_txparam *tp = ni->ni_txparms;
256 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
257 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
259 * No fixed rate is requested. For 11b start with
260 * the highest negotiated rate; otherwise, for 11g
261 * and 11a, we start "in the middle" at 24Mb or 36Mb.
263 srate = ni->ni_rates.rs_nrates - 1;
264 if (sc->sc_curmode != IEEE80211_MODE_11B) {
266 * Scan the negotiated rate set to find the
269 /* NB: the rate set is assumed sorted */
270 for (; srate >= 0 && RATE(srate) > 72; srate--)
275 * A fixed rate is to be used; ic_fixed_rate is the
276 * IEEE code for this rate (sans basic bit). Convert this
277 * to the index into the negotiated rate set for
278 * the node. We know the rate is there because the
279 * rate set is checked when the station associates.
281 /* NB: the rate set is assumed sorted */
282 srate = ni->ni_rates.rs_nrates - 1;
283 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
287 * The selected rate may not be available due to races
288 * and mode settings. Also orphaned nodes created in
289 * adhoc mode may not have any rate set so this lookup
290 * can fail. This is not fatal.
292 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
297 * Examine and potentially adjust the transmit rate.
300 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
302 struct ath_softc *sc = arg;
303 struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
304 struct ieee80211_rateset *rs = &ni->ni_rates;
305 int dir = 0, nrate, enough;
309 * XXX: very primitive version.
311 enough = (on->on_tx_ok + on->on_tx_err >= 10);
313 /* no packet reached -> down */
314 if (on->on_tx_err > 0 && on->on_tx_ok == 0)
317 /* all packets needs retry in average -> down */
318 if (enough && on->on_tx_ok < on->on_tx_retr)
321 /* no error and less than rate_raise% of packets need retry -> up */
322 if (enough && on->on_tx_err == 0 &&
323 on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
326 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
327 "ok %d err %d retr %d upper %d dir %d",
328 on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
333 if (enough && on->on_tx_upper > 0)
339 sc->sc_stats.ast_rate_drop++;
344 /* raise rate if we hit rate_raise_threshold */
345 if (++on->on_tx_upper < ath_rate_raise_threshold)
348 if (nrate + 1 < rs->rs_nrates) {
350 sc->sc_stats.ast_rate_raise++;
355 if (nrate != on->on_rix) {
356 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
357 "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
359 (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
360 on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
361 ath_rate_update(sc, ni, nrate);
363 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
367 ath_rate_sysctlattach(struct ath_softc *sc)
369 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
370 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
372 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
373 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
374 "rate control: operation interval (ms)");
375 /* XXX bounds check values */
376 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
377 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
378 "rate control: retry threshold to credit rate raise (%%)");
379 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
380 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
381 "rate control: # good periods before raising rate");
384 struct ath_ratectrl *
385 ath_rate_attach(struct ath_softc *sc)
387 struct onoe_softc *osc;
389 osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
392 osc->arc.arc_space = sizeof(struct onoe_node);
393 ath_rate_sysctlattach(sc);
399 ath_rate_detach(struct ath_ratectrl *arc)
401 struct onoe_softc *osc = (struct onoe_softc *) arc;