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>
23 * Naive implementation of the Adaptive Multi Rate Retry algorithm:
25 * "IEEE 802.11 Rate Adaptation: A Practical Approach"
26 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
27 * INRIA Sophia - Projet Planete
28 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #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 ieee80211_node *,
71 const struct ieee80211_ratectl_tx_status *);
72 static void amrr_tx_update_cb(void *, struct ieee80211_node *);
73 static void amrr_tx_update(struct ieee80211vap *vap,
74 struct ieee80211_ratectl_tx_stats *);
75 static void amrr_sysctlattach(struct ieee80211vap *,
76 struct sysctl_ctx_list *, struct sysctl_oid *);
77 static void amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s);
79 /* number of references from net80211 layer */
82 static const struct ieee80211_ratectl amrr = {
87 .ir_deinit = amrr_deinit,
88 .ir_node_init = amrr_node_init,
89 .ir_node_deinit = amrr_node_deinit,
91 .ir_tx_complete = amrr_tx_complete,
92 .ir_tx_update = amrr_tx_update,
93 .ir_setinterval = amrr_setinterval,
94 .ir_node_stats = amrr_node_stats,
96 IEEE80211_RATECTL_MODULE(amrr, 1);
97 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
100 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
102 struct ieee80211_amrr *amrr = vap->iv_rs;
109 amrr->amrr_interval = msecs_to_ticks(msecs);
113 amrr_init(struct ieee80211vap *vap)
115 struct ieee80211_amrr *amrr;
117 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
119 nrefs++; /* XXX locking */
120 amrr = vap->iv_rs = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr),
121 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
123 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
126 amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
127 amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
128 amrr_setinterval(vap, 500 /* ms */);
129 amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
133 amrr_deinit(struct ieee80211vap *vap)
135 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
136 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
137 nrefs--; /* XXX locking */
141 * Return whether 11n rates are possible.
143 * Some 11n devices may return HT information but no HT rates.
144 * Thus, we shouldn't treat them as an 11n node.
147 amrr_node_is_11n(struct ieee80211_node *ni)
150 if (ni->ni_chan == NULL)
152 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
154 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
156 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
160 amrr_node_init(struct ieee80211_node *ni)
162 const struct ieee80211_rateset *rs = NULL;
163 struct ieee80211vap *vap = ni->ni_vap;
164 struct ieee80211_amrr *amrr = vap->iv_rs;
165 struct ieee80211_amrr_node *amn;
169 if_printf(vap->iv_ifp, "ratectl structure was not allocated, "
170 "per-node structure allocation skipped\n");
174 if (ni->ni_rctls == NULL) {
175 ni->ni_rctls = amn = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr_node),
176 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
178 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
184 amn->amn_amrr = amrr;
185 amn->amn_success = 0;
186 amn->amn_recovery = 0;
187 amn->amn_txcnt = amn->amn_retrycnt = 0;
188 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
190 /* 11n or not? Pick the right rateset */
191 if (amrr_node_is_11n(ni)) {
193 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
194 "%s: 11n node", __func__);
195 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
197 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
198 "%s: non-11n node", __func__);
202 /* Initial rate - lowest */
203 rate = rs->rs_rates[0];
205 /* XXX clear the basic rate flag if it's not 11n */
206 if (! amrr_node_is_11n(ni))
207 rate &= IEEE80211_RATE_VAL;
209 /* pick initial rate from the rateset - HT or otherwise */
210 /* Pick something low that's likely to succeed */
211 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
213 /* legacy - anything < 36mbit, stop searching */
214 /* 11n - stop at MCS4 */
215 if (amrr_node_is_11n(ni)) {
216 if ((rs->rs_rates[amn->amn_rix] & 0x1f) < 4)
218 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
221 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
223 /* if the rate is an 11n rate, ensure the MCS bit is set */
224 if (amrr_node_is_11n(ni))
225 rate |= IEEE80211_RATE_MCS;
227 /* Assign initial rate from the rateset */
228 ni->ni_txrate = rate;
229 amn->amn_ticks = ticks;
231 /* XXX TODO: we really need a rate-to-string method */
232 /* XXX TODO: non-11n rate should be divided by two.. */
233 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
234 "AMRR: nrates=%d, initial rate %s%d",
236 amrr_node_is_11n(ni) ? "MCS " : "",
237 rate & IEEE80211_RATE_VAL);
241 amrr_node_deinit(struct ieee80211_node *ni)
243 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
247 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
248 struct ieee80211_node *ni)
250 int rix = amn->amn_rix;
251 const struct ieee80211_rateset *rs = NULL;
253 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
255 /* 11n or not? Pick the right rateset */
256 if (amrr_node_is_11n(ni)) {
258 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
263 /* XXX TODO: we really need a rate-to-string method */
264 /* XXX TODO: non-11n rate should be divided by two.. */
265 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
266 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
267 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
272 * XXX This is totally bogus for 11n, as although high MCS
273 * rates for each stream may be failing, the next stream
276 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
277 * MCS23, we should skip 6/7 and try 8 onwards.
279 if (is_success(amn)) {
281 if (amn->amn_success >= amn->amn_success_threshold &&
282 rix + 1 < rs->rs_nrates) {
283 amn->amn_recovery = 1;
284 amn->amn_success = 0;
286 /* XXX TODO: we really need a rate-to-string method */
287 /* XXX TODO: non-11n rate should be divided by two.. */
288 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
289 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
290 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
291 amn->amn_txcnt, amn->amn_retrycnt);
293 amn->amn_recovery = 0;
295 } else if (is_failure(amn)) {
296 amn->amn_success = 0;
298 if (amn->amn_recovery) {
299 amn->amn_success_threshold *= 2;
300 if (amn->amn_success_threshold >
301 amrr->amrr_max_success_threshold)
302 amn->amn_success_threshold =
303 amrr->amrr_max_success_threshold;
305 amn->amn_success_threshold =
306 amrr->amrr_min_success_threshold;
309 /* XXX TODO: we really need a rate-to-string method */
310 /* XXX TODO: non-11n rate should be divided by two.. */
311 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
312 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
313 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
314 amn->amn_txcnt, amn->amn_retrycnt);
316 amn->amn_recovery = 0;
321 amn->amn_retrycnt = 0;
327 * Return the rate index to use in sending a data frame.
328 * Update our internal state if it's been long enough.
329 * If the rate changes we also update ni_txrate to match.
332 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
334 struct ieee80211_amrr_node *amn = ni->ni_rctls;
335 struct ieee80211_amrr *amrr;
336 const struct ieee80211_rateset *rs = NULL;
339 /* XXX should return -1 here, but drivers may not expect this... */
342 ni->ni_txrate = ni->ni_rates.rs_rates[0];
346 amrr = amn->amn_amrr;
348 /* 11n or not? Pick the right rateset */
349 if (amrr_node_is_11n(ni)) {
351 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
356 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
357 rix = amrr_update(amrr, amn, ni);
358 if (rix != amn->amn_rix) {
359 /* update public rate */
360 ni->ni_txrate = rs->rs_rates[rix];
361 /* XXX strip basic rate flag from txrate, if non-11n */
362 if (amrr_node_is_11n(ni))
363 ni->ni_txrate |= IEEE80211_RATE_MCS;
365 ni->ni_txrate &= IEEE80211_RATE_VAL;
368 amn->amn_ticks = ticks;
375 * Update statistics with tx complete status. Ok is non-zero
376 * if the packet is known to be ACK'd. Retries has the number
377 * retransmissions (i.e. xmit attempts - 1).
380 amrr_tx_complete(const struct ieee80211_node *ni,
381 const struct ieee80211_ratectl_tx_status *status)
383 struct ieee80211_amrr_node *amn = ni->ni_rctls;
390 if (status->flags & IEEE80211_RATECTL_STATUS_LONG_RETRY)
391 retries = status->long_retries;
394 if (status->status == IEEE80211_RATECTL_TX_SUCCESS)
396 amn->amn_retrycnt += retries;
400 amrr_tx_update_cb(void *arg, struct ieee80211_node *ni)
402 struct ieee80211_ratectl_tx_stats *stats = arg;
403 struct ieee80211_amrr_node *amn = ni->ni_rctls;
404 int txcnt, success, retrycnt;
409 txcnt = stats->nframes;
410 success = stats->nsuccess;
412 if (stats->flags & IEEE80211_RATECTL_TX_STATS_RETRIES)
413 retrycnt = stats->nretries;
415 amn->amn_txcnt += txcnt;
416 amn->amn_success += success;
417 amn->amn_retrycnt += retrycnt;
421 * Set tx count/retry statistics explicitly. Intended for
422 * drivers that poll the device for statistics maintained
426 amrr_tx_update(struct ieee80211vap *vap,
427 struct ieee80211_ratectl_tx_stats *stats)
430 if (stats->flags & IEEE80211_RATECTL_TX_STATS_NODE)
431 amrr_tx_update_cb(stats, stats->ni);
433 ieee80211_iterate_nodes_vap(&vap->iv_ic->ic_sta, vap,
434 amrr_tx_update_cb, stats);
439 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
441 struct ieee80211vap *vap = arg1;
442 struct ieee80211_amrr *amrr = vap->iv_rs;
448 msecs = ticks_to_msecs(amrr->amrr_interval);
449 error = sysctl_handle_int(oidp, &msecs, 0, req);
450 if (error || !req->newptr)
452 amrr_setinterval(vap, msecs);
457 amrr_sysctlattach(struct ieee80211vap *vap,
458 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
460 struct ieee80211_amrr *amrr = vap->iv_rs;
465 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
466 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
467 vap, 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
468 /* XXX bounds check values */
469 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
470 "amrr_max_sucess_threshold", CTLFLAG_RW,
471 &amrr->amrr_max_success_threshold, 0, "");
472 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
473 "amrr_min_sucess_threshold", CTLFLAG_RW,
474 &amrr->amrr_min_success_threshold, 0, "");
478 amrr_print_node_rate(struct ieee80211_amrr_node *amn,
479 struct ieee80211_node *ni, struct sbuf *s)
482 struct ieee80211_rateset *rs;
484 if (amrr_node_is_11n(ni)) {
485 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
486 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
487 sbuf_printf(s, "rate: MCS %d\n", rate);
490 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
491 sbuf_printf(s, "rate: %d Mbit\n", rate / 2);
496 amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s)
498 struct ieee80211_amrr_node *amn = ni->ni_rctls;
500 /* XXX TODO: check locking? */
505 amrr_print_node_rate(amn, ni, s);
506 sbuf_printf(s, "ticks: %d\n", amn->amn_ticks);
507 sbuf_printf(s, "txcnt: %u\n", amn->amn_txcnt);
508 sbuf_printf(s, "success: %u\n", amn->amn_success);
509 sbuf_printf(s, "success_threshold: %u\n", amn->amn_success_threshold);
510 sbuf_printf(s, "recovery: %u\n", amn->amn_recovery);
511 sbuf_printf(s, "retry_cnt: %u\n", amn->amn_retrycnt);