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: releng/11.0/sys/net80211/ieee80211_amrr.c 302307 2016-07-01 19:58:13Z adrian $");
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/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sbuf.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
46
47 #ifdef INET
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
50 #endif
51
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_ht.h>
54 #include <net80211/ieee80211_amrr.h>
55 #include <net80211/ieee80211_ratectl.h>
56
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)
63
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 ieee80211vap *,
73 const struct ieee80211_node *, int,
74 void *, void *);
75 static void amrr_tx_update(const struct ieee80211vap *vap,
76 const struct ieee80211_node *, void *, void *, void *);
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);
80
81 /* number of references from net80211 layer */
82 static int nrefs = 0;
83
84 static const struct ieee80211_ratectl amrr = {
85 .ir_name = "amrr",
86 .ir_attach = NULL,
87 .ir_detach = NULL,
88 .ir_init = amrr_init,
89 .ir_deinit = amrr_deinit,
90 .ir_node_init = amrr_node_init,
91 .ir_node_deinit = amrr_node_deinit,
92 .ir_rate = amrr_rate,
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,
97 };
98 IEEE80211_RATECTL_MODULE(amrr, 1);
99 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
100
101 static void
102 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
103 {
104 struct ieee80211_amrr *amrr = vap->iv_rs;
105 int t;
106
107 if (msecs < 100)
108 msecs = 100;
109 t = msecs_to_ticks(msecs);
110 amrr->amrr_interval = (t < 1) ? 1 : t;
111 }
112
113 static void
114 amrr_init(struct ieee80211vap *vap)
115 {
116 struct ieee80211_amrr *amrr;
117
118 KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
119
120 amrr = vap->iv_rs = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr),
121 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
122 if (amrr == NULL) {
123 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
124 return;
125 }
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);
130 }
131
132 static void
133 amrr_deinit(struct ieee80211vap *vap)
134 {
135 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
136 }
137
138 /*
139 * Return whether 11n rates are possible.
140 *
141 * Some 11n devices may return HT information but no HT rates.
142 * Thus, we shouldn't treat them as an 11n node.
143 */
144 static int
145 amrr_node_is_11n(struct ieee80211_node *ni)
146 {
147
148 if (ni->ni_chan == NULL)
149 return (0);
150 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
151 return (0);
152 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
153 return (0);
154 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
155 }
156
157 static void
158 amrr_node_init(struct ieee80211_node *ni)
159 {
160 const struct ieee80211_rateset *rs = NULL;
161 struct ieee80211vap *vap = ni->ni_vap;
162 struct ieee80211_amrr *amrr = vap->iv_rs;
163 struct ieee80211_amrr_node *amn;
164 uint8_t rate;
165
166 if (ni->ni_rctls == NULL) {
167 ni->ni_rctls = amn = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr_node),
168 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
169 if (amn == NULL) {
170 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
171 "structure\n");
172 return;
173 }
174 } else
175 amn = ni->ni_rctls;
176 amn->amn_amrr = amrr;
177 amn->amn_success = 0;
178 amn->amn_recovery = 0;
179 amn->amn_txcnt = amn->amn_retrycnt = 0;
180 amn->amn_success_threshold = amrr->amrr_min_success_threshold;
181
182 /* 11n or not? Pick the right rateset */
183 if (amrr_node_is_11n(ni)) {
184 /* XXX ew */
185 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
186 "%s: 11n node", __func__);
187 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
188 } else {
189 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
190 "%s: non-11n node", __func__);
191 rs = &ni->ni_rates;
192 }
193
194 /* Initial rate - lowest */
195 rate = rs->rs_rates[0];
196
197 /* XXX clear the basic rate flag if it's not 11n */
198 if (! amrr_node_is_11n(ni))
199 rate &= IEEE80211_RATE_VAL;
200
201 /* pick initial rate from the rateset - HT or otherwise */
202 /* Pick something low that's likely to succeed */
203 for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
204 amn->amn_rix--) {
205 /* legacy - anything < 36mbit, stop searching */
206 /* 11n - stop at MCS4 */
207 if (amrr_node_is_11n(ni)) {
208 if ((rs->rs_rates[amn->amn_rix] & 0x1f) < 4)
209 break;
210 } else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
211 break;
212 }
213 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
214
215 /* if the rate is an 11n rate, ensure the MCS bit is set */
216 if (amrr_node_is_11n(ni))
217 rate |= IEEE80211_RATE_MCS;
218
219 /* Assign initial rate from the rateset */
220 ni->ni_txrate = rate;
221 amn->amn_ticks = ticks;
222
223 /* XXX TODO: we really need a rate-to-string method */
224 /* XXX TODO: non-11n rate should be divided by two.. */
225 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
226 "AMRR: nrates=%d, initial rate %s%d",
227 rs->rs_nrates,
228 amrr_node_is_11n(ni) ? "MCS " : "",
229 rate & IEEE80211_RATE_VAL);
230 }
231
232 static void
233 amrr_node_deinit(struct ieee80211_node *ni)
234 {
235 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
236 }
237
238 static int
239 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
240 struct ieee80211_node *ni)
241 {
242 int rix = amn->amn_rix;
243 const struct ieee80211_rateset *rs = NULL;
244
245 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
246
247 /* 11n or not? Pick the right rateset */
248 if (amrr_node_is_11n(ni)) {
249 /* XXX ew */
250 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
251 } else {
252 rs = &ni->ni_rates;
253 }
254
255 /* XXX TODO: we really need a rate-to-string method */
256 /* XXX TODO: non-11n rate should be divided by two.. */
257 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
258 "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
259 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
260 amn->amn_txcnt,
261 amn->amn_retrycnt);
262
263 /*
264 * XXX This is totally bogus for 11n, as although high MCS
265 * rates for each stream may be failing, the next stream
266 * should be checked.
267 *
268 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
269 * MCS23, we should skip 6/7 and try 8 onwards.
270 */
271 if (is_success(amn)) {
272 amn->amn_success++;
273 if (amn->amn_success >= amn->amn_success_threshold &&
274 rix + 1 < rs->rs_nrates) {
275 amn->amn_recovery = 1;
276 amn->amn_success = 0;
277 rix++;
278 /* XXX TODO: we really need a rate-to-string method */
279 /* XXX TODO: non-11n rate should be divided by two.. */
280 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
281 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
282 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
283 amn->amn_txcnt, amn->amn_retrycnt);
284 } else {
285 amn->amn_recovery = 0;
286 }
287 } else if (is_failure(amn)) {
288 amn->amn_success = 0;
289 if (rix > 0) {
290 if (amn->amn_recovery) {
291 amn->amn_success_threshold *= 2;
292 if (amn->amn_success_threshold >
293 amrr->amrr_max_success_threshold)
294 amn->amn_success_threshold =
295 amrr->amrr_max_success_threshold;
296 } else {
297 amn->amn_success_threshold =
298 amrr->amrr_min_success_threshold;
299 }
300 rix--;
301 /* XXX TODO: we really need a rate-to-string method */
302 /* XXX TODO: non-11n rate should be divided by two.. */
303 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
304 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
305 rs->rs_rates[rix] & IEEE80211_RATE_VAL,
306 amn->amn_txcnt, amn->amn_retrycnt);
307 }
308 amn->amn_recovery = 0;
309 }
310
311 /* reset counters */
312 amn->amn_txcnt = 0;
313 amn->amn_retrycnt = 0;
314
315 return rix;
316 }
317
318 /*
319 * Return the rate index to use in sending a data frame.
320 * Update our internal state if it's been long enough.
321 * If the rate changes we also update ni_txrate to match.
322 */
323 static int
324 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
325 {
326 struct ieee80211_amrr_node *amn = ni->ni_rctls;
327 struct ieee80211_amrr *amrr = amn->amn_amrr;
328 const struct ieee80211_rateset *rs = NULL;
329 int rix;
330
331 /* 11n or not? Pick the right rateset */
332 if (amrr_node_is_11n(ni)) {
333 /* XXX ew */
334 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
335 } else {
336 rs = &ni->ni_rates;
337 }
338
339 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
340 rix = amrr_update(amrr, amn, ni);
341 if (rix != amn->amn_rix) {
342 /* update public rate */
343 ni->ni_txrate = rs->rs_rates[rix];
344 /* XXX strip basic rate flag from txrate, if non-11n */
345 if (amrr_node_is_11n(ni))
346 ni->ni_txrate |= IEEE80211_RATE_MCS;
347 else
348 ni->ni_txrate &= IEEE80211_RATE_VAL;
349 amn->amn_rix = rix;
350 }
351 amn->amn_ticks = ticks;
352 } else
353 rix = amn->amn_rix;
354 return rix;
355 }
356
357 /*
358 * Update statistics with tx complete status. Ok is non-zero
359 * if the packet is known to be ACK'd. Retries has the number
360 * retransmissions (i.e. xmit attempts - 1).
361 */
362 static void
363 amrr_tx_complete(const struct ieee80211vap *vap,
364 const struct ieee80211_node *ni, int ok,
365 void *arg1, void *arg2 __unused)
366 {
367 struct ieee80211_amrr_node *amn = ni->ni_rctls;
368 int retries = *(int *)arg1;
369
370 amn->amn_txcnt++;
371 if (ok)
372 amn->amn_success++;
373 amn->amn_retrycnt += retries;
374 }
375
376 /*
377 * Set tx count/retry statistics explicitly. Intended for
378 * drivers that poll the device for statistics maintained
379 * in the device.
380 */
381 static void
382 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
383 void *arg1, void *arg2, void *arg3)
384 {
385 struct ieee80211_amrr_node *amn = ni->ni_rctls;
386 int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
387
388 amn->amn_txcnt = txcnt;
389 amn->amn_success = success;
390 amn->amn_retrycnt = retrycnt;
391 }
392
393 static int
394 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
395 {
396 struct ieee80211vap *vap = arg1;
397 struct ieee80211_amrr *amrr = vap->iv_rs;
398 int msecs = ticks_to_msecs(amrr->amrr_interval);
399 int error;
400
401 error = sysctl_handle_int(oidp, &msecs, 0, req);
402 if (error || !req->newptr)
403 return error;
404 amrr_setinterval(vap, msecs);
405 return 0;
406 }
407
408 static void
409 amrr_sysctlattach(struct ieee80211vap *vap,
410 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
411 {
412 struct ieee80211_amrr *amrr = vap->iv_rs;
413
414 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
415 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
416 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
417 /* XXX bounds check values */
418 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
419 "amrr_max_sucess_threshold", CTLFLAG_RW,
420 &amrr->amrr_max_success_threshold, 0, "");
421 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
422 "amrr_min_sucess_threshold", CTLFLAG_RW,
423 &amrr->amrr_min_success_threshold, 0, "");
424 }
425
426 static void
427 amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s)
428 {
429 int rate;
430 struct ieee80211_amrr_node *amn = ni->ni_rctls;
431 struct ieee80211_rateset *rs;
432
433 /* XXX TODO: check locking? */
434
435 /* XXX TODO: this should be a method */
436 if (amrr_node_is_11n(ni)) {
437 rs = (struct ieee80211_rateset *) &ni->ni_htrates;
438 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
439 sbuf_printf(s, "rate: MCS %d\n", rate);
440 } else {
441 rs = &ni->ni_rates;
442 rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
443 sbuf_printf(s, "rate: %d Mbit\n", rate / 2);
444 }
445
446 sbuf_printf(s, "ticks: %d\n", amn->amn_ticks);
447 sbuf_printf(s, "txcnt: %u\n", amn->amn_txcnt);
448 sbuf_printf(s, "success: %u\n", amn->amn_success);
449 sbuf_printf(s, "success_threshold: %u\n", amn->amn_success_threshold);
450 sbuf_printf(s, "recovery: %u\n", amn->amn_recovery);
451 sbuf_printf(s, "retry_cnt: %u\n", amn->amn_retrycnt);
452 }
Cache object: 6b27654079b8cf0a4ac52a42fbd55ceb
|