1 /* $OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
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.
19 */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
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>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
44
45 #ifdef INET
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #endif
49
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_ht.h>
52 #include <net80211/ieee80211_amrr.h>
53 #include <net80211/ieee80211_ratectl.h>
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
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,
72 void *, void *);
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 *);
77
78 /* number of references from net80211 layer */
79 static int nrefs = 0;
80
81 static 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 };
94 IEEE80211_RATECTL_MODULE(amrr, 1);
95 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
96
97 static void
amrr_setinterval(const struct ieee80211vap * vap,int msecs)98 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
99 {
100 struct ieee80211_amrr *amrr = vap->iv_rs;
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
109 static void
amrr_init(struct ieee80211vap * vap)110 amrr_init(struct ieee80211vap *vap)
111 {
112 struct ieee80211_amrr *amrr;
113
114 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
115
116 amrr = vap->iv_rs = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr),
117 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
118 if (amrr == NULL) {
119 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
120 return;
121 }
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);
126 }
127
128 static void
amrr_deinit(struct ieee80211vap * vap)129 amrr_deinit(struct ieee80211vap *vap)
130 {
131 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
132 }
133
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 */
140 static int
amrr_node_is_11n(struct ieee80211_node * ni)141 amrr_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
153 static void
amrr_node_init(struct ieee80211_node * ni)154 amrr_node_init(struct ieee80211_node *ni)
155 {
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;
160 uint8_t rate;
161
162 if (ni->ni_rctls == NULL) {
163 ni->ni_rctls = amn = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr_node),
164 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_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
171 amn = ni->ni_rctls;
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
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;
217 amn->amn_ticks = ticks;
218
219 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
220 "AMRR: nrates=%d, initial rate %d",
221 rs->rs_nrates,
222 rate);
223 }
224
225 static void
amrr_node_deinit(struct ieee80211_node * ni)226 amrr_node_deinit(struct ieee80211_node *ni)
227 {
228 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
229 }
230
231 static int
amrr_update(struct ieee80211_amrr * amrr,struct ieee80211_amrr_node * amn,struct ieee80211_node * ni)232 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
233 struct ieee80211_node *ni)
234 {
235 int rix = amn->amn_rix;
236 const struct ieee80211_rateset *rs = NULL;
237
238 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
239
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 */
262 if (is_success(amn)) {
263 amn->amn_success++;
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;
268 rix++;
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);
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)",
292 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
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 */
310 static int
amrr_rate(struct ieee80211_node * ni,void * arg __unused,uint32_t iarg __unused)311 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
312 {
313 struct ieee80211_amrr_node *amn = ni->ni_rctls;
314 struct ieee80211_amrr *amrr = amn->amn_amrr;
315 const struct ieee80211_rateset *rs = NULL;
316 int rix;
317
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
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;
334 else
335 ni->ni_txrate &= IEEE80211_RATE_VAL;
336 amn->amn_rix = rix;
337 }
338 amn->amn_ticks = ticks;
339 } else
340 rix = amn->amn_rix;
341 return rix;
342 }
343
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 */
349 static void
amrr_tx_complete(const struct ieee80211vap * vap,const struct ieee80211_node * ni,int ok,void * arg1,void * arg2 __unused)350 amrr_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 */
368 static void
amrr_tx_update(const struct ieee80211vap * vap,const struct ieee80211_node * ni,void * arg1,void * arg2,void * arg3)369 amrr_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
380 static int
amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)381 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
382 {
383 struct ieee80211vap *vap = arg1;
384 struct ieee80211_amrr *amrr = vap->iv_rs;
385 int msecs = ticks_to_msecs(amrr->amrr_interval);
386 int error;
387
388 error = sysctl_handle_int(oidp, &msecs, 0, req);
389 if (error || !req->newptr)
390 return error;
391 amrr_setinterval(vap, msecs);
392 return 0;
393 }
394
395 static void
amrr_sysctlattach(struct ieee80211vap * vap,struct sysctl_ctx_list * ctx,struct sysctl_oid * tree)396 amrr_sysctlattach(struct ieee80211vap *vap,
397 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
398 {
399 struct ieee80211_amrr *amrr = vap->iv_rs;
400
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, "");
411 }
412