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/malloc.h>
37 #include <sys/module.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_ht.h>
54 #include <net80211/ieee80211_amrr.h>
55 #include <net80211/ieee80211_ratectl.h>
57 #define is_success(amn) \
58 ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
59 #define is_failure(amn) \
60 ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
61 #define is_enough(amn) \
62 ((amn)->amn_txcnt > 10)
64 static void amrr_setinterval(const struct ieee80211vap *, int);
65 static void amrr_init(struct ieee80211vap *);
66 static void amrr_deinit(struct ieee80211vap *);
67 static void amrr_node_init(struct ieee80211_node *);
68 static void amrr_node_deinit(struct ieee80211_node *);
69 static int amrr_update(struct ieee80211_amrr *,
70 struct ieee80211_amrr_node *, struct ieee80211_node *);
71 static int amrr_rate(struct ieee80211_node *, void *, uint32_t);
72 static void amrr_tx_complete(const struct ieee80211_node *,
73 const struct ieee80211_ratectl_tx_status *);
74 static void amrr_tx_update_cb(void *, struct ieee80211_node *);
75 static void amrr_tx_update(struct ieee80211vap *vap,
76 struct ieee80211_ratectl_tx_stats *);
77 static void amrr_sysctlattach(struct ieee80211vap *,
78 struct sysctl_ctx_list *, struct sysctl_oid *);
79 static void amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s);
81 /* number of references from net80211 layer */
84 static const struct ieee80211_ratectl amrr = {
89 .ir_deinit = amrr_deinit,
90 .ir_node_init = amrr_node_init,
91 .ir_node_deinit = amrr_node_deinit,
93 .ir_tx_complete = amrr_tx_complete,
94 .ir_tx_update = amrr_tx_update,
95 .ir_setinterval = amrr_setinterval,
96 .ir_node_stats = amrr_node_stats,
98 IEEE80211_RATECTL_MODULE(amrr, 1);
99 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
102 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
104 struct ieee80211_amrr *amrr = vap->iv_rs;
111 amrr->amrr_interval = msecs_to_ticks(msecs);
115 amrr_init(struct ieee80211vap *vap)
117 struct ieee80211_amrr *amrr;
119 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
121 nrefs++; /* XXX locking */
122 amrr = vap->iv_rs = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr),
123 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
125 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
128 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
129 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
130 amrr_setinterval(vap, 500 /* ms */);
131 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
135 amrr_deinit(struct ieee80211vap *vap)
137 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
138 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
139 nrefs--; /* XXX locking */
143 * Return whether 11n rates are possible.
145 * Some 11n devices may return HT information but no HT rates.
146 * Thus, we shouldn't treat them as an 11n node.
149 amrr_node_is_11n(struct ieee80211_node *ni)
152 if (ni->ni_chan == NULL)
154 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
156 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
158 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
162 amrr_node_init(struct ieee80211_node *ni)
164 const struct ieee80211_rateset *rs = NULL;
165 struct ieee80211vap *vap = ni->ni_vap;
166 struct ieee80211_amrr *amrr = vap->iv_rs;
167 struct ieee80211_amrr_node *amn;
171 if_printf(vap->iv_ifp, "ratectl structure was not allocated, "
172 "per-node structure allocation skipped\n");
176 if (ni->ni_rctls == NULL) {
177 ni->ni_rctls = amn = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr_node),
178 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
180 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
186 amn->amn_amrr = amrr;
187 amn->amn_success = 0;
188 amn->amn_recovery = 0;
189 amn->amn_txcnt = amn->amn_retrycnt = 0;
190 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
192 /* 11n or not? Pick the right rateset */
193 if (amrr_node_is_11n(ni)) {
195 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
196 "%s: 11n node", __func__);
197 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
199 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
200 "%s: non-11n node", __func__);
204 /* Initial rate - lowest */
205 rate = rs->rs_rates[0];
207 /* XXX clear the basic rate flag if it's not 11n */
208 if (! amrr_node_is_11n(ni))
209 rate &= IEEE80211_RATE_VAL;
211 /* pick initial rate from the rateset - HT or otherwise */
212 /* Pick something low that's likely to succeed */
213 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
215 /* legacy - anything < 36mbit, stop searching */
216 /* 11n - stop at MCS4 */
217 if (amrr_node_is_11n(ni)) {
218 if ((rs->rs_rates[amn->amn_rix] & 0x1f) < 4)
220 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
223 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
225 /* if the rate is an 11n rate, ensure the MCS bit is set */
226 if (amrr_node_is_11n(ni))
227 rate |= IEEE80211_RATE_MCS;
229 /* Assign initial rate from the rateset */
230 ni->ni_txrate = rate;
231 amn->amn_ticks = ticks;
233 /* XXX TODO: we really need a rate-to-string method */
234 /* XXX TODO: non-11n rate should be divided by two.. */
235 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
236 "AMRR: nrates=%d, initial rate %s%d",
238 amrr_node_is_11n(ni) ? "MCS " : "",
239 rate & IEEE80211_RATE_VAL);
243 amrr_node_deinit(struct ieee80211_node *ni)
245 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
249 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
250 struct ieee80211_node *ni)
252 int rix = amn->amn_rix;
253 const struct ieee80211_rateset *rs = NULL;
255 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
257 /* 11n or not? Pick the right rateset */
258 if (amrr_node_is_11n(ni)) {
260 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
265 /* XXX TODO: we really need a rate-to-string method */
266 /* XXX TODO: non-11n rate should be divided by two.. */
267 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
268 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
269 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
274 * XXX This is totally bogus for 11n, as although high MCS
275 * rates for each stream may be failing, the next stream
278 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
279 * MCS23, we should skip 6/7 and try 8 onwards.
281 if (is_success(amn)) {
283 if (amn->amn_success >= amn->amn_success_threshold &&
284 rix + 1 < rs->rs_nrates) {
285 amn->amn_recovery = 1;
286 amn->amn_success = 0;
288 /* XXX TODO: we really need a rate-to-string method */
289 /* XXX TODO: non-11n rate should be divided by two.. */
290 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
291 "AMRR increasing 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;
297 } else if (is_failure(amn)) {
298 amn->amn_success = 0;
300 if (amn->amn_recovery) {
301 amn->amn_success_threshold *= 2;
302 if (amn->amn_success_threshold >
303 amrr->amrr_max_success_threshold)
304 amn->amn_success_threshold =
305 amrr->amrr_max_success_threshold;
307 amn->amn_success_threshold =
308 amrr->amrr_min_success_threshold;
311 /* XXX TODO: we really need a rate-to-string method */
312 /* XXX TODO: non-11n rate should be divided by two.. */
313 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
314 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
315 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
316 amn->amn_txcnt, amn->amn_retrycnt);
318 amn->amn_recovery = 0;
323 amn->amn_retrycnt = 0;
329 * Return the rate index to use in sending a data frame.
330 * Update our internal state if it's been long enough.
331 * If the rate changes we also update ni_txrate to match.
334 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
336 struct ieee80211_amrr_node *amn = ni->ni_rctls;
337 struct ieee80211_amrr *amrr;
338 const struct ieee80211_rateset *rs = NULL;
341 /* XXX should return -1 here, but drivers may not expect this... */
344 ni->ni_txrate = ni->ni_rates.rs_rates[0];
348 amrr = amn->amn_amrr;
350 /* 11n or not? Pick the right rateset */
351 if (amrr_node_is_11n(ni)) {
353 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
358 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
359 rix = amrr_update(amrr, amn, ni);
360 if (rix != amn->amn_rix) {
361 /* update public rate */
362 ni->ni_txrate = rs->rs_rates[rix];
363 /* XXX strip basic rate flag from txrate, if non-11n */
364 if (amrr_node_is_11n(ni))
365 ni->ni_txrate |= IEEE80211_RATE_MCS;
367 ni->ni_txrate &= IEEE80211_RATE_VAL;
370 amn->amn_ticks = ticks;
377 * Update statistics with tx complete status. Ok is non-zero
378 * if the packet is known to be ACK'd. Retries has the number
379 * retransmissions (i.e. xmit attempts - 1).
382 amrr_tx_complete(const struct ieee80211_node *ni,
383 const struct ieee80211_ratectl_tx_status *status)
385 struct ieee80211_amrr_node *amn = ni->ni_rctls;
392 if (status->flags & IEEE80211_RATECTL_STATUS_LONG_RETRY)
393 retries = status->long_retries;
396 if (status->status == IEEE80211_RATECTL_TX_SUCCESS)
398 amn->amn_retrycnt += retries;
402 amrr_tx_update_cb(void *arg, struct ieee80211_node *ni)
404 struct ieee80211_ratectl_tx_stats *stats = arg;
405 struct ieee80211_amrr_node *amn = ni->ni_rctls;
406 int txcnt, success, retrycnt;
411 txcnt = stats->nframes;
412 success = stats->nsuccess;
414 if (stats->flags & IEEE80211_RATECTL_TX_STATS_RETRIES)
415 retrycnt = stats->nretries;
417 amn->amn_txcnt += txcnt;
418 amn->amn_success += success;
419 amn->amn_retrycnt += retrycnt;
423 * Set tx count/retry statistics explicitly. Intended for
424 * drivers that poll the device for statistics maintained
428 amrr_tx_update(struct ieee80211vap *vap,
429 struct ieee80211_ratectl_tx_stats *stats)
432 if (stats->flags & IEEE80211_RATECTL_TX_STATS_NODE)
433 amrr_tx_update_cb(stats, stats->ni);
435 ieee80211_iterate_nodes_vap(&vap->iv_ic->ic_sta, vap,
436 amrr_tx_update_cb, stats);
441 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
443 struct ieee80211vap *vap = arg1;
444 struct ieee80211_amrr *amrr = vap->iv_rs;
450 msecs = ticks_to_msecs(amrr->amrr_interval);
451 error = sysctl_handle_int(oidp, &msecs, 0, req);
452 if (error || !req->newptr)
454 amrr_setinterval(vap, msecs);
459 amrr_sysctlattach(struct ieee80211vap *vap,
460 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
462 struct ieee80211_amrr *amrr = vap->iv_rs;
467 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
468 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
469 vap, 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
470 /* XXX bounds check values */
471 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
472 "amrr_max_sucess_threshold", CTLFLAG_RW,
473 &amrr->amrr_max_success_threshold, 0, "");
474 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
475 "amrr_min_sucess_threshold", CTLFLAG_RW,
476 &amrr->amrr_min_success_threshold, 0, "");
480 amrr_print_node_rate(struct ieee80211_amrr_node *amn,
481 struct ieee80211_node *ni, struct sbuf *s)
484 struct ieee80211_rateset *rs;
486 if (amrr_node_is_11n(ni)) {
487 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
488 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
489 sbuf_printf(s, "rate: MCS %d\n", rate);
492 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
493 sbuf_printf(s, "rate: %d Mbit\n", rate / 2);
498 amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s)
500 struct ieee80211_amrr_node *amn = ni->ni_rctls;
502 /* XXX TODO: check locking? */
507 amrr_print_node_rate(amn, ni, s);
508 sbuf_printf(s, "ticks: %d\n", amn->amn_ticks);
509 sbuf_printf(s, "txcnt: %u\n", amn->amn_txcnt);
510 sbuf_printf(s, "success: %u\n", amn->amn_success);
511 sbuf_printf(s, "success_threshold: %u\n", amn->amn_success_threshold);
512 sbuf_printf(s, "recovery: %u\n", amn->amn_recovery);
513 sbuf_printf(s, "retry_cnt: %u\n", amn->amn_retrycnt);