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 <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_ht.h>
52 #include <netproto/802_11/ieee80211_amrr.h>
53 #include <netproto/802_11/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 = kmalloc(sizeof(struct ieee80211_amrr),
117 M_80211_RATECTL, M_INTWAIT|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 kfree(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 = kmalloc(sizeof(struct ieee80211_amrr_node),
164 M_80211_RATECTL, M_INTWAIT|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 /* Pick something low that's likely to succeed */
199 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
201 /* legacy - anything < 36mbit, stop searching */
202 /* 11n - stop at MCS4 */
203 if (amrr_node_is_11n(ni)) {
204 if ((rs->rs_rates[amn->amn_rix] & 0x1f) < 4)
206 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
209 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
211 /* if the rate is an 11n rate, ensure the MCS bit is set */
212 if (amrr_node_is_11n(ni))
213 rate |= IEEE80211_RATE_MCS;
215 /* Assign initial rate from the rateset */
216 ni->ni_txrate = rate;
217 amn->amn_ticks = ticks;
219 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
220 "AMRR: nrates=%d, initial rate %d",
226 amrr_node_deinit(struct ieee80211_node *ni)
228 kfree(ni->ni_rctls, M_80211_RATECTL);
232 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
233 struct ieee80211_node *ni)
235 int rix = amn->amn_rix;
236 const struct ieee80211_rateset *rs = NULL;
238 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
240 /* 11n or not? Pick the right rateset */
241 if (amrr_node_is_11n(ni)) {
243 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
248 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
249 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
250 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
255 * XXX This is totally bogus for 11n, as although high MCS
256 * rates for each stream may be failing, the next stream
259 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
260 * MCS23, we should skip 6/7 and try 8 onwards.
262 if (is_success(amn)) {
264 if (amn->amn_success >= amn->amn_success_threshold &&
265 rix + 1 < rs->rs_nrates) {
266 amn->amn_recovery = 1;
267 amn->amn_success = 0;
269 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
270 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
271 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
272 amn->amn_txcnt, amn->amn_retrycnt);
274 amn->amn_recovery = 0;
276 } else if (is_failure(amn)) {
277 amn->amn_success = 0;
279 if (amn->amn_recovery) {
280 amn->amn_success_threshold *= 2;
281 if (amn->amn_success_threshold >
282 amrr->amrr_max_success_threshold)
283 amn->amn_success_threshold =
284 amrr->amrr_max_success_threshold;
286 amn->amn_success_threshold =
287 amrr->amrr_min_success_threshold;
290 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
291 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
292 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
293 amn->amn_txcnt, amn->amn_retrycnt);
295 amn->amn_recovery = 0;
300 amn->amn_retrycnt = 0;
306 * Return the rate index to use in sending a data frame.
307 * Update our internal state if it's been long enough.
308 * If the rate changes we also update ni_txrate to match.
311 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
313 struct ieee80211_amrr_node *amn = ni->ni_rctls;
314 struct ieee80211_amrr *amrr = amn->amn_amrr;
315 const struct ieee80211_rateset *rs = NULL;
318 /* 11n or not? Pick the right rateset */
319 if (amrr_node_is_11n(ni)) {
321 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
326 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
327 rix = amrr_update(amrr, amn, ni);
328 if (rix != amn->amn_rix) {
329 /* update public rate */
330 ni->ni_txrate = rs->rs_rates[rix];
331 /* XXX strip basic rate flag from txrate, if non-11n */
332 if (amrr_node_is_11n(ni))
333 ni->ni_txrate |= IEEE80211_RATE_MCS;
335 ni->ni_txrate &= IEEE80211_RATE_VAL;
338 amn->amn_ticks = ticks;
345 * Update statistics with tx complete status. Ok is non-zero
346 * if the packet is known to be ACK'd. Retries has the number
347 * retransmissions (i.e. xmit attempts - 1).
350 amrr_tx_complete(const struct ieee80211vap *vap,
351 const struct ieee80211_node *ni, int ok,
352 void *arg1, void *arg2 __unused)
354 struct ieee80211_amrr_node *amn = ni->ni_rctls;
355 int retries = *(int *)arg1;
360 amn->amn_retrycnt += retries;
364 * Set tx count/retry statistics explicitly. Intended for
365 * drivers that poll the device for statistics maintained
369 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
370 void *arg1, void *arg2, void *arg3)
372 struct ieee80211_amrr_node *amn = ni->ni_rctls;
373 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
375 amn->amn_txcnt = txcnt;
376 amn->amn_success = success;
377 amn->amn_retrycnt = retrycnt;
381 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
383 struct ieee80211vap *vap = arg1;
384 struct ieee80211_amrr *amrr = vap->iv_rs;
385 int msecs = ticks_to_msecs(amrr->amrr_interval);
388 error = sysctl_handle_int(oidp, &msecs, 0, req);
389 if (error || !req->newptr)
391 amrr_setinterval(vap, msecs);
396 amrr_sysctlattach(struct ieee80211vap *vap,
397 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
399 struct ieee80211_amrr *amrr = vap->iv_rs;
401 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
402 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
403 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
404 /* XXX bounds check values */
405 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
406 "amrr_max_sucess_threshold", CTLFLAG_RW,
407 &amrr->amrr_max_success_threshold, 0, "");
408 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
409 "amrr_min_sucess_threshold", CTLFLAG_RW,
410 &amrr->amrr_min_success_threshold, 0, "");