xref: /freebsd/sys/net80211/ieee80211_amrr.c (revision 51e235148a4becba94e824a44bd69687644a7f56)
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 /*-
23  * Naive implementation of the Adaptive Multi Rate Retry algorithm:
24  *
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
29  */
30 #include "opt_wlan.h"
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/sbuf.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 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);
78 
79 /* number of references from net80211 layer */
80 static	int nrefs = 0;
81 
82 static const struct ieee80211_ratectl amrr = {
83 	.ir_name	= "amrr",
84 	.ir_attach	= NULL,
85 	.ir_detach	= NULL,
86 	.ir_init	= amrr_init,
87 	.ir_deinit	= amrr_deinit,
88 	.ir_node_init	= amrr_node_init,
89 	.ir_node_deinit	= amrr_node_deinit,
90 	.ir_rate	= amrr_rate,
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,
95 };
96 IEEE80211_RATECTL_MODULE(amrr, 1);
97 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
98 
99 static void
100 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
101 {
102 	struct ieee80211_amrr *amrr = vap->iv_rs;
103 
104 	if (!amrr)
105 		return;
106 
107 	if (msecs < 100)
108 		msecs = 100;
109 	amrr->amrr_interval = msecs_to_ticks(msecs);
110 }
111 
112 static void
113 amrr_init(struct ieee80211vap *vap)
114 {
115 	struct ieee80211_amrr *amrr;
116 
117 	KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
118 
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);
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 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
137 	nrefs--;		/* XXX locking */
138 }
139 
140 /*
141  * Return whether 11n rates are possible.
142  *
143  * Some 11n devices may return HT information but no HT rates.
144  * Thus, we shouldn't treat them as an 11n node.
145  */
146 static int
147 amrr_node_is_11n(struct ieee80211_node *ni)
148 {
149 
150 	if (ni->ni_chan == NULL)
151 		return (0);
152 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
153 		return (0);
154 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
155 		return (0);
156 	return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
157 }
158 
159 static void
160 amrr_node_init(struct ieee80211_node *ni)
161 {
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;
166 	uint8_t rate;
167 
168 	if (!amrr) {
169 		if_printf(vap->iv_ifp, "ratectl structure was not allocated, "
170 		    "per-node structure allocation skipped\n");
171 		return;
172 	}
173 
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);
177 		if (amn == NULL) {
178 			if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
179 			    "structure\n");
180 			return;
181 		}
182 	} else
183 		amn = ni->ni_rctls;
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;
189 
190 	/* 11n or not? Pick the right rateset */
191 	if (amrr_node_is_11n(ni)) {
192 		/* XXX ew */
193 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
194 		    "%s: 11n node", __func__);
195 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
196 	} else {
197 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
198 		    "%s: non-11n node", __func__);
199 		rs = &ni->ni_rates;
200 	}
201 
202 	/* Initial rate - lowest */
203 	rate = rs->rs_rates[0];
204 
205 	/* XXX clear the basic rate flag if it's not 11n */
206 	if (! amrr_node_is_11n(ni))
207 		rate &= IEEE80211_RATE_VAL;
208 
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;
212 	    amn->amn_rix--) {
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)
217 				break;
218 		} else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
219 			break;
220 	}
221 	rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
222 
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;
226 
227 	/* Assign initial rate from the rateset */
228 	ni->ni_txrate = rate;
229 	amn->amn_ticks = ticks;
230 
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",
235 	    rs->rs_nrates,
236 	    amrr_node_is_11n(ni) ? "MCS " : "",
237 	    rate & IEEE80211_RATE_VAL);
238 }
239 
240 static void
241 amrr_node_deinit(struct ieee80211_node *ni)
242 {
243 	IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
244 }
245 
246 static int
247 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
248     struct ieee80211_node *ni)
249 {
250 	int rix = amn->amn_rix;
251 	const struct ieee80211_rateset *rs = NULL;
252 
253 	KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
254 
255 	/* 11n or not? Pick the right rateset */
256 	if (amrr_node_is_11n(ni)) {
257 		/* XXX ew */
258 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
259 	} else {
260 		rs = &ni->ni_rates;
261 	}
262 
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,
268 	    amn->amn_txcnt,
269 	    amn->amn_retrycnt);
270 
271 	/*
272 	 * XXX This is totally bogus for 11n, as although high MCS
273 	 * rates for each stream may be failing, the next stream
274 	 * should be checked.
275 	 *
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.
278 	 */
279 	if (is_success(amn)) {
280 		amn->amn_success++;
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;
285 			rix++;
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);
292 		} else {
293 			amn->amn_recovery = 0;
294 		}
295 	} else if (is_failure(amn)) {
296 		amn->amn_success = 0;
297 		if (rix > 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;
304 			} else {
305 				amn->amn_success_threshold =
306 				    amrr->amrr_min_success_threshold;
307 			}
308 			rix--;
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);
315 		}
316 		amn->amn_recovery = 0;
317 	}
318 
319 	/* reset counters */
320 	amn->amn_txcnt = 0;
321 	amn->amn_retrycnt = 0;
322 
323 	return rix;
324 }
325 
326 /*
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.
330  */
331 static int
332 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
333 {
334 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
335 	struct ieee80211_amrr *amrr;
336 	const struct ieee80211_rateset *rs = NULL;
337 	int rix;
338 
339 	/* XXX should return -1 here, but drivers may not expect this... */
340 	if (!amn)
341 	{
342 		ni->ni_txrate = ni->ni_rates.rs_rates[0];
343 		return 0;
344 	}
345 
346 	amrr = amn->amn_amrr;
347 
348 	/* 11n or not? Pick the right rateset */
349 	if (amrr_node_is_11n(ni)) {
350 		/* XXX ew */
351 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
352 	} else {
353 		rs = &ni->ni_rates;
354 	}
355 
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;
364 			else
365 				ni->ni_txrate &= IEEE80211_RATE_VAL;
366 			amn->amn_rix = rix;
367 		}
368 		amn->amn_ticks = ticks;
369 	} else
370 		rix = amn->amn_rix;
371 	return rix;
372 }
373 
374 /*
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).
378  */
379 static void
380 amrr_tx_complete(const struct ieee80211_node *ni,
381     const struct ieee80211_ratectl_tx_status *status)
382 {
383 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
384 	int retries;
385 
386 	if (!amn)
387 		return;
388 
389 	retries = 0;
390 	if (status->flags & IEEE80211_RATECTL_STATUS_LONG_RETRY)
391 		retries = status->long_retries;
392 
393 	amn->amn_txcnt++;
394 	if (status->status == IEEE80211_RATECTL_TX_SUCCESS)
395 		amn->amn_success++;
396 	amn->amn_retrycnt += retries;
397 }
398 
399 static void
400 amrr_tx_update_cb(void *arg, struct ieee80211_node *ni)
401 {
402 	struct ieee80211_ratectl_tx_stats *stats = arg;
403 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
404 	int txcnt, success, retrycnt;
405 
406 	if (!amn)
407 		return;
408 
409 	txcnt = stats->nframes;
410 	success = stats->nsuccess;
411 	retrycnt = 0;
412 	if (stats->flags & IEEE80211_RATECTL_TX_STATS_RETRIES)
413 		retrycnt = stats->nretries;
414 
415 	amn->amn_txcnt += txcnt;
416 	amn->amn_success += success;
417 	amn->amn_retrycnt += retrycnt;
418 }
419 
420 /*
421  * Set tx count/retry statistics explicitly.  Intended for
422  * drivers that poll the device for statistics maintained
423  * in the device.
424  */
425 static void
426 amrr_tx_update(struct ieee80211vap *vap,
427     struct ieee80211_ratectl_tx_stats *stats)
428 {
429 
430 	if (stats->flags & IEEE80211_RATECTL_TX_STATS_NODE)
431 		amrr_tx_update_cb(stats, stats->ni);
432 	else {
433 		ieee80211_iterate_nodes_vap(&vap->iv_ic->ic_sta, vap,
434 		    amrr_tx_update_cb, stats);
435 	}
436 }
437 
438 static int
439 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
440 {
441 	struct ieee80211vap *vap = arg1;
442 	struct ieee80211_amrr *amrr = vap->iv_rs;
443 	int msecs, error;
444 
445 	if (!amrr)
446 		return ENOMEM;
447 
448 	msecs = ticks_to_msecs(amrr->amrr_interval);
449 	error = sysctl_handle_int(oidp, &msecs, 0, req);
450 	if (error || !req->newptr)
451 		return error;
452 	amrr_setinterval(vap, msecs);
453 	return 0;
454 }
455 
456 static void
457 amrr_sysctlattach(struct ieee80211vap *vap,
458     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
459 {
460 	struct ieee80211_amrr *amrr = vap->iv_rs;
461 
462 	if (!amrr)
463 		return;
464 
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, "");
475 }
476 
477 static void
478 amrr_print_node_rate(struct ieee80211_amrr_node *amn,
479     struct ieee80211_node *ni, struct sbuf *s)
480 {
481 	int rate;
482 	struct ieee80211_rateset *rs;
483 
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);
488 	} else {
489 		rs = &ni->ni_rates;
490 		rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
491 		sbuf_printf(s, "rate: %d Mbit\n", rate / 2);
492 	}
493 }
494 
495 static void
496 amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s)
497 {
498 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
499 
500 	/* XXX TODO: check locking? */
501 
502 	if (!amn)
503 		return;
504 
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);
512 }
513