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