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 * $Id: amrr.c 3742 2008-06-21 14:40:43Z mentor $
41 * AMRR rate control. See:
42 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
43 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
44 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
46 #ifndef AUTOCONF_INCLUDED
47 #include <linux/config.h>
49 #include <linux/version.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/skbuff.h>
53 #include <linux/netdevice.h>
54 #include <linux/random.h>
55 #include <linux/delay.h>
56 #include <linux/cache.h>
57 #include <linux/sysctl.h>
58 #include <linux/proc_fs.h>
59 #include <linux/if_arp.h>
61 #include <asm/uaccess.h>
63 #include <net80211/if_media.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_rate.h>
67 #include "if_athvar.h"
68 #include "if_ath_hal.h"
75 #define DPRINTF(sc, _fmt, ...) do { \
76 if (sc->sc_debug & 0x10) \
77 printk(_fmt, __VA_ARGS__); \
80 #define DPRINTF(sc, _fmt, ...)
83 static int ath_rateinterval
= 1000; /* rate ctl interval (ms) */
84 static int ath_rate_max_success_threshold
= 10;
85 static int ath_rate_min_success_threshold
= 1;
87 static void ath_ratectl(unsigned long);
88 static void ath_rate_update(struct ath_softc
*, struct ieee80211_node
*, int);
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 u_int8_t
*rix
, unsigned int *try0
, u_int8_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_get_mrr(struct ath_softc
*sc
, struct ath_node
*an
, int shortPreamble
,
121 size_t frame_size
, u_int8_t rix
, struct ieee80211_mrr
*mrr
)
123 struct amrr_node
*amn
= ATH_NODE_AMRR(an
);
125 mrr
->rate1
= amn
->amn_tx_rate1sp
;
126 mrr
->retries1
= amn
->amn_tx_try1
;
127 mrr
->rate2
= amn
->amn_tx_rate2sp
;
128 mrr
->retries2
= amn
->amn_tx_try2
;
129 mrr
->rate3
= amn
->amn_tx_rate3sp
;
130 mrr
->retries3
= amn
->amn_tx_try3
;
134 ath_rate_tx_complete(struct ath_softc
*sc
,
135 struct ath_node
*an
, const struct ath_buf
*bf
)
137 struct amrr_node
*amn
= ATH_NODE_AMRR(an
);
138 const struct ath_tx_status
*ts
= &bf
->bf_dsstatus
.ds_txstat
;
139 int sr
= ts
->ts_shortretry
;
140 int lr
= ts
->ts_longretry
;
141 int retry_count
= sr
+ lr
;
143 amn
->amn_tx_try0_cnt
++;
144 if (retry_count
== 1) {
145 amn
->amn_tx_try1_cnt
++;
146 } else if (retry_count
== 2) {
147 amn
->amn_tx_try1_cnt
++;
148 amn
->amn_tx_try2_cnt
++;
149 } else if (retry_count
== 3) {
150 amn
->amn_tx_try1_cnt
++;
151 amn
->amn_tx_try2_cnt
++;
152 amn
->amn_tx_try3_cnt
++;
153 } else if (retry_count
> 3) {
154 amn
->amn_tx_try1_cnt
++;
155 amn
->amn_tx_try2_cnt
++;
156 amn
->amn_tx_try3_cnt
++;
157 amn
->amn_tx_failure_cnt
++;
162 ath_rate_newassoc(struct ath_softc
*sc
, struct ath_node
*an
, int isnew
)
165 ath_rate_ctl_start(sc
, &an
->an_node
);
169 node_reset (struct amrr_node
*amn
)
171 amn
->amn_tx_try0_cnt
= 0;
172 amn
->amn_tx_try1_cnt
= 0;
173 amn
->amn_tx_try2_cnt
= 0;
174 amn
->amn_tx_try3_cnt
= 0;
175 amn
->amn_tx_failure_cnt
= 0;
176 amn
->amn_success
= 0;
177 amn
->amn_recovery
= 0;
178 amn
->amn_success_threshold
= ath_rate_min_success_threshold
;
183 * The code below assumes that we are dealing with hardware multi rate retry
184 * I have no idea what will happen if you try to use this module with another
185 * type of hardware. Your machine might catch fire or it might work with
186 * horrible performance...
189 ath_rate_update(struct ath_softc
*sc
, struct ieee80211_node
*ni
, int rate
)
191 struct ath_node
*an
= ATH_NODE(ni
);
192 struct amrr_node
*amn
= ATH_NODE_AMRR(an
);
193 const HAL_RATE_TABLE
*rt
= sc
->sc_currates
;
196 KASSERT(rt
!= NULL
, ("no rate table, mode %u", sc
->sc_curmode
));
198 DPRINTF(sc
, "%s: set xmit rate for " MAC_FMT
" to %dM\n",
199 __func__
, MAC_ADDR(ni
->ni_macaddr
),
200 ni
->ni_rates
.rs_nrates
> 0 ?
201 (ni
->ni_rates
.rs_rates
[rate
] & IEEE80211_RATE_VAL
) / 2 : 0);
203 ni
->ni_txrate
= rate
;
205 * Before associating a node has no rate set setup
206 * so we can't calculate any transmit codes to use.
207 * This is ok since we should never be sending anything
208 * but management frames and those always go at the
209 * lowest hardware rate.
211 if (ni
->ni_rates
.rs_nrates
> 0) {
213 sc
->sc_rixmap
[ni
->ni_rates
.rs_rates
[rate
] & IEEE80211_RATE_VAL
];
214 amn
->amn_tx_rate0
= rt
->info
[amn
->amn_tx_rix0
].rateCode
;
215 amn
->amn_tx_rate0sp
= amn
->amn_tx_rate0
|
216 rt
->info
[amn
->amn_tx_rix0
].shortPreamble
;
217 if (sc
->sc_mrretry
) {
218 amn
->amn_tx_try0
= 1;
219 amn
->amn_tx_try1
= 1;
220 amn
->amn_tx_try2
= 1;
221 amn
->amn_tx_try3
= 1;
223 rix
= sc
->sc_rixmap
[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;
231 rix
= sc
->sc_rixmap
[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
= amn
->amn_tx_rate3
|
242 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 ieee80211vap
*vap
= ni
->ni_vap
;
272 KASSERT(ni
->ni_rates
.rs_nrates
> 0, ("no rates"));
273 if (vap
->iv_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
--);
287 KASSERT(srate
>= 0, ("bogus rate set"));
291 * A fixed rate is to be used; ic_fixed_rate is an
292 * index into the supported rate set. Convert this
293 * to the index into the negotiated rate set for
294 * the node. We know the rate is there because the
295 * rate set is checked when the station associates.
297 srate
= ni
->ni_rates
.rs_nrates
- 1;
298 for (; srate
>= 0 && RATE(srate
) != vap
->iv_fixed_rate
; srate
--);
300 ("fixed rate %d not in rate set", vap
->iv_fixed_rate
));
302 ath_rate_update(sc
, ni
, srate
);
307 ath_rate_cb(void *arg
, struct ieee80211_node
*ni
)
309 ath_rate_update(ni
->ni_ic
->ic_dev
->priv
, ni
, (long) arg
);
313 * Reset the rate control state for each 802.11 state transition.
316 ath_rate_newstate(struct ieee80211vap
*vap
, enum ieee80211_state state
)
318 struct ieee80211com
*ic
= vap
->iv_ic
;
319 struct ath_softc
*sc
= ic
->ic_dev
->priv
;
320 struct amrr_softc
*asc
= (struct amrr_softc
*)sc
->sc_rc
;
321 struct ieee80211_node
*ni
;
323 if (state
== IEEE80211_S_INIT
) {
324 del_timer(&asc
->timer
);
327 if (ic
->ic_opmode
== IEEE80211_M_STA
) {
329 * Reset local xmit state; this is really only
330 * meaningful when operating in station mode.
333 if (state
== IEEE80211_S_RUN
)
334 ath_rate_ctl_start(sc
, ni
);
336 ath_rate_update(sc
, ni
, 0);
339 * When operating as a station the node table holds
340 * the APs that were discovered during scanning.
341 * For any other operating mode we want to reset the
342 * tx rate state of each node.
344 ieee80211_iterate_nodes(&ic
->ic_sta
, ath_rate_cb
, NULL
);
345 ath_rate_update(sc
, vap
->iv_bss
, 0);
347 if ((vap
->iv_fixed_rate
== IEEE80211_FIXED_RATE_NONE
) && (state
== IEEE80211_S_RUN
)) {
350 * Start the background rate control thread if we
351 * are not configured to use a fixed xmit rate.
353 interval
= ath_rateinterval
;
354 if (ic
->ic_opmode
== IEEE80211_M_STA
)
356 mod_timer(&asc
->timer
, jiffies
+ ((HZ
* interval
) / 1000));
361 * Examine and potentially adjust the transmit rate.
364 ath_rate_ctl(void *arg
, struct ieee80211_node
*ni
)
366 struct ath_softc
*sc
= arg
;
367 struct amrr_node
*amn
= ATH_NODE_AMRR(ATH_NODE (ni
));
370 #define is_success(amn) (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt / 10))
371 #define is_enough(amn) (amn->amn_tx_try0_cnt > 10)
372 #define is_failure(amn) (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt / 3))
373 #define is_max_rate(ni) ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
374 #define is_min_rate(ni) (ni->ni_txrate == 0)
376 old_rate
= ni
->ni_txrate
;
378 DPRINTF (sc
, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
379 amn
->amn_tx_try0_cnt
,
380 amn
->amn_tx_try1_cnt
,
381 amn
->amn_tx_try2_cnt
,
382 amn
->amn_tx_try3_cnt
,
383 amn
->amn_success_threshold
);
384 if (is_success(amn
) && is_enough(amn
)) {
386 if (amn
->amn_success
== amn
->amn_success_threshold
&&
388 amn
->amn_recovery
= 1;
389 amn
->amn_success
= 0;
391 DPRINTF(sc
, "increase rate to %d\n", ni
->ni_txrate
);
393 amn
->amn_recovery
= 0;
394 } else if (is_failure(amn
)) {
395 amn
->amn_success
= 0;
396 if (!is_min_rate(ni
)) {
397 if (amn
->amn_recovery
) {
398 /* recovery failure. */
399 amn
->amn_success_threshold
*= 2;
400 amn
->amn_success_threshold
= min(amn
->amn_success_threshold
,
401 (u_int
)ath_rate_max_success_threshold
);
402 DPRINTF(sc
, "decrease rate recovery thr: %d\n",
403 amn
->amn_success_threshold
);
405 /* simple failure. */
406 amn
->amn_success_threshold
= ath_rate_min_success_threshold
;
407 DPRINTF(sc
, "decrease rate normal thr: %d\n",
408 amn
->amn_success_threshold
);
410 amn
->amn_recovery
= 0;
413 amn
->amn_recovery
= 0;
415 if (is_enough(amn
) || old_rate
!= ni
->ni_txrate
) {
416 /* reset counters. */
417 amn
->amn_tx_try0_cnt
= 0;
418 amn
->amn_tx_try1_cnt
= 0;
419 amn
->amn_tx_try2_cnt
= 0;
420 amn
->amn_tx_try3_cnt
= 0;
421 amn
->amn_tx_failure_cnt
= 0;
423 if (old_rate
!= ni
->ni_txrate
)
424 ath_rate_update(sc
, ni
, ni
->ni_txrate
);
428 ath_ratectl(unsigned long data
)
430 struct net_device
*dev
= (struct net_device
*)data
;
431 struct ath_softc
*sc
= dev
->priv
;
432 struct amrr_softc
*asc
= (struct amrr_softc
*)sc
->sc_rc
;
433 struct ieee80211com
*ic
= &sc
->sc_ic
;
436 if (dev
->flags
& IFF_RUNNING
) {
437 sc
->sc_stats
.ast_rate_calls
++;
439 if (ic
->ic_opmode
== IEEE80211_M_STA
) {
440 struct ieee80211vap
*tmpvap
;
441 TAILQ_FOREACH(tmpvap
, &ic
->ic_vaps
, iv_next
) {
442 ath_rate_ctl(sc
, tmpvap
->iv_bss
); /* NB: no reference */
445 ieee80211_iterate_nodes(&ic
->ic_sta
, ath_rate_ctl
, sc
);
447 interval
= ath_rateinterval
;
448 if (ic
->ic_opmode
== IEEE80211_M_STA
)
450 mod_timer(&asc
->timer
, jiffies
+ ((HZ
* interval
) / 1000));
453 static struct ath_ratectrl
*
454 ath_rate_attach(struct ath_softc
*sc
)
456 struct amrr_softc
*asc
;
458 _MOD_INC_USE(THIS_MODULE
, return NULL
);
459 asc
= kmalloc(sizeof(struct amrr_softc
), GFP_ATOMIC
);
461 _MOD_DEC_USE(THIS_MODULE
);
464 asc
->arc
.arc_space
= sizeof(struct amrr_node
);
465 asc
->arc
.arc_vap_space
= 0;
466 init_timer(&asc
->timer
);
467 asc
->timer
.data
= (unsigned long) sc
->sc_dev
;
468 asc
->timer
.function
= ath_ratectl
;
474 ath_rate_detach(struct ath_ratectrl
*arc
)
476 struct amrr_softc
*asc
= (struct amrr_softc
*)arc
;
478 del_timer(&asc
->timer
);
480 _MOD_DEC_USE(THIS_MODULE
);
483 static int minrateinterval
= 500; /* 500ms */
484 static int maxint
= 0x7fffffff; /* 32-bit big */
485 static int min_threshold
= 1;
488 * Static (i.e. global) sysctls.
491 static ctl_table ath_rate_static_sysctls
[] = {
492 { .ctl_name
= CTL_AUTO
,
493 .procname
= "interval",
495 .data
= &ath_rateinterval
,
496 .maxlen
= sizeof(ath_rateinterval
),
497 .extra1
= &minrateinterval
,
499 .proc_handler
= proc_dointvec_minmax
501 { .ctl_name
= CTL_AUTO
,
502 .procname
= "max_success_threshold",
504 .data
= &ath_rate_max_success_threshold
,
505 .maxlen
= sizeof(ath_rate_max_success_threshold
),
506 .extra1
= &min_threshold
,
508 .proc_handler
= proc_dointvec_minmax
510 { .ctl_name
= CTL_AUTO
,
511 .procname
= "min_success_threshold",
513 .data
= &ath_rate_min_success_threshold
,
514 .maxlen
= sizeof(ath_rate_min_success_threshold
),
515 .extra1
= &min_threshold
,
517 .proc_handler
= proc_dointvec_minmax
521 static ctl_table ath_rate_table
[] = {
522 { .ctl_name
= CTL_AUTO
,
523 .procname
= "rate_amrr",
525 .child
= ath_rate_static_sysctls
528 static ctl_table ath_ath_table
[] = {
529 { .ctl_name
= DEV_ATH
,
532 .child
= ath_rate_table
535 static ctl_table ath_root_table
[] = {
536 { .ctl_name
= CTL_DEV
,
539 .child
= ath_ath_table
542 static struct ctl_table_header
*ath_sysctl_header
;
544 static struct ieee80211_rate_ops ath_rate_ops
= {
545 .ratectl_id
= IEEE80211_RATE_AMRR
,
546 .node_init
= ath_rate_node_init
,
547 .node_cleanup
= ath_rate_node_cleanup
,
548 .findrate
= ath_rate_findrate
,
549 .get_mrr
= ath_rate_get_mrr
,
550 .tx_complete
= ath_rate_tx_complete
,
551 .newassoc
= ath_rate_newassoc
,
552 .newstate
= ath_rate_newstate
,
553 .attach
= ath_rate_attach
,
554 .detach
= ath_rate_detach
,
559 static char *version
= "0.1 (" RELEASE_VERSION
")";
560 static char *dev_info
= "ath_rate_amrr";
563 MODULE_AUTHOR("INRIA, Mathieu Lacage");
564 MODULE_DESCRIPTION("AMRR Rate control algorithm");
565 #ifdef MODULE_VERSION
566 MODULE_VERSION(RELEASE_VERSION
);
568 #ifdef MODULE_LICENSE
569 MODULE_LICENSE("Dual BSD/GPL");
573 init_ath_rate_amrr(void)
575 int ret
= ieee80211_rate_register(&ath_rate_ops
);
579 ath_sysctl_header
= ATH_REGISTER_SYSCTL_TABLE(ath_root_table
);
582 module_init(init_ath_rate_amrr
);
585 exit_ath_rate_amrr(void)
587 if (ath_sysctl_header
!= NULL
)
588 unregister_sysctl_table(ath_sysctl_header
);
589 ieee80211_rate_unregister(&ath_rate_ops
);
591 module_exit(exit_ath_rate_amrr
);