xref: /freebsd/sys/net80211/ieee80211_amrr.c (revision 521fc850a0d917c94b875b8b8083de0e35383d35)
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_media.h>
42 
43 #ifdef INET
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
46 #endif
47 
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_ht.h>
50 #include <net80211/ieee80211_amrr.h>
51 #include <net80211/ieee80211_ratectl.h>
52 
53 #define is_success(amn)	\
54 	((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
55 #define is_failure(amn)	\
56 	((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
57 #define is_enough(amn)		\
58 	((amn)->amn_txcnt > 10)
59 
60 static void	amrr_setinterval(const struct ieee80211vap *, int);
61 static void	amrr_init(struct ieee80211vap *);
62 static void	amrr_deinit(struct ieee80211vap *);
63 static void	amrr_node_init(struct ieee80211_node *);
64 static void	amrr_node_deinit(struct ieee80211_node *);
65 static int	amrr_update(struct ieee80211_amrr *,
66     			struct ieee80211_amrr_node *, struct ieee80211_node *);
67 static int	amrr_rate(struct ieee80211_node *, void *, uint32_t);
68 static void	amrr_tx_complete(const struct ieee80211vap *,
69     			const struct ieee80211_node *, int,
70 			void *, void *);
71 static void	amrr_tx_update(const struct ieee80211vap *vap,
72 			const struct ieee80211_node *, void *, void *, void *);
73 static void	amrr_sysctlattach(struct ieee80211vap *,
74 			struct sysctl_ctx_list *, struct sysctl_oid *);
75 
76 /* number of references from net80211 layer */
77 static	int nrefs = 0;
78 
79 static const struct ieee80211_ratectl amrr = {
80 	.ir_name	= "amrr",
81 	.ir_attach	= NULL,
82 	.ir_detach	= NULL,
83 	.ir_init	= amrr_init,
84 	.ir_deinit	= amrr_deinit,
85 	.ir_node_init	= amrr_node_init,
86 	.ir_node_deinit	= amrr_node_deinit,
87 	.ir_rate	= amrr_rate,
88 	.ir_tx_complete	= amrr_tx_complete,
89 	.ir_tx_update	= amrr_tx_update,
90 	.ir_setinterval	= amrr_setinterval,
91 };
92 IEEE80211_RATECTL_MODULE(amrr, 1);
93 IEEE80211_RATECTL_ALG(amrr, IEEE80211_RATECTL_AMRR, amrr);
94 
95 static void
96 amrr_setinterval(const struct ieee80211vap *vap, int msecs)
97 {
98 	struct ieee80211_amrr *amrr = vap->iv_rs;
99 	int t;
100 
101 	if (msecs < 100)
102 		msecs = 100;
103 	t = msecs_to_ticks(msecs);
104 	amrr->amrr_interval = (t < 1) ? 1 : t;
105 }
106 
107 static void
108 amrr_init(struct ieee80211vap *vap)
109 {
110 	struct ieee80211_amrr *amrr;
111 
112 	KASSERT(vap->iv_rs == NULL, ("%s called multiple times", __func__));
113 
114 	amrr = vap->iv_rs = malloc(sizeof(struct ieee80211_amrr),
115 	    M_80211_RATECTL, M_NOWAIT|M_ZERO);
116 	if (amrr == NULL) {
117 		if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
118 		return;
119 	}
120 	amrr->amrr_min_success_threshold = IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD;
121 	amrr->amrr_max_success_threshold = IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD;
122 	amrr_setinterval(vap, 500 /* ms */);
123 	amrr_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
124 }
125 
126 static void
127 amrr_deinit(struct ieee80211vap *vap)
128 {
129 	free(vap->iv_rs, M_80211_RATECTL);
130 }
131 
132 static int
133 amrr_node_is_11n(struct ieee80211_node *ni)
134 {
135 
136 	if (ni->ni_chan == NULL)
137 		return (0);
138 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
139 		return (0);
140 	return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
141 }
142 
143 static void
144 amrr_node_init(struct ieee80211_node *ni)
145 {
146 	const struct ieee80211_rateset *rs = NULL;
147 	struct ieee80211vap *vap = ni->ni_vap;
148 	struct ieee80211_amrr *amrr = vap->iv_rs;
149 	struct ieee80211_amrr_node *amn;
150 	uint8_t rate;
151 
152 	if (ni->ni_rctls == NULL) {
153 		ni->ni_rctls = amn = malloc(sizeof(struct ieee80211_amrr_node),
154 		    M_80211_RATECTL, M_NOWAIT|M_ZERO);
155 		if (amn == NULL) {
156 			if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
157 			    "structure\n");
158 			return;
159 		}
160 	} else
161 		amn = ni->ni_rctls;
162 	amn->amn_amrr = amrr;
163 	amn->amn_success = 0;
164 	amn->amn_recovery = 0;
165 	amn->amn_txcnt = amn->amn_retrycnt = 0;
166 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
167 
168 	/* 11n or not? Pick the right rateset */
169 	if (amrr_node_is_11n(ni)) {
170 		/* XXX ew */
171 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
172 		    "%s: 11n node", __func__);
173 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
174 	} else {
175 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
176 		    "%s: non-11n node", __func__);
177 		rs = &ni->ni_rates;
178 	}
179 
180 	/* Initial rate - lowest */
181 	rate = rs->rs_rates[0];
182 
183 	/* XXX clear the basic rate flag if it's not 11n */
184 	if (! amrr_node_is_11n(ni))
185 		rate &= IEEE80211_RATE_VAL;
186 
187 	/* pick initial rate from the rateset - HT or otherwise */
188 	for (amn->amn_rix = rs->rs_nrates - 1; amn->amn_rix > 0;
189 	    amn->amn_rix--) {
190 		/* legacy - anything < 36mbit, stop searching */
191 		/* 11n - stop at MCS4 / MCS12 / MCS28 */
192 		if (amrr_node_is_11n(ni) &&
193 		    (rs->rs_rates[amn->amn_rix] & 0x7) < 4)
194 			break;
195 		else if ((rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) <= 72)
196 			break;
197 		rate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL;
198 	}
199 
200 	/* if the rate is an 11n rate, ensure the MCS bit is set */
201 	if (amrr_node_is_11n(ni))
202 		rate |= IEEE80211_RATE_MCS;
203 
204 	/* Assign initial rate from the rateset */
205 	ni->ni_txrate = rate;
206 	amn->amn_ticks = ticks;
207 
208 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
209 	    "AMRR: nrates=%d, initial rate %d",
210 	    rs->rs_nrates,
211 	    rate);
212 }
213 
214 static void
215 amrr_node_deinit(struct ieee80211_node *ni)
216 {
217 	free(ni->ni_rctls, M_80211_RATECTL);
218 }
219 
220 static int
221 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn,
222     struct ieee80211_node *ni)
223 {
224 	int rix = amn->amn_rix;
225 	const struct ieee80211_rateset *rs = NULL;
226 
227 	KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt));
228 
229 	/* 11n or not? Pick the right rateset */
230 	if (amrr_node_is_11n(ni)) {
231 		/* XXX ew */
232 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
233 	} else {
234 		rs = &ni->ni_rates;
235 	}
236 
237 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
238 	    "AMRR: current rate %d, txcnt=%d, retrycnt=%d",
239 	    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
240 	    amn->amn_txcnt,
241 	    amn->amn_retrycnt);
242 
243 	if (is_success(amn)) {
244 		amn->amn_success++;
245 		if (amn->amn_success >= amn->amn_success_threshold &&
246 		    rix + 1 < rs->rs_nrates) {
247 			amn->amn_recovery = 1;
248 			amn->amn_success = 0;
249 			rix++;
250 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
251 			    "AMRR increasing rate %d (txcnt=%d retrycnt=%d)",
252 			    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
253 			    amn->amn_txcnt, amn->amn_retrycnt);
254 		} else {
255 			amn->amn_recovery = 0;
256 		}
257 	} else if (is_failure(amn)) {
258 		amn->amn_success = 0;
259 		if (rix > 0) {
260 			if (amn->amn_recovery) {
261 				amn->amn_success_threshold *= 2;
262 				if (amn->amn_success_threshold >
263 				    amrr->amrr_max_success_threshold)
264 					amn->amn_success_threshold =
265 					    amrr->amrr_max_success_threshold;
266 			} else {
267 				amn->amn_success_threshold =
268 				    amrr->amrr_min_success_threshold;
269 			}
270 			rix--;
271 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
272 			    "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)",
273 			    rs->rs_rates[rix] & IEEE80211_RATE_VAL,
274 			    amn->amn_txcnt, amn->amn_retrycnt);
275 		}
276 		amn->amn_recovery = 0;
277 	}
278 
279 	/* reset counters */
280 	amn->amn_txcnt = 0;
281 	amn->amn_retrycnt = 0;
282 
283 	return rix;
284 }
285 
286 /*
287  * Return the rate index to use in sending a data frame.
288  * Update our internal state if it's been long enough.
289  * If the rate changes we also update ni_txrate to match.
290  */
291 static int
292 amrr_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg __unused)
293 {
294 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
295 	struct ieee80211_amrr *amrr = amn->amn_amrr;
296 	const struct ieee80211_rateset *rs = NULL;
297 	int rix;
298 
299 	/* 11n or not? Pick the right rateset */
300 	if (amrr_node_is_11n(ni)) {
301 		/* XXX ew */
302 		rs = (struct ieee80211_rateset *) &ni->ni_htrates;
303 	} else {
304 		rs = &ni->ni_rates;
305 	}
306 
307 	if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) {
308 		rix = amrr_update(amrr, amn, ni);
309 		if (rix != amn->amn_rix) {
310 			/* update public rate */
311 			ni->ni_txrate = rs->rs_rates[rix];
312 			/* XXX strip basic rate flag from txrate, if non-11n */
313 			if (amrr_node_is_11n(ni))
314 				ni->ni_txrate |= IEEE80211_RATE_MCS;
315 			else
316 				ni->ni_txrate &= IEEE80211_RATE_VAL;
317 			amn->amn_rix = rix;
318 		}
319 		amn->amn_ticks = ticks;
320 	} else
321 		rix = amn->amn_rix;
322 	return rix;
323 }
324 
325 /*
326  * Update statistics with tx complete status.  Ok is non-zero
327  * if the packet is known to be ACK'd.  Retries has the number
328  * retransmissions (i.e. xmit attempts - 1).
329  */
330 static void
331 amrr_tx_complete(const struct ieee80211vap *vap,
332     const struct ieee80211_node *ni, int ok,
333     void *arg1, void *arg2 __unused)
334 {
335 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
336 	int retries = *(int *)arg1;
337 
338 	amn->amn_txcnt++;
339 	if (ok)
340 		amn->amn_success++;
341 	amn->amn_retrycnt += retries;
342 }
343 
344 /*
345  * Set tx count/retry statistics explicitly.  Intended for
346  * drivers that poll the device for statistics maintained
347  * in the device.
348  */
349 static void
350 amrr_tx_update(const struct ieee80211vap *vap, const struct ieee80211_node *ni,
351     void *arg1, void *arg2, void *arg3)
352 {
353 	struct ieee80211_amrr_node *amn = ni->ni_rctls;
354 	int txcnt = *(int *)arg1, success = *(int *)arg2, retrycnt = *(int *)arg3;
355 
356 	amn->amn_txcnt = txcnt;
357 	amn->amn_success = success;
358 	amn->amn_retrycnt = retrycnt;
359 }
360 
361 static int
362 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS)
363 {
364 	struct ieee80211vap *vap = arg1;
365 	struct ieee80211_amrr *amrr = vap->iv_rs;
366 	int msecs = ticks_to_msecs(amrr->amrr_interval);
367 	int error;
368 
369 	error = sysctl_handle_int(oidp, &msecs, 0, req);
370 	if (error || !req->newptr)
371 		return error;
372 	amrr_setinterval(vap, msecs);
373 	return 0;
374 }
375 
376 static void
377 amrr_sysctlattach(struct ieee80211vap *vap,
378     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
379 {
380 	struct ieee80211_amrr *amrr = vap->iv_rs;
381 
382 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
383 	    "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
384 	    0, amrr_sysctl_interval, "I", "amrr operation interval (ms)");
385 	/* XXX bounds check values */
386 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
387 	    "amrr_max_sucess_threshold", CTLFLAG_RW,
388 	    &amrr->amrr_max_success_threshold, 0, "");
389 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
390 	    "amrr_min_sucess_threshold", CTLFLAG_RW,
391 	    &amrr->amrr_min_success_threshold, 0, "");
392 }
393