xref: /freebsd/sys/dev/ath/ath_rate/amrr/amrr.c (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
1 /*-
2  * Copyright (c) 2004 INRIA
3  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  * 3. Neither the names of the above-listed copyright holders nor the names
17  *    of any contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * NO WARRANTY
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
28  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
30  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGES.
36  *
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 /*
43  * AMRR rate control. See:
44  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
45  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
46  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
47  */
48 #include "opt_inet.h"
49 #include "opt_wlan.h"
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sysctl.h>
54 #include <sys/module.h>
55 #include <sys/kernel.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/errno.h>
59 
60 #include <machine/bus.h>
61 #include <machine/resource.h>
62 #include <sys/bus.h>
63 
64 #include <sys/socket.h>
65 
66 #include <net/if.h>
67 #include <net/if_media.h>
68 #include <net/if_arp.h>
69 
70 #include <net80211/ieee80211_var.h>
71 
72 #include <net/bpf.h>
73 
74 #ifdef INET
75 #include <netinet/in.h>
76 #include <netinet/if_ether.h>
77 #endif
78 
79 #include <dev/ath/if_athvar.h>
80 #include <dev/ath/ath_rate/amrr/amrr.h>
81 #include <contrib/dev/ath/ah_desc.h>
82 
83 static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
84 static	int ath_rate_max_success_threshold = 10;
85 static	int ath_rate_min_success_threshold = 1;
86 
87 static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
88 			int rate);
89 static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
90 static void	ath_rate_ctl(void *, struct ieee80211_node *);
91 
92 void
93 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
94 {
95 	/* NB: assumed to be zero'd by caller */
96 }
97 
98 void
99 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
100 {
101 }
102 
103 void
104 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
105 	int shortPreamble, size_t frameLen,
106 	u_int8_t *rix, int *try0, u_int8_t *txrate)
107 {
108 	struct amrr_node *amn = ATH_NODE_AMRR(an);
109 
110 	*rix = amn->amn_tx_rix0;
111 	*try0 = amn->amn_tx_try0;
112 	if (shortPreamble)
113 		*txrate = amn->amn_tx_rate0sp;
114 	else
115 		*txrate = amn->amn_tx_rate0;
116 }
117 
118 void
119 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
120 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
121 {
122 	struct amrr_node *amn = ATH_NODE_AMRR(an);
123 
124 	ath_hal_setupxtxdesc(sc->sc_ah, ds
125 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
126 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
127 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
128 	);
129 }
130 
131 void
132 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
133 	const struct ath_buf *bf)
134 {
135 	struct amrr_node *amn = ATH_NODE_AMRR(an);
136 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
137 	int sr = ts->ts_shortretry;
138 	int lr = ts->ts_longretry;
139 	int retry_count = sr + lr;
140 
141 	amn->amn_tx_try0_cnt++;
142 	if (retry_count == 1) {
143 		amn->amn_tx_try1_cnt++;
144 	} else if (retry_count == 2) {
145 		amn->amn_tx_try1_cnt++;
146 		amn->amn_tx_try2_cnt++;
147 	} else if (retry_count == 3) {
148 		amn->amn_tx_try1_cnt++;
149 		amn->amn_tx_try2_cnt++;
150 		amn->amn_tx_try3_cnt++;
151 	} else if (retry_count > 3) {
152 		amn->amn_tx_try1_cnt++;
153 		amn->amn_tx_try2_cnt++;
154 		amn->amn_tx_try3_cnt++;
155 		amn->amn_tx_failure_cnt++;
156 	}
157 	if (amn->amn_interval != 0 &&
158 	    ticks - amn->amn_ticks > amn->amn_interval) {
159 		ath_rate_ctl(sc, &an->an_node);
160 		amn->amn_ticks = ticks;
161 	}
162 }
163 
164 void
165 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
166 {
167 	if (isnew)
168 		ath_rate_ctl_start(sc, &an->an_node);
169 }
170 
171 static void
172 node_reset(struct amrr_node *amn)
173 {
174 	amn->amn_tx_try0_cnt = 0;
175 	amn->amn_tx_try1_cnt = 0;
176 	amn->amn_tx_try2_cnt = 0;
177 	amn->amn_tx_try3_cnt = 0;
178 	amn->amn_tx_failure_cnt = 0;
179   	amn->amn_success = 0;
180   	amn->amn_recovery = 0;
181   	amn->amn_success_threshold = ath_rate_min_success_threshold;
182 }
183 
184 
185 /**
186  * The code below assumes that we are dealing with hardware multi rate retry
187  * I have no idea what will happen if you try to use this module with another
188  * type of hardware. Your machine might catch fire or it might work with
189  * horrible performance...
190  */
191 static void
192 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
193 {
194 	struct ath_node *an = ATH_NODE(ni);
195 	struct amrr_node *amn = ATH_NODE_AMRR(an);
196 	struct ieee80211vap *vap = ni->ni_vap;
197 	const HAL_RATE_TABLE *rt = sc->sc_currates;
198 	u_int8_t rix;
199 
200 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
201 
202 	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
203 	    "%s: set xmit rate to %dM", __func__,
204 	    ni->ni_rates.rs_nrates > 0 ?
205 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
206 
207 	amn->amn_rix = rate;
208 	/*
209 	 * Before associating a node has no rate set setup
210 	 * so we can't calculate any transmit codes to use.
211 	 * This is ok since we should never be sending anything
212 	 * but management frames and those always go at the
213 	 * lowest hardware rate.
214 	 */
215 	if (ni->ni_rates.rs_nrates > 0) {
216 		ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
217 		amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
218 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
219 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
220 			rt->info[amn->amn_tx_rix0].shortPreamble;
221 		if (sc->sc_mrretry) {
222 			amn->amn_tx_try0 = 1;
223 			amn->amn_tx_try1 = 1;
224 			amn->amn_tx_try2 = 1;
225 			amn->amn_tx_try3 = 1;
226 			if (--rate >= 0) {
227 				rix = sc->sc_rixmap[
228 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
229 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
230 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
231 					rt->info[rix].shortPreamble;
232 			} else {
233 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
234 			}
235 			if (--rate >= 0) {
236 				rix = sc->sc_rixmap[
237 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
238 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
239 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
240 					rt->info[rix].shortPreamble;
241 			} else {
242 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
243 			}
244 			if (rate > 0) {
245 				/* NB: only do this if we didn't already do it above */
246 				amn->amn_tx_rate3 = rt->info[0].rateCode;
247 				amn->amn_tx_rate3sp =
248 					amn->amn_tx_rate3 | rt->info[0].shortPreamble;
249 			} else {
250 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
251 			}
252 		} else {
253 			amn->amn_tx_try0 = ATH_TXMAXTRY;
254 			/* theorically, these statements are useless because
255 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
256 			 */
257 			amn->amn_tx_try1 = 0;
258 			amn->amn_tx_try2 = 0;
259 			amn->amn_tx_try3 = 0;
260 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
261 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
262 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
263 		}
264 	}
265 	node_reset(amn);
266 
267 	amn->amn_interval = ath_rateinterval;
268 	if (vap->iv_opmode == IEEE80211_M_STA)
269 		amn->amn_interval /= 2;
270 	amn->amn_interval = (amn->amn_interval * hz) / 1000;
271 }
272 
273 /*
274  * Set the starting transmit rate for a node.
275  */
276 static void
277 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
278 {
279 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
280 	struct ath_node *an = ATH_NODE(ni);
281 	const struct ieee80211_txparam *tp = an->an_tp;
282 	int srate;
283 
284 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
285 	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
286 		/*
287 		 * No fixed rate is requested. For 11b start with
288 		 * the highest negotiated rate; otherwise, for 11g
289 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
290 		 */
291 		srate = ni->ni_rates.rs_nrates - 1;
292 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
293 			/*
294 			 * Scan the negotiated rate set to find the
295 			 * closest rate.
296 			 */
297 			/* NB: the rate set is assumed sorted */
298 			for (; srate >= 0 && RATE(srate) > 72; srate--)
299 				;
300 		}
301 	} else {
302 		/*
303 		 * A fixed rate is to be used; ic_fixed_rate is the
304 		 * IEEE code for this rate (sans basic bit).  Convert this
305 		 * to the index into the negotiated rate set for
306 		 * the node.  We know the rate is there because the
307 		 * rate set is checked when the station associates.
308 		 */
309 		/* NB: the rate set is assumed sorted */
310 		srate = ni->ni_rates.rs_nrates - 1;
311 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
312 			;
313 	}
314 	/*
315 	 * The selected rate may not be available due to races
316 	 * and mode settings.  Also orphaned nodes created in
317 	 * adhoc mode may not have any rate set so this lookup
318 	 * can fail.  This is not fatal.
319 	 */
320 	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
321 #undef RATE
322 }
323 
324 static void
325 ath_rate_cb(void *arg, struct ieee80211_node *ni)
326 {
327 	struct ath_softc *sc = arg;
328 
329 	ath_rate_update(sc, ni, 0);
330 }
331 
332 /*
333  * Reset the rate control state for each 802.11 state transition.
334  */
335 void
336 ath_rate_newstate(struct ieee80211vap *vap, enum ieee80211_state state)
337 {
338 	struct ieee80211com *ic = vap->iv_ic;
339 	struct ath_softc *sc = ic->ic_ifp->if_softc;
340 	struct ieee80211_node *ni;
341 
342 	if (state == IEEE80211_S_INIT)
343 		return;
344 	if (vap->iv_opmode == IEEE80211_M_STA) {
345 		/*
346 		 * Reset local xmit state; this is really only
347 		 * meaningful when operating in station mode.
348 		 */
349 		ni = vap->iv_bss;
350 		if (state == IEEE80211_S_RUN) {
351 			ath_rate_ctl_start(sc, ni);
352 		} else {
353 			ath_rate_update(sc, ni, 0);
354 		}
355 	} else {
356 		/*
357 		 * When operating as a station the node table holds
358 		 * the AP's that were discovered during scanning.
359 		 * For any other operating mode we want to reset the
360 		 * tx rate state of each node.
361 		 */
362 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
363 		ath_rate_update(sc, vap->iv_bss, 0);
364 	}
365 }
366 
367 /*
368  * Examine and potentially adjust the transmit rate.
369  */
370 static void
371 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
372 {
373 	struct ath_softc *sc = arg;
374 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
375 	int rix;
376 
377 #define is_success(amn) \
378 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
379 #define is_enough(amn) \
380 (amn->amn_tx_try0_cnt > 10)
381 #define is_failure(amn) \
382 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
383 
384 	rix = amn->amn_rix;
385 
386   	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
387 	    "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
388 	    amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
389 	    amn->amn_tx_try3_cnt, amn->amn_success_threshold);
390   	if (is_success (amn) && is_enough (amn)) {
391 		amn->amn_success++;
392 		if (amn->amn_success == amn->amn_success_threshold &&
393 		    rix + 1 < ni->ni_rates.rs_nrates) {
394   			amn->amn_recovery = 1;
395   			amn->amn_success = 0;
396   			rix++;
397 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
398 			    "increase rate to %d", rix);
399   		} else {
400 			amn->amn_recovery = 0;
401 		}
402   	} else if (is_failure (amn)) {
403   		amn->amn_success = 0;
404 		if (rix > 0) {
405   			if (amn->amn_recovery) {
406   				/* recovery failure. */
407   				amn->amn_success_threshold *= 2;
408   				amn->amn_success_threshold = min (amn->amn_success_threshold,
409 								  (u_int)ath_rate_max_success_threshold);
410 				IEEE80211_NOTE(ni->ni_vap,
411 				    IEEE80211_MSG_RATECTL, ni,
412 				    "decrease rate recovery thr: %d",
413 				    amn->amn_success_threshold);
414   			} else {
415   				/* simple failure. */
416  				amn->amn_success_threshold = ath_rate_min_success_threshold;
417 				IEEE80211_NOTE(ni->ni_vap,
418 				    IEEE80211_MSG_RATECTL, ni,
419 				    "decrease rate normal thr: %d",
420 				    amn->amn_success_threshold);
421   			}
422 			amn->amn_recovery = 0;
423   			rix--;
424    		} else {
425 			amn->amn_recovery = 0;
426 		}
427 
428    	}
429 	if (is_enough (amn) || rix != amn->amn_rix) {
430 		/* reset counters. */
431 		amn->amn_tx_try0_cnt = 0;
432 		amn->amn_tx_try1_cnt = 0;
433 		amn->amn_tx_try2_cnt = 0;
434 		amn->amn_tx_try3_cnt = 0;
435 		amn->amn_tx_failure_cnt = 0;
436 	}
437 	if (rix != amn->amn_rix) {
438 		ath_rate_update(sc, ni, rix);
439 	}
440 }
441 
442 static void
443 ath_rate_sysctlattach(struct ath_softc *sc)
444 {
445 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
446 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
447 
448 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
449 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
450 		"rate control: operation interval (ms)");
451 	/* XXX bounds check values */
452 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
453 		"max_sucess_threshold", CTLFLAG_RW,
454 		&ath_rate_max_success_threshold, 0, "");
455 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
456 		"min_sucess_threshold", CTLFLAG_RW,
457 		&ath_rate_min_success_threshold, 0, "");
458 }
459 
460 struct ath_ratectrl *
461 ath_rate_attach(struct ath_softc *sc)
462 {
463 	struct amrr_softc *asc;
464 
465 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
466 	if (asc == NULL)
467 		return NULL;
468 	asc->arc.arc_space = sizeof(struct amrr_node);
469 	ath_rate_sysctlattach(sc);
470 
471 	return &asc->arc;
472 }
473 
474 void
475 ath_rate_detach(struct ath_ratectrl *arc)
476 {
477 	struct amrr_softc *asc = (struct amrr_softc *) arc;
478 
479 	free(asc, M_DEVBUF);
480 }
481 
482 /*
483  * Module glue.
484  */
485 static int
486 amrr_modevent(module_t mod, int type, void *unused)
487 {
488 	switch (type) {
489 	case MOD_LOAD:
490 		if (bootverbose)
491 			printf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
492 		return 0;
493 	case MOD_UNLOAD:
494 		return 0;
495 	}
496 	return EINVAL;
497 }
498 
499 static moduledata_t amrr_mod = {
500 	"ath_rate",
501 	amrr_modevent,
502 	0
503 };
504 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
505 MODULE_VERSION(ath_rate, 1);
506 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
507