1 /* $OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $ */
5 * Damien Bergamini <damien.bergamini@free.fr>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Naive implementation of the Adaptive Multi Rate Retry algorithm:
26 * "IEEE 802.11 Rate Adaptation: A Practical Approach"
27 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
28 * INRIA Sophia - Projet Planete
29 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <net/if_media.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_amrr.h>
50 #define is_success(amn) \
51 ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
52 #define is_failure(amn) \
53 ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
54 #define is_enough(amn) \
55 ((amn)->amn_txcnt > 10)
57 static void amrr_sysctlattach(struct ieee80211_amrr *amrr,
58 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree);
60 /* number of references from net80211 layer */
64 ieee80211_amrr_setinterval(struct ieee80211_amrr *amrr, int msecs)
70 t = msecs_to_ticks(msecs);
71 amrr->amrr_interval = (t < 1) ? 1 : t;
75 ieee80211_amrr_init(struct ieee80211_amrr *amrr,
76 struct ieee80211vap *vap, int amin, int amax, int interval)
78 /* XXX bounds check? */
79 amrr->amrr_min_success_threshold = amin;
80 amrr->amrr_max_success_threshold = amax;
81 ieee80211_amrr_setinterval(amrr, interval);
83 amrr_sysctlattach(amrr, vap->iv_sysctl, vap->iv_oid);
87 ieee80211_amrr_cleanup(struct ieee80211_amrr *amrr)
92 ieee80211_amrr_node_init(struct ieee80211_amrr *amrr,
93 struct ieee80211_amrr_node *amn, struct ieee80211_node *ni)
95 const struct ieee80211_rateset *rs = &ni->ni_rates;
99 amn->amn_recovery = 0;
100 amn->amn_txcnt = amn->amn_retrycnt = 0;
101 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
103 /* pick initial rate */
104 for (amn->amn_rix = rs->rs_nrates - 1;
105 amn->amn_rix > 0 && (rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) > 72;
108 ni->ni_txrate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
109 amn->amn_ticks = ticks;
111 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
112 "AMRR initial rate %d", ni->ni_txrate);
116 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
117 struct ieee80211_node *ni)
119 int rix = amn->amn_rix;
121 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
123 if (is_success(amn)) {
125 if (amn->amn_success >= amn->amn_success_threshold &&
126 rix + 1 < ni->ni_rates.rs_nrates) {
127 amn->amn_recovery = 1;
128 amn->amn_success = 0;
130 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
131 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
132 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
133 amn->amn_txcnt, amn->amn_retrycnt);
135 amn->amn_recovery = 0;
137 } else if (is_failure(amn)) {
138 amn->amn_success = 0;
140 if (amn->amn_recovery) {
141 amn->amn_success_threshold *= 2;
142 if (amn->amn_success_threshold >
143 amrr->amrr_max_success_threshold)
144 amn->amn_success_threshold =
145 amrr->amrr_max_success_threshold;
147 amn->amn_success_threshold =
148 amrr->amrr_min_success_threshold;
151 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
152 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
153 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
154 amn->amn_txcnt, amn->amn_retrycnt);
156 amn->amn_recovery = 0;
161 amn->amn_retrycnt = 0;
167 * Return the rate index to use in sending a data frame.
168 * Update our internal state if it's been long enough.
169 * If the rate changes we also update ni_txrate to match.
172 ieee80211_amrr_choose(struct ieee80211_node *ni,
173 struct ieee80211_amrr_node *amn)
175 struct ieee80211_amrr *amrr = amn->amn_amrr;
178 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
179 rix = amrr_update(amrr, amn, ni);
180 if (rix != amn->amn_rix) {
181 /* update public rate */
183 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
186 amn->amn_ticks = ticks;
193 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
195 struct ieee80211_amrr *amrr = arg1;
196 int msecs = ticks_to_msecs(amrr->amrr_interval);
199 error = sysctl_handle_int(oidp, &msecs, 0, req);
200 if (error || !req->newptr)
202 ieee80211_amrr_setinterval(amrr, msecs);
207 amrr_sysctlattach(struct ieee80211_amrr *amrr,
208 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
211 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
212 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, amrr,
213 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
214 /* XXX bounds check values */
215 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
216 "amrr_max_sucess_threshold", CTLFLAG_RW,
217 &amrr->amrr_max_success_threshold, 0, "");
218 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
219 "amrr_min_sucess_threshold", CTLFLAG_RW,
220 &amrr->amrr_min_success_threshold, 0, "");
226 IEEE80211_RATE_MODULE(amrr, 1);