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.8.2.3 2006/02/24 19:51:11 sam Exp $
38 * $DragonFly: src/sys/dev/netif/ath/rate_amrr/amrr.c,v 1.4 2006/12/22 23:26:18 swildner 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_desc *ds, const struct ath_desc *ds0)
136 struct amrr_node *amn = ATH_NODE_AMRR(an);
137 int sr = ds->ds_txstat.ts_shortretry;
138 int lr = ds->ds_txstat.ts_longretry;
139 int retry_count = sr + lr;
141 amn->amn_tx_try0_cnt++;
142 if (retry_count == 1) {
143 amn->amn_tx_try1_cnt++;
144 } else if (retry_count == 2) {
145 amn->amn_tx_try1_cnt++;
146 amn->amn_tx_try2_cnt++;
147 } else if (retry_count == 3) {
148 amn->amn_tx_try1_cnt++;
149 amn->amn_tx_try2_cnt++;
150 amn->amn_tx_try3_cnt++;
151 } else if (retry_count > 3) {
152 amn->amn_tx_try1_cnt++;
153 amn->amn_tx_try2_cnt++;
154 amn->amn_tx_try3_cnt++;
155 amn->amn_tx_failure_cnt++;
160 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
163 ath_rate_ctl_start(sc, &an->an_node);
167 node_reset (struct amrr_node *amn)
169 amn->amn_tx_try0_cnt = 0;
170 amn->amn_tx_try1_cnt = 0;
171 amn->amn_tx_try2_cnt = 0;
172 amn->amn_tx_try3_cnt = 0;
173 amn->amn_tx_failure_cnt = 0;
174 amn->amn_success = 0;
175 amn->amn_recovery = 0;
176 amn->amn_success_threshold = ath_rate_min_success_threshold;
181 * The code below assumes that we are dealing with hardware multi rate retry
182 * I have no idea what will happen if you try to use this module with another
183 * type of hardware. Your machine might catch fire or it might work with
184 * horrible performance...
187 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
189 struct ath_node *an = ATH_NODE(ni);
190 struct amrr_node *amn = ATH_NODE_AMRR(an);
191 const HAL_RATE_TABLE *rt = sc->sc_currates;
194 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
196 DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
197 __func__, ni->ni_macaddr, ":",
198 ni->ni_rates.rs_nrates > 0 ?
199 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
201 ni->ni_txrate = rate;
203 * Before associating a node has no rate set setup
204 * so we can't calculate any transmit codes to use.
205 * This is ok since we should never be sending anything
206 * but management frames and those always go at the
207 * lowest hardware rate.
209 if (ni->ni_rates.rs_nrates > 0) {
210 amn->amn_tx_rix0 = sc->sc_rixmap[
211 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
212 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
213 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
214 rt->info[amn->amn_tx_rix0].shortPreamble;
215 if (sc->sc_mrretry) {
216 amn->amn_tx_try0 = 1;
217 amn->amn_tx_try1 = 1;
218 amn->amn_tx_try2 = 1;
219 amn->amn_tx_try3 = 1;
222 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
223 amn->amn_tx_rate1 = rt->info[rix].rateCode;
224 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
225 rt->info[rix].shortPreamble;
227 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
231 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
232 amn->amn_tx_rate2 = rt->info[rix].rateCode;
233 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
234 rt->info[rix].shortPreamble;
236 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
239 /* NB: only do this if we didn't already do it above */
240 amn->amn_tx_rate3 = rt->info[0].rateCode;
241 amn->amn_tx_rate3sp =
242 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
244 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
247 amn->amn_tx_try0 = ATH_TXMAXTRY;
248 /* theorically, these statements are useless because
249 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
251 amn->amn_tx_try1 = 0;
252 amn->amn_tx_try2 = 0;
253 amn->amn_tx_try3 = 0;
254 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
255 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
256 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
263 * Set the starting transmit rate for a node.
266 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
268 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
269 struct ieee80211com *ic = &sc->sc_ic;
272 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
273 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
275 * No fixed rate is requested. For 11b start with
276 * the highest negotiated rate; otherwise, for 11g
277 * and 11a, we start "in the middle" at 24Mb or 36Mb.
279 srate = ni->ni_rates.rs_nrates - 1;
280 if (sc->sc_curmode != IEEE80211_MODE_11B) {
282 * Scan the negotiated rate set to find the
285 /* NB: the rate set is assumed sorted */
286 for (; srate >= 0 && RATE(srate) > 72; srate--)
288 KASSERT(srate >= 0, ("bogus rate set"));
292 * A fixed rate is to be used; ic_fixed_rate is an
293 * index into the supported rate set. Convert this
294 * to the index into the negotiated rate set for
295 * the node. We know the rate is there because the
296 * rate set is checked when the station associates.
298 const struct ieee80211_rateset *rs =
299 &ic->ic_sup_rates[ic->ic_curmode];
300 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
301 /* NB: the rate set is assumed sorted */
302 srate = ni->ni_rates.rs_nrates - 1;
303 for (; srate >= 0 && RATE(srate) != r; srate--)
306 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
308 ath_rate_update(sc, ni, srate);
313 ath_rate_cb(void *arg, struct ieee80211_node *ni)
315 struct ath_softc *sc = arg;
317 ath_rate_update(sc, ni, 0);
321 * Reset the rate control state for each 802.11 state transition.
324 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
326 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
327 struct ieee80211com *ic = &sc->sc_ic;
328 struct ifnet *ifp = &ic->ic_if;
329 struct ieee80211_node *ni;
331 ASSERT_SERIALIZED(ifp->if_serializer);
333 if (state == IEEE80211_S_INIT) {
334 callout_stop(&asc->timer);
337 if (ic->ic_opmode == IEEE80211_M_STA) {
339 * Reset local xmit state; this is really only
340 * meaningful when operating in station mode.
343 if (state == IEEE80211_S_RUN) {
344 ath_rate_ctl_start(sc, ni);
346 ath_rate_update(sc, ni, 0);
350 * When operating as a station the node table holds
351 * the AP's that were discovered during scanning.
352 * For any other operating mode we want to reset the
353 * tx rate state of each node.
355 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
356 ath_rate_update(sc, ic->ic_bss, 0);
358 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
359 state == IEEE80211_S_RUN) {
362 * Start the background rate control thread if we
363 * are not configured to use a fixed xmit rate.
365 interval = ath_rateinterval;
366 if (ic->ic_opmode == IEEE80211_M_STA)
368 callout_reset(&asc->timer, (interval * hz) / 1000,
374 * Examine and potentially adjust the transmit rate.
377 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
379 struct ath_softc *sc = arg;
380 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
383 #define is_success(amn) \
384 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
385 #define is_enough(amn) \
386 (amn->amn_tx_try0_cnt > 10)
387 #define is_failure(amn) \
388 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
389 #define is_max_rate(ni) \
390 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
391 #define is_min_rate(ni) \
394 old_rate = ni->ni_txrate;
396 DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
397 amn->amn_tx_try0_cnt,
398 amn->amn_tx_try1_cnt,
399 amn->amn_tx_try2_cnt,
400 amn->amn_tx_try3_cnt,
401 amn->amn_success_threshold);
402 if (is_success (amn) && is_enough (amn)) {
404 if (amn->amn_success == amn->amn_success_threshold &&
406 amn->amn_recovery = 1;
407 amn->amn_success = 0;
409 DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
411 amn->amn_recovery = 0;
413 } else if (is_failure (amn)) {
414 amn->amn_success = 0;
415 if (!is_min_rate (ni)) {
416 if (amn->amn_recovery) {
417 /* recovery failure. */
418 amn->amn_success_threshold *= 2;
419 amn->amn_success_threshold = min (amn->amn_success_threshold,
420 (u_int)ath_rate_max_success_threshold);
421 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
423 /* simple failure. */
424 amn->amn_success_threshold = ath_rate_min_success_threshold;
425 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
427 amn->amn_recovery = 0;
430 amn->amn_recovery = 0;
434 if (is_enough (amn) || old_rate != ni->ni_txrate) {
435 /* reset counters. */
436 amn->amn_tx_try0_cnt = 0;
437 amn->amn_tx_try1_cnt = 0;
438 amn->amn_tx_try2_cnt = 0;
439 amn->amn_tx_try3_cnt = 0;
440 amn->amn_tx_failure_cnt = 0;
442 if (old_rate != ni->ni_txrate) {
443 ath_rate_update(sc, ni, ni->ni_txrate);
448 ath_ratectl(void *arg)
450 struct ifnet *ifp = arg;
451 struct ath_softc *sc = ifp->if_softc;
452 struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
453 struct ieee80211com *ic = &sc->sc_ic;
456 lwkt_serialize_enter(ifp->if_serializer);
458 if (ifp->if_flags & IFF_RUNNING) {
459 sc->sc_stats.ast_rate_calls++;
461 if (ic->ic_opmode == IEEE80211_M_STA)
462 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
464 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
466 interval = ath_rateinterval;
467 if (ic->ic_opmode == IEEE80211_M_STA)
469 callout_reset(&asc->timer, (interval * hz) / 1000, ath_ratectl, ifp);
471 lwkt_serialize_exit(ifp->if_serializer);
475 ath_rate_sysctlattach(struct ath_softc *sc)
477 struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
478 struct sysctl_oid *tree = sc->sc_sysctl_tree;
480 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
481 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
482 "rate control: operation interval (ms)");
483 /* XXX bounds check values */
484 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
485 "max_sucess_threshold", CTLFLAG_RW,
486 &ath_rate_max_success_threshold, 0, "");
487 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
488 "min_sucess_threshold", CTLFLAG_RW,
489 &ath_rate_min_success_threshold, 0, "");
492 struct ath_ratectrl *
493 ath_rate_attach(struct ath_softc *sc)
495 struct amrr_softc *asc;
497 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
500 asc->arc.arc_space = sizeof(struct amrr_node);
501 callout_init(&asc->timer);
502 ath_rate_sysctlattach(sc);
508 ath_rate_detach(struct ath_ratectrl *arc)
510 struct amrr_softc *asc = (struct amrr_softc *)arc;
512 kfree(asc, M_DEVBUF);
516 ath_rate_stop(struct ath_ratectrl *arc)
518 struct amrr_softc *asc = (struct amrr_softc *)arc;
520 /* ASSERT_SERIALIZED */
521 callout_stop(&asc->timer);
528 amrr_modevent(module_t mod, int type, void *unused)
533 kprintf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
541 static moduledata_t amrr_mod = {
546 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
547 MODULE_VERSION(ath_rate, 1);
548 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);