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_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_ht.h>
52 #include <net80211/ieee80211_amrr.h>
53 #include <net80211/ieee80211_ratectl.h>
55 #define is_success(amn) \
56 ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
57 #define is_failure(amn) \
58 ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
59 #define is_enough(amn) \
60 ((amn)->amn_txcnt > 10)
62 static void amrr_setinterval(const struct ieee80211vap *, int);
63 static void amrr_init(struct ieee80211vap *);
64 static void amrr_deinit(struct ieee80211vap *);
65 static void amrr_node_init(struct ieee80211_node *);
66 static void amrr_node_deinit(struct ieee80211_node *);
67 static int amrr_update(struct ieee80211_amrr *,
68 struct ieee80211_amrr_node *, struct ieee80211_node *);
69 static int amrr_rate(struct ieee80211_node *, void *, uint32_t);
70 static void amrr_tx_complete(const struct ieee80211vap *,
71 const struct ieee80211_node *, int,
73 static void amrr_tx_update(const struct ieee80211vap *vap,
74 const struct ieee80211_node *, void *, void *, void *);
75 static void amrr_sysctlattach(struct ieee80211vap *,
76 struct sysctl_ctx_list *, struct sysctl_oid *);
78 /* number of references from net80211 layer */
81 static const struct ieee80211_ratectl amrr = {
86 .ir_deinit = amrr_deinit,
87 .ir_node_init = amrr_node_init,
88 .ir_node_deinit = amrr_node_deinit,
90 .ir_tx_complete = amrr_tx_complete,
91 .ir_tx_update = amrr_tx_update,
92 .ir_setinterval = amrr_setinterval,
94 IEEE80211_RATECTL_MODULE(amrr, 1);
95 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
98 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
100 struct ieee80211_amrr *amrr = vap->iv_rs;
105 t = msecs_to_ticks(msecs);
106 amrr->amrr_interval = (t < 1) ? 1 : t;
110 amrr_init(struct ieee80211vap *vap)
112 struct ieee80211_amrr *amrr;
114 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
116 amrr = vap->iv_rs = malloc(sizeof(struct ieee80211_amrr),
117 M_80211_RATECTL, M_NOWAIT|M_ZERO);
119 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
122 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
123 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
124 amrr_setinterval(vap, 500 /* ms */);
125 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
129 amrr_deinit(struct ieee80211vap *vap)
131 free(vap->iv_rs, M_80211_RATECTL);
135 * Return whether 11n rates are possible.
137 * Some 11n devices may return HT information but no HT rates.
138 * Thus, we shouldn't treat them as an 11n node.
141 amrr_node_is_11n(struct ieee80211_node *ni)
144 if (ni->ni_chan == NULL)
146 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
148 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
150 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
154 amrr_node_init(struct ieee80211_node *ni)
156 const struct ieee80211_rateset *rs = NULL;
157 struct ieee80211vap *vap = ni->ni_vap;
158 struct ieee80211_amrr *amrr = vap->iv_rs;
159 struct ieee80211_amrr_node *amn;
162 if (ni->ni_rctls == NULL) {
163 ni->ni_rctls = amn = malloc(sizeof(struct ieee80211_amrr_node),
164 M_80211_RATECTL, M_NOWAIT|M_ZERO);
166 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
172 amn->amn_amrr = amrr;
173 amn->amn_success = 0;
174 amn->amn_recovery = 0;
175 amn->amn_txcnt = amn->amn_retrycnt = 0;
176 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
178 /* 11n or not? Pick the right rateset */
179 if (amrr_node_is_11n(ni)) {
181 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
182 "%s: 11n node", __func__);
183 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
185 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
186 "%s: non-11n node", __func__);
190 /* Initial rate - lowest */
191 rate = rs->rs_rates[0];
193 /* XXX clear the basic rate flag if it's not 11n */
194 if (! amrr_node_is_11n(ni))
195 rate &= IEEE80211_RATE_VAL;
197 /* pick initial rate from the rateset - HT or otherwise */
198 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
200 /* legacy - anything < 36mbit, stop searching */
201 /* 11n - stop at MCS4 / MCS12 / MCS28 */
202 if (amrr_node_is_11n(ni)) {
203 if ((rs->rs_rates[amn->amn_rix] & 0x7) < 4)
205 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
208 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
210 /* if the rate is an 11n rate, ensure the MCS bit is set */
211 if (amrr_node_is_11n(ni))
212 rate |= IEEE80211_RATE_MCS;
214 /* Assign initial rate from the rateset */
215 ni->ni_txrate = rate;
216 amn->amn_ticks = ticks;
218 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
219 "AMRR: nrates=%d, initial rate %d",
225 amrr_node_deinit(struct ieee80211_node *ni)
227 free(ni->ni_rctls, M_80211_RATECTL);
231 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
232 struct ieee80211_node *ni)
234 int rix = amn->amn_rix;
235 const struct ieee80211_rateset *rs = NULL;
237 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
239 /* 11n or not? Pick the right rateset */
240 if (amrr_node_is_11n(ni)) {
242 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
247 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
248 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
249 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
254 * XXX This is totally bogus for 11n, as although high MCS
255 * rates for each stream may be failing, the next stream
258 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
259 * MCS23, we should skip 6/7 and try 8 onwards.
261 if (is_success(amn)) {
263 if (amn->amn_success >= amn->amn_success_threshold &&
264 rix + 1 < rs->rs_nrates) {
265 amn->amn_recovery = 1;
266 amn->amn_success = 0;
268 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
269 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
270 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
271 amn->amn_txcnt, amn->amn_retrycnt);
273 amn->amn_recovery = 0;
275 } else if (is_failure(amn)) {
276 amn->amn_success = 0;
278 if (amn->amn_recovery) {
279 amn->amn_success_threshold *= 2;
280 if (amn->amn_success_threshold >
281 amrr->amrr_max_success_threshold)
282 amn->amn_success_threshold =
283 amrr->amrr_max_success_threshold;
285 amn->amn_success_threshold =
286 amrr->amrr_min_success_threshold;
289 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
290 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
291 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
292 amn->amn_txcnt, amn->amn_retrycnt);
294 amn->amn_recovery = 0;
299 amn->amn_retrycnt = 0;
305 * Return the rate index to use in sending a data frame.
306 * Update our internal state if it's been long enough.
307 * If the rate changes we also update ni_txrate to match.
310 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
312 struct ieee80211_amrr_node *amn = ni->ni_rctls;
313 struct ieee80211_amrr *amrr = amn->amn_amrr;
314 const struct ieee80211_rateset *rs = NULL;
317 /* 11n or not? Pick the right rateset */
318 if (amrr_node_is_11n(ni)) {
320 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
325 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
326 rix = amrr_update(amrr, amn, ni);
327 if (rix != amn->amn_rix) {
328 /* update public rate */
329 ni->ni_txrate = rs->rs_rates[rix];
330 /* XXX strip basic rate flag from txrate, if non-11n */
331 if (amrr_node_is_11n(ni))
332 ni->ni_txrate |= IEEE80211_RATE_MCS;
334 ni->ni_txrate &= IEEE80211_RATE_VAL;
337 amn->amn_ticks = ticks;
344 * Update statistics with tx complete status. Ok is non-zero
345 * if the packet is known to be ACK'd. Retries has the number
346 * retransmissions (i.e. xmit attempts - 1).
349 amrr_tx_complete(const struct ieee80211vap *vap,
350 const struct ieee80211_node *ni, int ok,
351 void *arg1, void *arg2 __unused)
353 struct ieee80211_amrr_node *amn = ni->ni_rctls;
354 int retries = *(int *)arg1;
359 amn->amn_retrycnt += retries;
363 * Set tx count/retry statistics explicitly. Intended for
364 * drivers that poll the device for statistics maintained
368 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
369 void *arg1, void *arg2, void *arg3)
371 struct ieee80211_amrr_node *amn = ni->ni_rctls;
372 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
374 amn->amn_txcnt = txcnt;
375 amn->amn_success = success;
376 amn->amn_retrycnt = retrycnt;
380 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
382 struct ieee80211vap *vap = arg1;
383 struct ieee80211_amrr *amrr = vap->iv_rs;
384 int msecs = ticks_to_msecs(amrr->amrr_interval);
387 error = sysctl_handle_int(oidp, &msecs, 0, req);
388 if (error || !req->newptr)
390 amrr_setinterval(vap, msecs);
395 amrr_sysctlattach(struct ieee80211vap *vap,
396 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
398 struct ieee80211_amrr *amrr = vap->iv_rs;
400 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
401 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
402 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
403 /* XXX bounds check values */
404 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
405 "amrr_max_sucess_threshold", CTLFLAG_RW,
406 &amrr->amrr_max_success_threshold, 0, "");
407 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
408 "amrr_min_sucess_threshold", CTLFLAG_RW,
409 &amrr->amrr_min_success_threshold, 0, "");