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/netproto/802_11/wlan_ratectl/amrr/ieee80211_ratectl_amrr.c,v 1.11 2008/01/15 09:01:13 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/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/sysctl.h>
53 #include <sys/serialize.h>
56 #include <net/if_media.h>
57 #include <net/if_arp.h>
59 #include <netproto/802_11/ieee80211_var.h>
60 #include <netproto/802_11/wlan_ratectl/amrr/ieee80211_amrr_param.h>
64 #define DPRINTF(asc, lv, fmt, ...) do { \
65 if ((asc)->param->amrr_debug >= lv) \
66 kprintf(fmt, __VA_ARGS__); \
69 #define DPRINTF(asc, lv, fmt, ...)
73 struct ieee80211com *ic;
74 struct callout timer; /* periodic timer */
75 struct sysctl_ctx_list sysctl_ctx;
76 struct sysctl_oid *sysctl_oid;
78 struct ieee80211_amrr_param *param;
79 #define max_success_threshold param->amrr_max_success_threshold
80 #define min_success_threshold param->amrr_min_success_threshold
84 /* AMRR statistics for this node */
86 u_int ad_tx_failure_cnt;
88 /* AMRR algorithm state for this node */
89 u_int ad_success_threshold;
94 static void *amrr_attach(struct ieee80211com *);
95 static void amrr_detach(void *);
96 static void amrr_data_alloc(struct ieee80211_node *);
97 static void amrr_data_free(struct ieee80211_node *);
98 static void amrr_data_dup(const struct ieee80211_node *,
99 struct ieee80211_node *);
100 static void amrr_newstate(void *, enum ieee80211_state);
101 static void amrr_tx_complete(void *, struct ieee80211_node *, int,
102 const struct ieee80211_ratectl_res[],
104 static void amrr_newassoc(void *, struct ieee80211_node *, int);
105 static int amrr_findrate(void *, struct ieee80211_node *, int,
108 static void amrr_sysctl_attach(struct amrr_softc *);
109 static void amrr_update(struct amrr_softc *, struct ieee80211_node *, int);
110 static void amrr_start(struct amrr_softc *, struct ieee80211_node *);
111 static void amrr_tick(void *);
112 static void amrr_ratectl(void *, struct ieee80211_node *);
113 static void amrr_gather_stats(struct amrr_softc *, struct ieee80211_node *);
115 static const struct ieee80211_ratectl amrr = {
117 .rc_ratectl = IEEE80211_RATECTL_AMRR,
118 .rc_attach = amrr_attach,
119 .rc_detach = amrr_detach,
120 .rc_data_alloc = amrr_data_alloc,
121 .rc_data_free = amrr_data_free,
122 .rc_data_dup = amrr_data_dup,
123 .rc_newstate = amrr_newstate,
124 .rc_tx_complete = amrr_tx_complete,
125 .rc_newassoc = amrr_newassoc,
126 .rc_findrate = amrr_findrate
129 static u_int amrr_nrefs;
131 MALLOC_DEFINE(M_AMRR_RATECTL_DATA, "amrr_ratectl_data",
132 "amrr rate control data");
135 amrr_findrate(void *arg, struct ieee80211_node *ni,
136 int frame_len __unused, int rateidx[], int rateidx_len)
138 struct amrr_softc *asc = arg;
141 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) {
142 DPRINTF(asc, 5, "%s: number of rates changed, restart\n",
146 rate_idx = ni->ni_txrate;
148 for (i = 0; i < rateidx_len; ++i) {
151 rateidx[i] = rate_idx--;
154 rateidx[rateidx_len - 1] = 0;
159 amrr_tx_complete(void *arg __unused, struct ieee80211_node *ni,
160 int frame_len __unused,
161 const struct ieee80211_ratectl_res res[],
162 int res_len, int data_retries __unused,
163 int rts_retries __unused, int is_fail)
165 struct amrr_data *ad = ni->ni_rate_data;
173 for (i = 0; i < res_len; ++i)
174 total_tries += res[i].rc_res_tries;
175 ad->ad_tx_cnt += total_tries;
177 ad->ad_tx_failure_cnt += total_tries;
178 if (res_len == 1 && !is_fail) {
179 KKASSERT(ad->ad_tx_failure_cnt != 0);
180 /* One packet is successfully transmitted at desired rate */
181 ad->ad_tx_failure_cnt--;
186 amrr_newassoc(void *arg, struct ieee80211_node *ni, int isnew)
193 * The code below assumes that we are dealing with hardware multi rate retry
194 * I have no idea what will happen if you try to use this module with another
195 * type of hardware. Your machine might catch fire or it might work with
196 * horrible performance...
199 amrr_update(struct amrr_softc *asc, struct ieee80211_node *ni, int rate)
201 struct amrr_data *ad = ni->ni_rate_data;
203 DPRINTF(asc, 5, "%s: set xmit rate for %6D to %dM\n",
204 __func__, ni->ni_macaddr, ":",
205 ni->ni_rates.rs_nrates > 0 ?
206 IEEE80211_RS_RATE(&ni->ni_rates, rate) / 2 : 0);
208 ni->ni_txrate = rate;
212 ad = ni->ni_rate_data;
218 ad->ad_tx_failure_cnt = 0;
221 ad->ad_success_threshold = asc->min_success_threshold;
225 * Set the starting transmit rate for a node.
228 amrr_start(struct amrr_softc *asc, struct ieee80211_node *ni)
230 #define RATE(_ix) IEEE80211_RS_RATE(&ni->ni_rates, (_ix))
231 struct ieee80211com *ic = asc->ic;
234 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
236 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
238 * For adhoc or ibss mode, start from the lowest rate.
240 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
241 ic->ic_opmode == IEEE80211_M_IBSS) {
242 amrr_update(asc, ni, 0);
247 * No fixed rate is requested. For 11b start with
248 * the highest negotiated rate; otherwise, for 11g
249 * and 11a, we start "in the middle" at 24Mb or 36Mb.
251 srate = ni->ni_rates.rs_nrates - 1;
252 if (ic->ic_curmode != IEEE80211_MODE_11B) {
254 * Scan the negotiated rate set to find the
257 /* NB: the rate set is assumed sorted */
258 for (; srate >= 0 && RATE(srate) > 72; srate--)
260 KASSERT(srate >= 0, ("bogus rate set"));
264 * A fixed rate is to be used; ic_fixed_rate is an
265 * index into the supported rate set. Convert this
266 * to the index into the negotiated rate set for
267 * the node. We know the rate is there because the
268 * rate set is checked when the station associates.
270 const struct ieee80211_rateset *rs =
271 &ic->ic_sup_rates[ic->ic_curmode];
272 int r = IEEE80211_RS_RATE(rs, ic->ic_fixed_rate);
274 /* NB: the rate set is assumed sorted */
275 srate = ni->ni_rates.rs_nrates - 1;
276 for (; srate >= 0 && RATE(srate) != r; srate--)
279 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
281 amrr_update(asc, ni, srate);
286 amrr_rate_cb(void *arg, struct ieee80211_node *ni)
288 amrr_update(arg, ni, 0);
292 * Reset the rate control state for each 802.11 state transition.
295 amrr_newstate(void *arg, enum ieee80211_state state)
297 struct amrr_softc *asc = arg;
298 struct ieee80211com *ic = asc->ic;
299 struct ieee80211_node *ni;
301 if (state == IEEE80211_S_INIT) {
302 callout_stop(&asc->timer);
306 if (ic->ic_opmode == IEEE80211_M_STA) {
308 * Reset local xmit state; this is really only
309 * meaningful when operating in station mode.
312 if (state == IEEE80211_S_RUN)
315 amrr_update(asc, ni, 0);
318 * When operating as a station the node table holds
319 * the AP's that were discovered during scanning.
320 * For any other operating mode we want to reset the
321 * tx rate state of each node.
323 ieee80211_iterate_nodes(&ic->ic_sta, amrr_rate_cb, asc);
324 amrr_update(asc, ic->ic_bss, 0);
326 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
327 state == IEEE80211_S_RUN) {
331 * Start the background rate control thread if we
332 * are not configured to use a fixed xmit rate.
334 interval = asc->param->amrr_interval;
335 if (ic->ic_opmode == IEEE80211_M_STA)
337 callout_reset(&asc->timer, (interval * hz) / 1000,
343 amrr_gather_stats(struct amrr_softc *asc, struct ieee80211_node *ni)
345 struct ieee80211com *ic = asc->ic;
346 const struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
347 struct amrr_data *ad = ni->ni_rate_data;
348 struct ieee80211_ratectl_stats stats;
349 u_int total_tries = 0;
351 st->rc_st_stats(ic, ni, &stats);
353 total_tries = stats.stats_pkt_ok +
354 stats.stats_pkt_err +
357 ad->ad_tx_cnt += total_tries;
358 ad->ad_tx_failure_cnt += (total_tries - stats.stats_pkt_noretry);
362 * Examine and potentially adjust the transmit rate.
365 amrr_ratectl(void *arg, struct ieee80211_node *ni)
367 struct amrr_softc *asc = arg;
368 const struct ieee80211_ratectl_state *st = &asc->ic->ic_ratectl;
369 struct amrr_data *ad = ni->ni_rate_data;
373 /* We are not ready to go, set TX rate to lowest one */
378 #define is_success(ad) (ad->ad_tx_failure_cnt < (ad->ad_tx_cnt / 10))
379 #define is_enough(ad) (ad->ad_tx_cnt > 10)
380 #define is_failure(ad) (ad->ad_tx_failure_cnt > (ad->ad_tx_cnt / 3))
381 #define is_max_rate(ni) ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
382 #define is_min_rate(ni) (ni->ni_txrate == 0)
384 old_rate = ni->ni_txrate;
386 if (st->rc_st_stats != NULL)
387 amrr_gather_stats(asc, ni);
389 DPRINTF(asc, 10, "tx_cnt: %u tx_failure_cnt: %u -- "
391 ad->ad_tx_cnt, ad->ad_tx_failure_cnt,
392 ad->ad_success_threshold);
394 if (is_success(ad) && is_enough(ad)) {
396 if (ad->ad_success == ad->ad_success_threshold &&
401 DPRINTF(asc, 5, "increase rate to %d\n", ni->ni_txrate);
405 } else if (is_failure(ad)) {
407 if (!is_min_rate(ni)) {
408 if (ad->ad_recovery) {
409 /* recovery failure. */
410 ad->ad_success_threshold *= 2;
411 ad->ad_success_threshold =
412 min(ad->ad_success_threshold,
413 (u_int)asc->max_success_threshold);
414 DPRINTF(asc, 5, "decrease rate recovery thr: "
415 "%d\n", ad->ad_success_threshold);
417 /* simple failure. */
418 ad->ad_success_threshold =
419 asc->min_success_threshold;
420 DPRINTF(asc, 5, "decrease rate normal thr: "
421 "%d\n", ad->ad_success_threshold);
429 if (is_enough(ad) || old_rate != ni->ni_txrate) {
430 /* reset counters. */
432 ad->ad_tx_failure_cnt = 0;
434 if (old_rate != ni->ni_txrate)
435 amrr_update(asc, ni, ni->ni_txrate);
441 struct amrr_softc *asc = arg;
442 struct ieee80211com *ic = asc->ic;
443 struct ifnet *ifp = &ic->ic_if;
446 ifnet_serialize_all(ifp);
448 if (ifp->if_flags & IFF_RUNNING) {
449 if (ic->ic_opmode == IEEE80211_M_STA)
450 amrr_ratectl(asc, ic->ic_bss); /* NB: no reference */
452 ieee80211_iterate_nodes(&ic->ic_sta, amrr_ratectl, asc);
454 interval = asc->param->amrr_interval;
455 if (ic->ic_opmode == IEEE80211_M_STA)
457 callout_reset(&asc->timer, (interval * hz) / 1000, amrr_tick, asc);
459 ifnet_deserialize_all(ifp);
463 amrr_sysctl_attach(struct amrr_softc *asc)
465 sysctl_ctx_init(&asc->sysctl_ctx);
466 asc->sysctl_oid = SYSCTL_ADD_NODE(&asc->sysctl_ctx,
467 SYSCTL_CHILDREN(asc->ic->ic_sysctl_oid),
468 OID_AUTO, "amrr_ratectl", CTLFLAG_RD, 0, "");
469 if (asc->sysctl_oid == NULL) {
470 kprintf("wlan_ratectl_amrr: create sysctl tree failed\n");
474 SYSCTL_ADD_INT(&asc->sysctl_ctx, SYSCTL_CHILDREN(asc->sysctl_oid),
475 OID_AUTO, "interval", CTLFLAG_RW,
476 &asc->param->amrr_interval, 0,
477 "rate control: operation interval (ms)");
479 /* XXX bounds check values */
480 SYSCTL_ADD_INT(&asc->sysctl_ctx, SYSCTL_CHILDREN(asc->sysctl_oid),
481 OID_AUTO, "max_sucess_threshold", CTLFLAG_RW,
482 &asc->param->amrr_max_success_threshold, 0, "");
484 SYSCTL_ADD_INT(&asc->sysctl_ctx, SYSCTL_CHILDREN(asc->sysctl_oid),
485 OID_AUTO, "min_sucess_threshold", CTLFLAG_RW,
486 &asc->param->amrr_min_success_threshold, 0, "");
488 SYSCTL_ADD_INT(&asc->sysctl_ctx, SYSCTL_CHILDREN(asc->sysctl_oid),
489 OID_AUTO, "debug", CTLFLAG_RW,
490 &asc->param->amrr_debug, 0, "debug level");
494 amrr_attach(struct ieee80211com *ic)
496 struct amrr_softc *asc;
500 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_WAITOK | M_ZERO);
503 callout_init(&asc->timer);
504 asc->param = ic->ic_ratectl.rc_st_attach(ic, IEEE80211_RATECTL_AMRR);
506 amrr_sysctl_attach(asc);
508 amrr_newstate(asc, ic->ic_state);
514 _amrr_data_free(void *arg __unused, struct ieee80211_node *ni)
520 amrr_detach(void *arg)
522 struct amrr_softc *asc = arg;
523 struct ieee80211com *ic = asc->ic;
525 amrr_newstate(asc, IEEE80211_S_INIT);
527 ieee80211_iterate_nodes(&ic->ic_sta, _amrr_data_free, NULL);
528 ieee80211_iterate_nodes(&ic->ic_scan, _amrr_data_free, NULL);
530 if (asc->sysctl_oid != NULL)
531 sysctl_ctx_free(&asc->sysctl_ctx);
532 kfree(asc, M_DEVBUF);
538 amrr_data_free(struct ieee80211_node *ni)
540 if (ni->ni_rate_data != NULL) {
541 kfree(ni->ni_rate_data, M_AMRR_RATECTL_DATA);
542 ni->ni_rate_data = NULL;
547 amrr_data_alloc(struct ieee80211_node *ni)
549 KKASSERT(ni->ni_rate_data == NULL);
550 ni->ni_rate_data = kmalloc(sizeof(struct amrr_data),
551 M_AMRR_RATECTL_DATA, M_NOWAIT | M_ZERO);
555 amrr_data_dup(const struct ieee80211_node *oni, struct ieee80211_node *nni)
557 if (oni->ni_rate_data == NULL || nni->ni_rate_data == NULL)
560 bcopy(oni->ni_rate_data, nni->ni_rate_data, sizeof(struct amrr_data));
567 amrr_modevent(module_t mod, int type, void *unused)
571 ieee80211_ratectl_register(&amrr);
575 kprintf("wlan_ratectl_amrr: still in use "
576 "(%u dynamic refs)\n", amrr_nrefs);
579 ieee80211_ratectl_unregister(&amrr);
585 static moduledata_t amrr_mod = {
590 DECLARE_MODULE(wlan_ratectl_amrr, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
591 MODULE_VERSION(wlan_ratectl_amrr, 1);
592 MODULE_DEPEND(wlan_ratectl_amrr, wlan, 1, 1, 1);