xref: /freebsd/sys/net80211/ieee80211_amrr.c (revision b00ab754)
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/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 ieee80211_node *,
73 			const struct ieee80211_ratectl_tx_status *);
74 static void	amrr_tx_update_cb(void *, struct ieee80211_node *);
75 static void	amrr_tx_update(struct ieee80211vap *vap,
76 			struct ieee80211_ratectl_tx_stats *);
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 	nrefs++;		/* XXX locking */
121 	amrr = vap->iv_rs = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr),
122 	    M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
123 	if (amrr == NULL) {
124 		if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
125 		return;
126 	}
127 	amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
128 	amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
129 	amrr_setinterval(vap, 500 /* ms */);
130 	amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
131 }
132 
133 static void
134 amrr_deinit(struct ieee80211vap *vap)
135 {
136 	IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
137 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
138 	nrefs--;		/* XXX locking */
139 }
140 
141 /*
142  * Return whether 11n rates are possible.
143  *
144  * Some 11n devices may return HT information but no HT rates.
145  * Thus, we shouldn't treat them as an 11n node.
146  */
147 static int
148 amrr_node_is_11n(struct ieee80211_node *ni)
149 {
150 
151 	if (ni->ni_chan == NULL)
152 		return (0);
153 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
154 		return (0);
155 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && ni->ni_htrates.rs_nrates == 0)
156 		return (0);
157 	return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
158 }
159 
160 static void
161 amrr_node_init(struct ieee80211_node *ni)
162 {
163 	const struct ieee80211_rateset *rs = NULL;
164 	struct ieee80211vap *vap = ni->ni_vap;
165 	struct ieee80211_amrr *amrr = vap->iv_rs;
166 	struct ieee80211_amrr_node *amn;
167 	uint8_t rate;
168 
169 	if (ni->ni_rctls == NULL) {
170 		ni->ni_rctls = amn = IEEE80211_MALLOC(sizeof(struct ieee80211_amrr_node),
171 		    M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
172 		if (amn == NULL) {
173 			if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
174 			    "structure\n");
175 			return;
176 		}
177 	} else
178 		amn = ni->ni_rctls;
179 	amn->amn_amrr = amrr;
180 	amn->amn_success = 0;
181 	amn->amn_recovery = 0;
182 	amn->amn_txcnt = amn->amn_retrycnt = 0;
183 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
184 
185 	/* 11n or not? Pick the right rateset */
186 	if (amrr_node_is_11n(ni)) {
187 		/* XXX ew */
188 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
189 		    "%s: 11n node", __func__);
190 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
191 	} else {
192 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
193 		    "%s: non-11n node", __func__);
194 		rs = &ni->ni_rates;
195 	}
196 
197 	/* Initial rate - lowest */
198 	rate = rs->rs_rates[0];
199 
200 	/* XXX clear the basic rate flag if it's not 11n */
201 	if (! amrr_node_is_11n(ni))
202 		rate &= IEEE80211_RATE_VAL;
203 
204 	/* pick initial rate from the rateset - HT or otherwise */
205 	/* Pick something low that's likely to succeed */
206 	for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
207 	    amn->amn_rix--) {
208 		/* legacy - anything < 36mbit, stop searching */
209 		/* 11n - stop at MCS4 */
210 		if (amrr_node_is_11n(ni)) {
211 			if ((rs->rs_rates[amn->amn_rix] & 0x1f) < 4)
212 				break;
213 		} else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
214 			break;
215 	}
216 	rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
217 
218 	/* if the rate is an 11n rate, ensure the MCS bit is set */
219 	if (amrr_node_is_11n(ni))
220 		rate |= IEEE80211_RATE_MCS;
221 
222 	/* Assign initial rate from the rateset */
223 	ni->ni_txrate = rate;
224 	amn->amn_ticks = ticks;
225 
226 	/* XXX TODO: we really need a rate-to-string method */
227 	/* XXX TODO: non-11n rate should be divided by two.. */
228 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
229 	    "AMRR: nrates=%d, initial rate %s%d",
230 	    rs->rs_nrates,
231 	    amrr_node_is_11n(ni) ? "MCS " : "",
232 	    rate & IEEE80211_RATE_VAL);
233 }
234 
235 static void
236 amrr_node_deinit(struct ieee80211_node *ni)
237 {
238 	IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
239 }
240 
241 static int
242 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
243     struct ieee80211_node *ni)
244 {
245 	int rix = amn->amn_rix;
246 	const struct ieee80211_rateset *rs = NULL;
247 
248 	KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
249 
250 	/* 11n or not? Pick the right rateset */
251 	if (amrr_node_is_11n(ni)) {
252 		/* XXX ew */
253 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
254 	} else {
255 		rs = &ni->ni_rates;
256 	}
257 
258 	/* XXX TODO: we really need a rate-to-string method */
259 	/* XXX TODO: non-11n rate should be divided by two.. */
260 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
261 	    "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
262 	    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
263 	    amn->amn_txcnt,
264 	    amn->amn_retrycnt);
265 
266 	/*
267 	 * XXX This is totally bogus for 11n, as although high MCS
268 	 * rates for each stream may be failing, the next stream
269 	 * should be checked.
270 	 *
271 	 * Eg, if MCS5 is ok but MCS6/7 isn't, and we can go up to
272 	 * MCS23, we should skip 6/7 and try 8 onwards.
273 	 */
274 	if (is_success(amn)) {
275 		amn->amn_success++;
276 		if (amn->amn_success >= amn->amn_success_threshold &&
277 		    rix + 1 < rs->rs_nrates) {
278 			amn->amn_recovery = 1;
279 			amn->amn_success = 0;
280 			rix++;
281 			/* XXX TODO: we really need a rate-to-string method */
282 			/* XXX TODO: non-11n rate should be divided by two.. */
283 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
284 			    "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
285 			    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
286 			    amn->amn_txcnt, amn->amn_retrycnt);
287 		} else {
288 			amn->amn_recovery = 0;
289 		}
290 	} else if (is_failure(amn)) {
291 		amn->amn_success = 0;
292 		if (rix > 0) {
293 			if (amn->amn_recovery) {
294 				amn->amn_success_threshold *= 2;
295 				if (amn->amn_success_threshold >
296 				    amrr->amrr_max_success_threshold)
297 					amn->amn_success_threshold =
298 					    amrr->amrr_max_success_threshold;
299 			} else {
300 				amn->amn_success_threshold =
301 				    amrr->amrr_min_success_threshold;
302 			}
303 			rix--;
304 			/* XXX TODO: we really need a rate-to-string method */
305 			/* XXX TODO: non-11n rate should be divided by two.. */
306 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
307 			    "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
308 			    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
309 			    amn->amn_txcnt, amn->amn_retrycnt);
310 		}
311 		amn->amn_recovery = 0;
312 	}
313 
314 	/* reset counters */
315 	amn->amn_txcnt = 0;
316 	amn->amn_retrycnt = 0;
317 
318 	return rix;
319 }
320 
321 /*
322  * Return the rate index to use in sending a data frame.
323  * Update our internal state if it's been long enough.
324  * If the rate changes we also update ni_txrate to match.
325  */
326 static int
327 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
328 {
329 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
330 	struct ieee80211_amrr *amrr = amn->amn_amrr;
331 	const struct ieee80211_rateset *rs = NULL;
332 	int rix;
333 
334 	/* 11n or not? Pick the right rateset */
335 	if (amrr_node_is_11n(ni)) {
336 		/* XXX ew */
337 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
338 	} else {
339 		rs = &ni->ni_rates;
340 	}
341 
342 	if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
343 		rix = amrr_update(amrr, amn, ni);
344 		if (rix != amn->amn_rix) {
345 			/* update public rate */
346 			ni->ni_txrate = rs->rs_rates[rix];
347 			/* XXX strip basic rate flag from txrate, if non-11n */
348 			if (amrr_node_is_11n(ni))
349 				ni->ni_txrate |= IEEE80211_RATE_MCS;
350 			else
351 				ni->ni_txrate &= IEEE80211_RATE_VAL;
352 			amn->amn_rix = rix;
353 		}
354 		amn->amn_ticks = ticks;
355 	} else
356 		rix = amn->amn_rix;
357 	return rix;
358 }
359 
360 /*
361  * Update statistics with tx complete status.  Ok is non-zero
362  * if the packet is known to be ACK'd.  Retries has the number
363  * retransmissions (i.e. xmit attempts - 1).
364  */
365 static void
366 amrr_tx_complete(const struct ieee80211_node *ni,
367     const struct ieee80211_ratectl_tx_status *status)
368 {
369 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
370 	int retries;
371 
372 	retries = 0;
373 	if (status->flags & IEEE80211_RATECTL_STATUS_LONG_RETRY)
374 		retries = status->long_retries;
375 
376 	amn->amn_txcnt++;
377 	if (status->status == IEEE80211_RATECTL_TX_SUCCESS)
378 		amn->amn_success++;
379 	amn->amn_retrycnt += retries;
380 }
381 
382 static void
383 amrr_tx_update_cb(void *arg, struct ieee80211_node *ni)
384 {
385 	struct ieee80211_ratectl_tx_stats *stats = arg;
386 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
387 	int txcnt, success, retrycnt;
388 
389 	txcnt = stats->nframes;
390 	success = stats->nsuccess;
391 	retrycnt = 0;
392 	if (stats->flags & IEEE80211_RATECTL_TX_STATS_RETRIES)
393 		retrycnt = stats->nretries;
394 
395 	amn->amn_txcnt += txcnt;
396 	amn->amn_success += success;
397 	amn->amn_retrycnt += retrycnt;
398 }
399 
400 /*
401  * Set tx count/retry statistics explicitly.  Intended for
402  * drivers that poll the device for statistics maintained
403  * in the device.
404  */
405 static void
406 amrr_tx_update(struct ieee80211vap *vap,
407     struct ieee80211_ratectl_tx_stats *stats)
408 {
409 
410 	if (stats->flags & IEEE80211_RATECTL_TX_STATS_NODE)
411 		amrr_tx_update_cb(stats, stats->ni);
412 	else {
413 		ieee80211_iterate_nodes_vap(&vap->iv_ic->ic_sta, vap,
414 		    amrr_tx_update_cb, stats);
415 	}
416 }
417 
418 static int
419 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
420 {
421 	struct ieee80211vap *vap = arg1;
422 	struct ieee80211_amrr *amrr = vap->iv_rs;
423 	int msecs = ticks_to_msecs(amrr->amrr_interval);
424 	int error;
425 
426 	error = sysctl_handle_int(oidp, &msecs, 0, req);
427 	if (error || !req->newptr)
428 		return error;
429 	amrr_setinterval(vap, msecs);
430 	return 0;
431 }
432 
433 static void
434 amrr_sysctlattach(struct ieee80211vap *vap,
435     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
436 {
437 	struct ieee80211_amrr *amrr = vap->iv_rs;
438 
439 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
440 	    "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
441 	    0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
442 	/* XXX bounds check values */
443 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
444 	    "amrr_max_sucess_threshold", CTLFLAG_RW,
445 	    &amrr->amrr_max_success_threshold, 0, "");
446 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
447 	    "amrr_min_sucess_threshold", CTLFLAG_RW,
448 	    &amrr->amrr_min_success_threshold, 0, "");
449 }
450 
451 static void
452 amrr_node_stats(struct ieee80211_node *ni, struct sbuf *s)
453 {
454 	int rate;
455 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
456 	struct ieee80211_rateset *rs;
457 
458 	/* XXX TODO: check locking? */
459 
460 	/* XXX TODO: this should be a method */
461 	if (amrr_node_is_11n(ni)) {
462 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
463 		rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
464 		sbuf_printf(s, "rate: MCS %d\n", rate);
465 	} else {
466 		rs = &ni->ni_rates;
467 		rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
468 		sbuf_printf(s, "rate: %d Mbit\n", rate / 2);
469 	}
470 
471 	sbuf_printf(s, "ticks: %d\n", amn->amn_ticks);
472 	sbuf_printf(s, "txcnt: %u\n", amn->amn_txcnt);
473 	sbuf_printf(s, "success: %u\n", amn->amn_success);
474 	sbuf_printf(s, "success_threshold: %u\n", amn->amn_success_threshold);
475 	sbuf_printf(s, "recovery: %u\n", amn->amn_recovery);
476 	sbuf_printf(s, "retry_cnt: %u\n", amn->amn_retrycnt);
477 }
478