1 /* $OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $ */
4 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
6 * Damien Bergamini <damien.bergamini@free.fr>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * $FreeBSD: head/sys/net80211/ieee80211_amrr.c 206358 2010-04-07 15:29:13Z rpaulo $
26 * Naive implementation of the Adaptive Multi Rate Retry algorithm:
28 * "IEEE 802.11 Rate Adaptation: A Practical Approach"
29 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
30 * INRIA Sophia - Projet Planete
31 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
42 #include <net/if_media.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_amrr.h>
51 #include <netproto/802_11/ieee80211_ratectl.h>
53 #define is_success(amn) \
54 ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
55 #define is_failure(amn) \
56 ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
57 #define is_enough(amn) \
58 ((amn)->amn_txcnt > 10)
60 static void amrr_setinterval(const struct ieee80211vap *, int);
61 static void amrr_init(struct ieee80211vap *);
62 static void amrr_deinit(struct ieee80211vap *);
63 static void amrr_node_init(struct ieee80211_node *);
64 static void amrr_node_deinit(struct ieee80211_node *);
65 static int amrr_update(struct ieee80211_amrr *,
66 struct ieee80211_amrr_node *, struct ieee80211_node *);
67 static int amrr_rate(struct ieee80211_node *, void *, uint32_t);
68 static void amrr_tx_complete(const struct ieee80211vap *,
69 const struct ieee80211_node *, int,
71 static void amrr_tx_update(const struct ieee80211vap *vap,
72 const struct ieee80211_node *, void *, void *, void *);
73 static void amrr_sysctlattach(struct ieee80211vap *,
74 struct sysctl_ctx_list *, struct sysctl_oid *);
76 /* number of references from net80211 layer */
79 static const struct ieee80211_ratectl amrr = {
84 .ir_deinit = amrr_deinit,
85 .ir_node_init = amrr_node_init,
86 .ir_node_deinit = amrr_node_deinit,
88 .ir_tx_complete = amrr_tx_complete,
89 .ir_tx_update = amrr_tx_update,
90 .ir_setinterval = amrr_setinterval,
92 IEEE80211_RATECTL_MODULE(amrr, 1);
93 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
96 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
98 struct ieee80211_amrr *amrr = vap->iv_rs;
103 t = msecs_to_ticks(msecs);
104 amrr->amrr_interval = (t < 1) ? 1 : t;
108 amrr_init(struct ieee80211vap *vap)
110 struct ieee80211_amrr *amrr;
112 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
114 vap->iv_rs = kmalloc(sizeof(struct ieee80211_amrr), M_80211_RATECTL,
117 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
118 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
119 amrr_setinterval(vap, 500 /* ms */);
120 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
124 amrr_deinit(struct ieee80211vap *vap)
126 kfree(vap->iv_rs, M_80211_RATECTL);
130 amrr_node_init(struct ieee80211_node *ni)
132 const struct ieee80211_rateset *rs = &ni->ni_rates;
133 struct ieee80211vap *vap = ni->ni_vap;
134 struct ieee80211_amrr *amrr = vap->iv_rs;
135 struct ieee80211_amrr_node *amn;
137 KASSERT(ni->ni_rctls == NULL, ("%s: ni_rctls already initialized",
140 ni->ni_rctls = kmalloc(sizeof(struct ieee80211_amrr_node),
141 M_80211_RATECTL, M_WAITOK|M_ZERO);
143 amn->amn_amrr = amrr;
144 amn->amn_success = 0;
145 amn->amn_recovery = 0;
146 amn->amn_txcnt = amn->amn_retrycnt = 0;
147 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
149 /* pick initial rate */
150 for (amn->amn_rix = rs->rs_nrates - 1;
151 amn->amn_rix > 0 && (rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) > 72;
154 ni->ni_txrate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
155 amn->amn_ticks = ticks;
157 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
158 "AMRR initial rate %d", ni->ni_txrate);
162 amrr_node_deinit(struct ieee80211_node *ni)
164 kfree(ni->ni_rctls, M_80211_RATECTL);
168 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
169 struct ieee80211_node *ni)
171 int rix = amn->amn_rix;
173 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
175 if (is_success(amn)) {
177 if (amn->amn_success >= amn->amn_success_threshold &&
178 rix + 1 < ni->ni_rates.rs_nrates) {
179 amn->amn_recovery = 1;
180 amn->amn_success = 0;
182 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
183 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
184 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
185 amn->amn_txcnt, amn->amn_retrycnt);
187 amn->amn_recovery = 0;
189 } else if (is_failure(amn)) {
190 amn->amn_success = 0;
192 if (amn->amn_recovery) {
193 amn->amn_success_threshold *= 2;
194 if (amn->amn_success_threshold >
195 amrr->amrr_max_success_threshold)
196 amn->amn_success_threshold =
197 amrr->amrr_max_success_threshold;
199 amn->amn_success_threshold =
200 amrr->amrr_min_success_threshold;
203 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
204 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
205 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL,
206 amn->amn_txcnt, amn->amn_retrycnt);
208 amn->amn_recovery = 0;
213 amn->amn_retrycnt = 0;
219 * Return the rate index to use in sending a data frame.
220 * Update our internal state if it's been long enough.
221 * If the rate changes we also update ni_txrate to match.
224 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
226 struct ieee80211_amrr_node *amn = ni->ni_rctls;
227 struct ieee80211_amrr *amrr = amn->amn_amrr;
230 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
231 rix = amrr_update(amrr, amn, ni);
232 if (rix != amn->amn_rix) {
233 /* update public rate */
235 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
238 amn->amn_ticks = ticks;
245 * Update statistics with tx complete status. Ok is non-zero
246 * if the packet is known to be ACK'd. Retries has the number
247 * retransmissions (i.e. xmit attempts - 1).
250 amrr_tx_complete(const struct ieee80211vap *vap,
251 const struct ieee80211_node *ni, int ok,
252 void *arg1, void *arg2 __unused)
254 struct ieee80211_amrr_node *amn = ni->ni_rctls;
255 int retries = *(int *)arg1;
260 amn->amn_retrycnt += retries;
264 * Set tx count/retry statistics explicitly. Intended for
265 * drivers that poll the device for statistics maintained
269 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
270 void *arg1, void *arg2, void *arg3)
272 struct ieee80211_amrr_node *amn = ni->ni_rctls;
273 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
275 amn->amn_txcnt = txcnt;
276 amn->amn_success = success;
277 amn->amn_retrycnt = retrycnt;
281 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
283 struct ieee80211vap *vap = arg1;
284 struct ieee80211_amrr *amrr = vap->iv_rs;
285 int msecs = ticks_to_msecs(amrr->amrr_interval);
288 error = sysctl_handle_int(oidp, &msecs, 0, req);
289 if (error || !req->newptr)
291 amrr_setinterval(vap, msecs);
296 amrr_sysctlattach(struct ieee80211vap *vap,
297 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
299 struct ieee80211_amrr *amrr = vap->iv_rs;
301 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
302 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
303 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
304 /* XXX bounds check values */
305 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
306 "amrr_max_sucess_threshold", CTLFLAG_RW,
307 &amrr->amrr_max_success_threshold, 0, "");
308 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
309 "amrr_min_sucess_threshold", CTLFLAG_RW,
310 &amrr->amrr_min_success_threshold, 0, "");