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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 * Naive implementation of the Adaptive Multi Rate Retry algorithm:
27 * "IEEE 802.11 Rate Adaptation: A Practical Approach"
28 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
29 * INRIA Sophia - Projet Planete
30 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
41 #include <net/if_media.h>
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_ht.h>
50 #include <net80211/ieee80211_amrr.h>
51 #include <net80211/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 amrr = vap->iv_rs = malloc(sizeof(struct ieee80211_amrr),
115 M_80211_RATECTL, M_NOWAIT|M_ZERO);
117 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
120 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
121 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
122 amrr_setinterval(vap, 500 /* ms */);
123 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
127 amrr_deinit(struct ieee80211vap *vap)
129 free(vap->iv_rs, M_80211_RATECTL);
133 amrr_node_is_11n(struct ieee80211_node *ni)
136 if (ni->ni_chan == NULL)
138 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
140 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
144 amrr_node_init(struct ieee80211_node *ni)
146 const struct ieee80211_rateset *rs = NULL;
147 struct ieee80211vap *vap = ni->ni_vap;
148 struct ieee80211_amrr *amrr = vap->iv_rs;
149 struct ieee80211_amrr_node *amn;
152 if (ni->ni_rctls == NULL) {
153 ni->ni_rctls = amn = malloc(sizeof(struct ieee80211_amrr_node),
154 M_80211_RATECTL, M_NOWAIT|M_ZERO);
156 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
162 amn->amn_amrr = amrr;
163 amn->amn_success = 0;
164 amn->amn_recovery = 0;
165 amn->amn_txcnt = amn->amn_retrycnt = 0;
166 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
168 /* 11n or not? Pick the right rateset */
169 if (amrr_node_is_11n(ni)) {
171 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
172 "%s: 11n node", __func__);
173 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
175 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
176 "%s: non-11n node", __func__);
180 /* Initial rate - lowest */
181 rate = rs->rs_rates[0];
183 /* XXX clear the basic rate flag if it's not 11n */
184 if (! amrr_node_is_11n(ni))
185 rate &= IEEE80211_RATE_VAL;
187 /* pick initial rate from the rateset - HT or otherwise */
188 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
190 /* legacy - anything < 36mbit, stop searching */
191 /* 11n - stop at MCS4 / MCS12 / MCS28 */
192 if (amrr_node_is_11n(ni) &&
193 (rs->rs_rates[amn->amn_rix] & 0x7) < 4)
195 else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
197 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
200 /* if the rate is an 11n rate, ensure the MCS bit is set */
201 if (amrr_node_is_11n(ni))
202 rate |= IEEE80211_RATE_MCS;
204 /* Assign initial rate from the rateset */
205 ni->ni_txrate = rate;
206 amn->amn_ticks = ticks;
208 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
209 "AMRR: nrates=%d, initial rate %d",
215 amrr_node_deinit(struct ieee80211_node *ni)
217 free(ni->ni_rctls, M_80211_RATECTL);
221 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
222 struct ieee80211_node *ni)
224 int rix = amn->amn_rix;
225 const struct ieee80211_rateset *rs = NULL;
227 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
229 /* 11n or not? Pick the right rateset */
230 if (amrr_node_is_11n(ni)) {
232 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
237 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
238 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
239 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
244 * XXX This is totally bogus for 11n, as although high MCS
245 * rates for each stream may be failing, the next stream
248 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
249 * MCS23, we should skip 6/7 and try 8 onwards.
251 if (is_success(amn)) {
253 if (amn->amn_success >= amn->amn_success_threshold &&
254 rix + 1 < rs->rs_nrates) {
255 amn->amn_recovery = 1;
256 amn->amn_success = 0;
258 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
259 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
260 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
261 amn->amn_txcnt, amn->amn_retrycnt);
263 amn->amn_recovery = 0;
265 } else if (is_failure(amn)) {
266 amn->amn_success = 0;
268 if (amn->amn_recovery) {
269 amn->amn_success_threshold *= 2;
270 if (amn->amn_success_threshold >
271 amrr->amrr_max_success_threshold)
272 amn->amn_success_threshold =
273 amrr->amrr_max_success_threshold;
275 amn->amn_success_threshold =
276 amrr->amrr_min_success_threshold;
279 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
280 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
281 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
282 amn->amn_txcnt, amn->amn_retrycnt);
284 amn->amn_recovery = 0;
289 amn->amn_retrycnt = 0;
295 * Return the rate index to use in sending a data frame.
296 * Update our internal state if it's been long enough.
297 * If the rate changes we also update ni_txrate to match.
300 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
302 struct ieee80211_amrr_node *amn = ni->ni_rctls;
303 struct ieee80211_amrr *amrr = amn->amn_amrr;
304 const struct ieee80211_rateset *rs = NULL;
307 /* 11n or not? Pick the right rateset */
308 if (amrr_node_is_11n(ni)) {
310 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
315 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
316 rix = amrr_update(amrr, amn, ni);
317 if (rix != amn->amn_rix) {
318 /* update public rate */
319 ni->ni_txrate = rs->rs_rates[rix];
320 /* XXX strip basic rate flag from txrate, if non-11n */
321 if (amrr_node_is_11n(ni))
322 ni->ni_txrate |= IEEE80211_RATE_MCS;
324 ni->ni_txrate &= IEEE80211_RATE_VAL;
327 amn->amn_ticks = ticks;
334 * Update statistics with tx complete status. Ok is non-zero
335 * if the packet is known to be ACK'd. Retries has the number
336 * retransmissions (i.e. xmit attempts - 1).
339 amrr_tx_complete(const struct ieee80211vap *vap,
340 const struct ieee80211_node *ni, int ok,
341 void *arg1, void *arg2 __unused)
343 struct ieee80211_amrr_node *amn = ni->ni_rctls;
344 int retries = *(int *)arg1;
349 amn->amn_retrycnt += retries;
353 * Set tx count/retry statistics explicitly. Intended for
354 * drivers that poll the device for statistics maintained
358 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
359 void *arg1, void *arg2, void *arg3)
361 struct ieee80211_amrr_node *amn = ni->ni_rctls;
362 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
364 amn->amn_txcnt = txcnt;
365 amn->amn_success = success;
366 amn->amn_retrycnt = retrycnt;
370 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
372 struct ieee80211vap *vap = arg1;
373 struct ieee80211_amrr *amrr = vap->iv_rs;
374 int msecs = ticks_to_msecs(amrr->amrr_interval);
377 error = sysctl_handle_int(oidp, &msecs, 0, req);
378 if (error || !req->newptr)
380 amrr_setinterval(vap, msecs);
385 amrr_sysctlattach(struct ieee80211vap *vap,
386 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
388 struct ieee80211_amrr *amrr = vap->iv_rs;
390 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
391 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
392 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
393 /* XXX bounds check values */
394 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
395 "amrr_max_sucess_threshold", CTLFLAG_RW,
396 &amrr->amrr_max_success_threshold, 0, "");
397 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
398 "amrr_min_sucess_threshold", CTLFLAG_RW,
399 &amrr->amrr_min_success_threshold, 0, "");