wlan - Update wlan from Adrian / FreeBSD
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_amrr.c
CommitLineData
63a787e0
RP
1/* $OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $ */
2
3/*-
4028af95 4 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
63a787e0
RP
5 * Copyright (c) 2006
6 * Damien Bergamini <damien.bergamini@free.fr>
7 *
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.
11 *
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.
63a787e0
RP
19 */
20
085ff963
MD
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD$");
4028af95 23
63a787e0
RP
24/*-
25 * Naive implementation of the Adaptive Multi Rate Retry algorithm:
26 *
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
31 */
32#include "opt_wlan.h"
33
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>
39
40#include <net/if.h>
085ff963 41#include <net/if_var.h>
63a787e0 42#include <net/if_media.h>
085ff963 43#include <net/ethernet.h>
63a787e0
RP
44
45#ifdef INET
46#include <netinet/in.h>
47#include <netinet/if_ether.h>
48#endif
49
50#include <netproto/802_11/ieee80211_var.h>
085ff963 51#include <netproto/802_11/ieee80211_ht.h>
63a787e0 52#include <netproto/802_11/ieee80211_amrr.h>
4028af95 53#include <netproto/802_11/ieee80211_ratectl.h>
63a787e0
RP
54
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)
61
4028af95
RP
62static void amrr_setinterval(const struct ieee80211vap *, int);
63static void amrr_init(struct ieee80211vap *);
64static void amrr_deinit(struct ieee80211vap *);
65static void amrr_node_init(struct ieee80211_node *);
66static void amrr_node_deinit(struct ieee80211_node *);
67static int amrr_update(struct ieee80211_amrr *,
68 struct ieee80211_amrr_node *, struct ieee80211_node *);
69static int amrr_rate(struct ieee80211_node *, void *, uint32_t);
70static void amrr_tx_complete(const struct ieee80211vap *,
71 const struct ieee80211_node *, int,
72 void *, void *);
73static void amrr_tx_update(const struct ieee80211vap *vap,
74 const struct ieee80211_node *, void *, void *, void *);
75static void amrr_sysctlattach(struct ieee80211vap *,
76 struct sysctl_ctx_list *, struct sysctl_oid *);
63a787e0
RP
77
78/* number of references from net80211 layer */
79static int nrefs = 0;
80
4028af95
RP
81static const struct ieee80211_ratectl amrr = {
82 .ir_name = "amrr",
83 .ir_attach = NULL,
84 .ir_detach = NULL,
85 .ir_init = amrr_init,
86 .ir_deinit = amrr_deinit,
87 .ir_node_init = amrr_node_init,
88 .ir_node_deinit = amrr_node_deinit,
89 .ir_rate = amrr_rate,
90 .ir_tx_complete = amrr_tx_complete,
91 .ir_tx_update = amrr_tx_update,
92 .ir_setinterval = amrr_setinterval,
93};
94IEEE80211_RATECTL_MODULE(amrr, 1);
95IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
96
97static void
98amrr_setinterval(const struct ieee80211vap *vap, int msecs)
63a787e0 99{
4028af95 100 struct ieee80211_amrr *amrr = vap->iv_rs;
63a787e0
RP
101 int t;
102
103 if (msecs < 100)
104 msecs = 100;
105 t = msecs_to_ticks(msecs);
106 amrr->amrr_interval = (t < 1) ? 1 : t;
107}
108
4028af95
RP
109static void
110amrr_init(struct ieee80211vap *vap)
63a787e0 111{
4028af95 112 struct ieee80211_amrr *amrr;
63a787e0 113
4028af95
RP
114 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
115
085ff963
MD
116 amrr = vap->iv_rs = kmalloc(sizeof(struct ieee80211_amrr),
117 M_80211_RATECTL, M_INTWAIT|M_ZERO);
118 if (amrr == NULL) {
119 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
120 return;
121 }
4028af95
RP
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);
63a787e0
RP
126}
127
4028af95
RP
128static void
129amrr_deinit(struct ieee80211vap *vap)
63a787e0 130{
4028af95 131 kfree(vap->iv_rs, M_80211_RATECTL);
63a787e0
RP
132}
133
085ff963
MD
134/*
135 * Return whether 11n rates are possible.
136 *
137 * Some 11n devices may return HT information but no HT rates.
138 * Thus, we shouldn't treat them as an 11n node.
139 */
140static int
141amrr_node_is_11n(struct ieee80211_node *ni)
142{
143
144 if (ni->ni_chan == NULL)
145 return (0);
146 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
147 return (0);
148 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
149 return (0);
150 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
151}
152
4028af95
RP
153static void
154amrr_node_init(struct ieee80211_node *ni)
63a787e0 155{
085ff963 156 const struct ieee80211_rateset *rs = NULL;
4028af95
RP
157 struct ieee80211vap *vap = ni->ni_vap;
158 struct ieee80211_amrr *amrr = vap->iv_rs;
159 struct ieee80211_amrr_node *amn;
085ff963 160 uint8_t rate;
4028af95 161
a72c14d0 162 if (ni->ni_rctls == NULL) {
085ff963
MD
163 ni->ni_rctls = amn = kmalloc(sizeof(struct ieee80211_amrr_node),
164 M_80211_RATECTL, M_INTWAIT|M_ZERO);
165 if (amn == NULL) {
166 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
167 "structure\n");
168 return;
169 }
170 } else
a72c14d0 171 amn = ni->ni_rctls;
63a787e0
RP
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;
177
085ff963
MD
178 /* 11n or not? Pick the right rateset */
179 if (amrr_node_is_11n(ni)) {
180 /* XXX ew */
181 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
182 "%s: 11n node", __func__);
183 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
184 } else {
185 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
186 "%s: non-11n node", __func__);
187 rs = &ni->ni_rates;
188 }
189
190 /* Initial rate - lowest */
191 rate = rs->rs_rates[0];
192
193 /* XXX clear the basic rate flag if it's not 11n */
194 if (! amrr_node_is_11n(ni))
195 rate &= IEEE80211_RATE_VAL;
196
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;
200 amn->amn_rix--) {
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)
205 break;
206 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
207 break;
208 }
209 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
210
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;
214
215 /* Assign initial rate from the rateset */
216 ni->ni_txrate = rate;
63a787e0
RP
217 amn->amn_ticks = ticks;
218
219 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
085ff963
MD
220 "AMRR: nrates=%d, initial rate %d",
221 rs->rs_nrates,
222 rate);
63a787e0
RP
223}
224
4028af95
RP
225static void
226amrr_node_deinit(struct ieee80211_node *ni)
227{
af2f0638 228 kfree(ni->ni_rctls, M_80211_RATECTL);
4028af95
RP
229}
230
63a787e0
RP
231static int
232amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
233 struct ieee80211_node *ni)
234{
235 int rix = amn->amn_rix;
085ff963 236 const struct ieee80211_rateset *rs = NULL;
63a787e0
RP
237
238 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
239
085ff963
MD
240 /* 11n or not? Pick the right rateset */
241 if (amrr_node_is_11n(ni)) {
242 /* XXX ew */
243 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
244 } else {
245 rs = &ni->ni_rates;
246 }
247
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,
251 amn->amn_txcnt,
252 amn->amn_retrycnt);
253
254 /*
255 * XXX This is totally bogus for 11n, as although high MCS
256 * rates for each stream may be failing, the next stream
257 * should be checked.
258 *
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.
261 */
63a787e0
RP
262 if (is_success(amn)) {
263 amn->amn_success++;
264 if (amn->amn_success >= amn->amn_success_threshold &&
085ff963 265 rix + 1 < rs->rs_nrates) {
63a787e0
RP
266 amn->amn_recovery = 1;
267 amn->amn_success = 0;
268 rix++;
269 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
270 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
085ff963 271 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
63a787e0
RP
272 amn->amn_txcnt, amn->amn_retrycnt);
273 } else {
274 amn->amn_recovery = 0;
275 }
276 } else if (is_failure(amn)) {
277 amn->amn_success = 0;
278 if (rix > 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;
285 } else {
286 amn->amn_success_threshold =
287 amrr->amrr_min_success_threshold;
288 }
289 rix--;
290 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
291 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
085ff963 292 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
63a787e0
RP
293 amn->amn_txcnt, amn->amn_retrycnt);
294 }
295 amn->amn_recovery = 0;
296 }
297
298 /* reset counters */
299 amn->amn_txcnt = 0;
300 amn->amn_retrycnt = 0;
301
302 return rix;
303}
304
305/*
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.
309 */
4028af95
RP
310static int
311amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
63a787e0 312{
4028af95 313 struct ieee80211_amrr_node *amn = ni->ni_rctls;
63a787e0 314 struct ieee80211_amrr *amrr = amn->amn_amrr;
085ff963 315 const struct ieee80211_rateset *rs = NULL;
63a787e0
RP
316 int rix;
317
085ff963
MD
318 /* 11n or not? Pick the right rateset */
319 if (amrr_node_is_11n(ni)) {
320 /* XXX ew */
321 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
322 } else {
323 rs = &ni->ni_rates;
324 }
325
63a787e0
RP
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 */
085ff963
MD
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;
334 else
335 ni->ni_txrate &= IEEE80211_RATE_VAL;
63a787e0
RP
336 amn->amn_rix = rix;
337 }
338 amn->amn_ticks = ticks;
339 } else
340 rix = amn->amn_rix;
341 return rix;
342}
343
4028af95
RP
344/*
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).
348 */
349static void
350amrr_tx_complete(const struct ieee80211vap *vap,
351 const struct ieee80211_node *ni, int ok,
352 void *arg1, void *arg2 __unused)
353{
354 struct ieee80211_amrr_node *amn = ni->ni_rctls;
355 int retries = *(int *)arg1;
356
357 amn->amn_txcnt++;
358 if (ok)
359 amn->amn_success++;
360 amn->amn_retrycnt += retries;
361}
362
363/*
364 * Set tx count/retry statistics explicitly. Intended for
365 * drivers that poll the device for statistics maintained
366 * in the device.
367 */
368static void
369amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
370 void *arg1, void *arg2, void *arg3)
371{
372 struct ieee80211_amrr_node *amn = ni->ni_rctls;
373 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
374
375 amn->amn_txcnt = txcnt;
376 amn->amn_success = success;
377 amn->amn_retrycnt = retrycnt;
378}
379
63a787e0
RP
380static int
381amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
382{
4028af95
RP
383 struct ieee80211vap *vap = arg1;
384 struct ieee80211_amrr *amrr = vap->iv_rs;
63a787e0
RP
385 int msecs = ticks_to_msecs(amrr->amrr_interval);
386 int error;
387
388 error = sysctl_handle_int(oidp, &msecs, 0, req);
085ff963
MD
389 if (error || !req->newptr)
390 return error;
391 amrr_setinterval(vap, msecs);
392 return 0;
63a787e0
RP
393}
394
395static void
4028af95 396amrr_sysctlattach(struct ieee80211vap *vap,
63a787e0
RP
397 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
398{
4028af95 399 struct ieee80211_amrr *amrr = vap->iv_rs;
63a787e0
RP
400
401 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4028af95 402 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
63a787e0
RP
403 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
404 /* XXX bounds check values */
085ff963 405 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
63a787e0
RP
406 "amrr_max_sucess_threshold", CTLFLAG_RW,
407 &amrr->amrr_max_success_threshold, 0, "");
085ff963 408 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
63a787e0
RP
409 "amrr_min_sucess_threshold", CTLFLAG_RW,
410 &amrr->amrr_min_success_threshold, 0, "");
411}