2 * Copyright (c) 2004 INRIA
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14 * redistribution must be conditioned upon including a substantially
15 * similar Disclaimer requirement for further binary redistribution.
16 * 3. Neither the names of the above-listed copyright holders nor the names
17 * of any contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
30 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGES.
37 * $FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.12 2006/12/13 19:34:34 sam Exp $
38 * $DragonFly: src/sys/dev/netif/ath/rate_amrr/amrr.c,v 1.5 2007/02/22 05:17:09 sephe Exp $
42 * AMRR rate control. See:
43 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
44 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
45 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/module.h>
52 #include <sys/kernel.h>
54 #include <sys/errno.h>
55 #include <sys/serialize.h>
58 #include <sys/socket.h>
61 #include <net/if_media.h>
62 #include <net/if_arp.h>
64 #include <netproto/802_11/ieee80211_var.h>
68 #include <dev/netif/ath/ath/if_athvar.h>
69 #include <dev/netif/ath/rate_amrr/amrr.h>
70 #include <contrib/dev/ath/ah_desc.h>
74 #define DPRINTF(sc, _fmt, ...) do { \
75 if (sc->sc_debug & 0x10) \
76 kprintf(_fmt, __VA_ARGS__); \
79 #define DPRINTF(sc, _fmt, ...)
82 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
83 static int ath_rate_max_success_threshold = 10;
84 static int ath_rate_min_success_threshold = 1;
86 static void ath_ratectl(void *);
87 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
89 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
90 static void ath_rate_ctl(void *, struct ieee80211_node *);
93 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
95 /* NB: assumed to be zero'd by caller */
96 ath_rate_update(sc, &an->an_node, 0);
100 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
105 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
106 int shortPreamble, size_t frameLen,
107 uint8_t *rix, int *try0, uint8_t *txrate)
109 struct amrr_node *amn = ATH_NODE_AMRR(an);
111 *rix = amn->amn_tx_rix0;
112 *try0 = amn->amn_tx_try0;
114 *txrate = amn->amn_tx_rate0sp;
116 *txrate = amn->amn_tx_rate0;
120 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
121 struct ath_desc *ds, int shortPreamble, uint8_t rix)
123 struct amrr_node *amn = ATH_NODE_AMRR(an);
125 ath_hal_setupxtxdesc(sc->sc_ah, ds
126 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
127 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
128 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
133 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
134 const struct ath_buf *bf)
136 struct amrr_node *amn = ATH_NODE_AMRR(an);
137 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
138 int sr = ts->ts_shortretry;
139 int lr = ts->ts_longretry;
140 int retry_count = sr + lr;
142 amn->amn_tx_try0_cnt++;
143 if (retry_count == 1) {
144 amn->amn_tx_try1_cnt++;
145 } else if (retry_count == 2) {
146 amn->amn_tx_try1_cnt++;
147 amn->amn_tx_try2_cnt++;
148 } else if (retry_count == 3) {
149 amn->amn_tx_try1_cnt++;
150 amn->amn_tx_try2_cnt++;
151 amn->amn_tx_try3_cnt++;
152 } else if (retry_count > 3) {
153 amn->amn_tx_try1_cnt++;
154 amn->amn_tx_try2_cnt++;
155 amn->amn_tx_try3_cnt++;
156 amn->amn_tx_failure_cnt++;
161 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
164 ath_rate_ctl_start(sc, &an->an_node);
168 node_reset (struct amrr_node *amn)
170 amn->amn_tx_try0_cnt = 0;
171 amn->amn_tx_try1_cnt = 0;
172 amn->amn_tx_try2_cnt = 0;
173 amn->amn_tx_try3_cnt = 0;
174 amn->amn_tx_failure_cnt = 0;
175 amn->amn_success = 0;
176 amn->amn_recovery = 0;
177 amn->amn_success_threshold = ath_rate_min_success_threshold;
182 * The code below assumes that we are dealing with hardware multi rate retry
183 * I have no idea what will happen if you try to use this module with another
184 * type of hardware. Your machine might catch fire or it might work with
185 * horrible performance...
188 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
190 struct ath_node *an = ATH_NODE(ni);
191 struct amrr_node *amn = ATH_NODE_AMRR(an);
192 const HAL_RATE_TABLE *rt = sc->sc_currates;
195 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
197 DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
198 __func__, ni->ni_macaddr, ":",
199 ni->ni_rates.rs_nrates > 0 ?
200 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
202 ni->ni_txrate = rate;
204 * Before associating a node has no rate set setup
205 * so we can't calculate any transmit codes to use.
206 * This is ok since we should never be sending anything
207 * but management frames and those always go at the
208 * lowest hardware rate.
210 if (ni->ni_rates.rs_nrates > 0) {
211 amn->amn_tx_rix0 = sc->sc_rixmap[
212 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
213 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
214 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
215 rt->info[amn->amn_tx_rix0].shortPreamble;
216 if (sc->sc_mrretry) {
217 amn->amn_tx_try0 = 1;
218 amn->amn_tx_try1 = 1;
219 amn->amn_tx_try2 = 1;
220 amn->amn_tx_try3 = 1;
223 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
224 amn->amn_tx_rate1 = rt->info[rix].rateCode;
225 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
226 rt->info[rix].shortPreamble;
228 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
232 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
233 amn->amn_tx_rate2 = rt->info[rix].rateCode;
234 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
235 rt->info[rix].shortPreamble;
237 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
240 /* NB: only do this if we didn't already do it above */
241 amn->amn_tx_rate3 = rt->info[0].rateCode;
242 amn->amn_tx_rate3sp =
243 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
245 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
248 amn->amn_tx_try0 = ATH_TXMAXTRY;
249 /* theorically, these statements are useless because
250 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
252 amn->amn_tx_try1 = 0;
253 amn->amn_tx_try2 = 0;
254 amn->amn_tx_try3 = 0;
255 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
256 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
257 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
264 * Set the starting transmit rate for a node.
267 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
269 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
270 struct ieee80211com *ic = &sc->sc_ic;
273 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
274 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
276 * No fixed rate is requested. For 11b start with
277 * the highest negotiated rate; otherwise, for 11g
278 * and 11a, we start "in the middle" at 24Mb or 36Mb.
280 srate = ni->ni_rates.rs_nrates - 1;
281 if (sc->sc_curmode != IEEE80211_MODE_11B) {
283 * Scan the negotiated rate set to find the
286 /* NB: the rate set is assumed sorted */
287 for (; srate >= 0 && RATE(srate) > 72; srate--)
289 KASSERT(srate >= 0, ("bogus rate set"));
293 * A fixed rate is to be used; ic_fixed_rate is an
294 * index into the supported rate set. Convert this
295 * to the index into the negotiated rate set for
296 * the node. We know the rate is there because the
297 * rate set is checked when the station associates.
299 const struct ieee80211_rateset *rs =
300 &ic->ic_sup_rates[ic->ic_curmode];
301 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
302 /* NB: the rate set is assumed sorted */
303 srate = ni->ni_rates.rs_nrates - 1;
304 for (; srate >= 0 && RATE(srate) != r; srate--)
307 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
309 ath_rate_update(sc, ni, srate);
314 ath_rate_cb(void *arg, struct ieee80211_node *ni)
316 struct ath_softc *sc = arg;
318 ath_rate_update(sc, ni, 0);
322 * Reset the rate control state for each 802.11 state transition.
325 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
327 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
328 struct ieee80211com *ic = &sc->sc_ic;
329 struct ifnet *ifp = &ic->ic_if;
330 struct ieee80211_node *ni;
332 ASSERT_SERIALIZED(ifp->if_serializer);
334 if (state == IEEE80211_S_INIT) {
335 callout_stop(&asc->timer);
338 if (ic->ic_opmode == IEEE80211_M_STA) {
340 * Reset local xmit state; this is really only
341 * meaningful when operating in station mode.
344 if (state == IEEE80211_S_RUN) {
345 ath_rate_ctl_start(sc, ni);
347 ath_rate_update(sc, ni, 0);
351 * When operating as a station the node table holds
352 * the AP's that were discovered during scanning.
353 * For any other operating mode we want to reset the
354 * tx rate state of each node.
356 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
357 ath_rate_update(sc, ic->ic_bss, 0);
359 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
360 state == IEEE80211_S_RUN) {
363 * Start the background rate control thread if we
364 * are not configured to use a fixed xmit rate.
366 interval = ath_rateinterval;
367 if (ic->ic_opmode == IEEE80211_M_STA)
369 callout_reset(&asc->timer, (interval * hz) / 1000,
375 * Examine and potentially adjust the transmit rate.
378 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
380 struct ath_softc *sc = arg;
381 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
384 #define is_success(amn) \
385 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
386 #define is_enough(amn) \
387 (amn->amn_tx_try0_cnt > 10)
388 #define is_failure(amn) \
389 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
390 #define is_max_rate(ni) \
391 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
392 #define is_min_rate(ni) \
395 old_rate = ni->ni_txrate;
397 DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
398 amn->amn_tx_try0_cnt,
399 amn->amn_tx_try1_cnt,
400 amn->amn_tx_try2_cnt,
401 amn->amn_tx_try3_cnt,
402 amn->amn_success_threshold);
403 if (is_success (amn) && is_enough (amn)) {
405 if (amn->amn_success == amn->amn_success_threshold &&
407 amn->amn_recovery = 1;
408 amn->amn_success = 0;
410 DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
412 amn->amn_recovery = 0;
414 } else if (is_failure (amn)) {
415 amn->amn_success = 0;
416 if (!is_min_rate (ni)) {
417 if (amn->amn_recovery) {
418 /* recovery failure. */
419 amn->amn_success_threshold *= 2;
420 amn->amn_success_threshold = min (amn->amn_success_threshold,
421 (u_int)ath_rate_max_success_threshold);
422 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
424 /* simple failure. */
425 amn->amn_success_threshold = ath_rate_min_success_threshold;
426 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
428 amn->amn_recovery = 0;
431 amn->amn_recovery = 0;
435 if (is_enough (amn) || old_rate != ni->ni_txrate) {
436 /* reset counters. */
437 amn->amn_tx_try0_cnt = 0;
438 amn->amn_tx_try1_cnt = 0;
439 amn->amn_tx_try2_cnt = 0;
440 amn->amn_tx_try3_cnt = 0;
441 amn->amn_tx_failure_cnt = 0;
443 if (old_rate != ni->ni_txrate) {
444 ath_rate_update(sc, ni, ni->ni_txrate);
449 ath_ratectl(void *arg)
451 struct ifnet *ifp = arg;
452 struct ath_softc *sc = ifp->if_softc;
453 struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
454 struct ieee80211com *ic = &sc->sc_ic;
457 lwkt_serialize_enter(ifp->if_serializer);
459 if (ifp->if_flags & IFF_RUNNING) {
460 sc->sc_stats.ast_rate_calls++;
462 if (ic->ic_opmode == IEEE80211_M_STA)
463 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
465 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
467 interval = ath_rateinterval;
468 if (ic->ic_opmode == IEEE80211_M_STA)
470 callout_reset(&asc->timer, (interval * hz) / 1000, ath_ratectl, ifp);
472 lwkt_serialize_exit(ifp->if_serializer);
476 ath_rate_sysctlattach(struct ath_softc *sc)
478 struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
479 struct sysctl_oid *tree = sc->sc_sysctl_tree;
481 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
482 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
483 "rate control: operation interval (ms)");
484 /* XXX bounds check values */
485 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
486 "max_sucess_threshold", CTLFLAG_RW,
487 &ath_rate_max_success_threshold, 0, "");
488 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
489 "min_sucess_threshold", CTLFLAG_RW,
490 &ath_rate_min_success_threshold, 0, "");
493 struct ath_ratectrl *
494 ath_rate_attach(struct ath_softc *sc)
496 struct amrr_softc *asc;
498 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
501 asc->arc.arc_space = sizeof(struct amrr_node);
502 callout_init(&asc->timer);
503 ath_rate_sysctlattach(sc);
509 ath_rate_detach(struct ath_ratectrl *arc)
511 struct amrr_softc *asc = (struct amrr_softc *)arc;
513 kfree(asc, M_DEVBUF);
517 ath_rate_stop(struct ath_ratectrl *arc)
519 struct amrr_softc *asc = (struct amrr_softc *)arc;
521 /* ASSERT_SERIALIZED */
522 callout_stop(&asc->timer);
529 amrr_modevent(module_t mod, int type, void *unused)
534 kprintf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
542 static moduledata_t amrr_mod = {
547 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
548 MODULE_VERSION(ath_rate, 1);
549 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);