xref: /freebsd/sys/dev/ath/ath_rate/amrr/amrr.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 INRIA
5  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16  *    redistribution must be conditioned upon including a substantially
17  *    similar Disclaimer requirement for further binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
30  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
31  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
32  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
35  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37  * THE POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40 
41 #include <sys/cdefs.h>
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_ath.h"
49 #include "opt_inet.h"
50 #include "opt_wlan.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.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 <dev/ath/ath_hal/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, int tid, int is_aggr,
106 	u_int8_t *rix, int *try0, u_int8_t *txrate, int *maxdur,
107 	int *maxpktlen)
108 {
109 	struct amrr_node *amn = ATH_NODE_AMRR(an);
110 
111 	*rix = amn->amn_tx_rix0;
112 	*try0 = amn->amn_tx_try0;
113 	if (shortPreamble)
114 		*txrate = amn->amn_tx_rate0sp;
115 	else
116 		*txrate = amn->amn_tx_rate0;
117 	maxdur = -1;
118 	maxpktlen = -1;
119 }
120 
121 /*
122  * Get the TX rates.
123  *
124  * The short preamble bits aren't set here; the caller should augment
125  * the returned rate with the relevant preamble rate flag.
126  */
127 void
128 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
129     uint8_t rix0, int is_aggr, struct ath_rc_series *rc)
130 {
131 	struct amrr_node *amn = ATH_NODE_AMRR(an);
132 
133 	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
134 
135 	rc[0].rix = amn->amn_tx_rate0;
136 	rc[1].rix = amn->amn_tx_rate1;
137 	rc[2].rix = amn->amn_tx_rate2;
138 	rc[3].rix = amn->amn_tx_rate3;
139 
140 	rc[0].tries = amn->amn_tx_try0;
141 	rc[1].tries = amn->amn_tx_try1;
142 	rc[2].tries = amn->amn_tx_try2;
143 	rc[3].tries = amn->amn_tx_try3;
144 }
145 
146 void
147 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
148 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
149 {
150 	struct amrr_node *amn = ATH_NODE_AMRR(an);
151 
152 	ath_hal_setupxtxdesc(sc->sc_ah, ds
153 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
154 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
155 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
156 	);
157 }
158 
159 void
160 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
161 	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
162 	int frame_size, int rc_framesize, int nframes, int nbad)
163 {
164 	struct amrr_node *amn = ATH_NODE_AMRR(an);
165 	int sr = ts->ts_shortretry;
166 	int lr = ts->ts_longretry;
167 	int retry_count = sr + lr;
168 
169 	amn->amn_tx_try0_cnt++;
170 	if (retry_count == 1) {
171 		amn->amn_tx_try1_cnt++;
172 	} else if (retry_count == 2) {
173 		amn->amn_tx_try1_cnt++;
174 		amn->amn_tx_try2_cnt++;
175 	} else if (retry_count == 3) {
176 		amn->amn_tx_try1_cnt++;
177 		amn->amn_tx_try2_cnt++;
178 		amn->amn_tx_try3_cnt++;
179 	} else if (retry_count > 3) {
180 		amn->amn_tx_try1_cnt++;
181 		amn->amn_tx_try2_cnt++;
182 		amn->amn_tx_try3_cnt++;
183 		amn->amn_tx_failure_cnt++;
184 	}
185 	if (amn->amn_interval != 0 &&
186 	    ticks - amn->amn_ticks > amn->amn_interval) {
187 		ath_rate_ctl(sc, &an->an_node);
188 		amn->amn_ticks = ticks;
189 	}
190 }
191 
192 void
193 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
194 {
195 	if (isnew)
196 		ath_rate_ctl_start(sc, &an->an_node);
197 }
198 
199 void
200 ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
201 {
202 }
203 
204 static void
205 node_reset(struct amrr_node *amn)
206 {
207 	amn->amn_tx_try0_cnt = 0;
208 	amn->amn_tx_try1_cnt = 0;
209 	amn->amn_tx_try2_cnt = 0;
210 	amn->amn_tx_try3_cnt = 0;
211 	amn->amn_tx_failure_cnt = 0;
212   	amn->amn_success = 0;
213   	amn->amn_recovery = 0;
214   	amn->amn_success_threshold = ath_rate_min_success_threshold;
215 }
216 
217 /**
218  * The code below assumes that we are dealing with hardware multi rate retry
219  * I have no idea what will happen if you try to use this module with another
220  * type of hardware. Your machine might catch fire or it might work with
221  * horrible performance...
222  */
223 static void
224 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
225 {
226 	struct ath_node *an = ATH_NODE(ni);
227 	struct amrr_node *amn = ATH_NODE_AMRR(an);
228 	struct ieee80211vap *vap = ni->ni_vap;
229 	const HAL_RATE_TABLE *rt = sc->sc_currates;
230 	u_int8_t rix;
231 
232 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
233 
234 	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
235 	    "%s: set xmit rate to %dM", __func__,
236 	    ni->ni_rates.rs_nrates > 0 ?
237 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
238 
239 	amn->amn_rix = rate;
240 	/*
241 	 * Before associating a node has no rate set setup
242 	 * so we can't calculate any transmit codes to use.
243 	 * This is ok since we should never be sending anything
244 	 * but management frames and those always go at the
245 	 * lowest hardware rate.
246 	 */
247 	if (ni->ni_rates.rs_nrates > 0) {
248 		ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
249 		amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
250 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
251 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
252 			rt->info[amn->amn_tx_rix0].shortPreamble;
253 		if (sc->sc_mrretry) {
254 			amn->amn_tx_try0 = 1;
255 			amn->amn_tx_try1 = 1;
256 			amn->amn_tx_try2 = 1;
257 			amn->amn_tx_try3 = 1;
258 			if (--rate >= 0) {
259 				rix = sc->sc_rixmap[
260 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
261 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
262 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
263 					rt->info[rix].shortPreamble;
264 			} else {
265 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
266 			}
267 			if (--rate >= 0) {
268 				rix = sc->sc_rixmap[
269 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
270 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
271 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
272 					rt->info[rix].shortPreamble;
273 			} else {
274 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
275 			}
276 			if (rate > 0) {
277 				/* NB: only do this if we didn't already do it above */
278 				amn->amn_tx_rate3 = rt->info[0].rateCode;
279 				amn->amn_tx_rate3sp =
280 					amn->amn_tx_rate3 | rt->info[0].shortPreamble;
281 			} else {
282 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
283 			}
284 		} else {
285 			amn->amn_tx_try0 = ATH_TXMAXTRY;
286 			/* theorically, these statements are useless because
287 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
288 			 */
289 			amn->amn_tx_try1 = 0;
290 			amn->amn_tx_try2 = 0;
291 			amn->amn_tx_try3 = 0;
292 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
293 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
294 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
295 		}
296 	}
297 	node_reset(amn);
298 
299 	amn->amn_interval = ath_rateinterval;
300 	if (vap->iv_opmode == IEEE80211_M_STA)
301 		amn->amn_interval /= 2;
302 	amn->amn_interval = (amn->amn_interval * hz) / 1000;
303 }
304 
305 /*
306  * Set the starting transmit rate for a node.
307  */
308 static void
309 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
310 {
311 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
312 	const struct ieee80211_txparam *tp = ni->ni_txparms;
313 	int srate;
314 
315 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
316 	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
317 		/*
318 		 * No fixed rate is requested. For 11b start with
319 		 * the highest negotiated rate; otherwise, for 11g
320 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
321 		 */
322 		srate = ni->ni_rates.rs_nrates - 1;
323 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
324 			/*
325 			 * Scan the negotiated rate set to find the
326 			 * closest rate.
327 			 */
328 			/* NB: the rate set is assumed sorted */
329 			for (; srate >= 0 && RATE(srate) > 72; srate--)
330 				;
331 		}
332 	} else {
333 		/*
334 		 * A fixed rate is to be used; ic_fixed_rate is the
335 		 * IEEE code for this rate (sans basic bit).  Convert this
336 		 * to the index into the negotiated rate set for
337 		 * the node.  We know the rate is there because the
338 		 * rate set is checked when the station associates.
339 		 */
340 		/* NB: the rate set is assumed sorted */
341 		srate = ni->ni_rates.rs_nrates - 1;
342 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
343 			;
344 	}
345 	/*
346 	 * The selected rate may not be available due to races
347 	 * and mode settings.  Also orphaned nodes created in
348 	 * adhoc mode may not have any rate set so this lookup
349 	 * can fail.  This is not fatal.
350 	 */
351 	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
352 #undef RATE
353 }
354 
355 /*
356  * Examine and potentially adjust the transmit rate.
357  */
358 static void
359 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
360 {
361 	struct ath_softc *sc = arg;
362 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
363 	int rix;
364 
365 #define is_success(amn) \
366 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
367 #define is_enough(amn) \
368 (amn->amn_tx_try0_cnt > 10)
369 #define is_failure(amn) \
370 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
371 
372 	rix = amn->amn_rix;
373 
374   	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
375 	    "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
376 	    amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
377 	    amn->amn_tx_try3_cnt, amn->amn_success_threshold);
378   	if (is_success (amn) && is_enough (amn)) {
379 		amn->amn_success++;
380 		if (amn->amn_success == amn->amn_success_threshold &&
381 		    rix + 1 < ni->ni_rates.rs_nrates) {
382   			amn->amn_recovery = 1;
383   			amn->amn_success = 0;
384   			rix++;
385 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
386 			    "increase rate to %d", rix);
387   		} else {
388 			amn->amn_recovery = 0;
389 		}
390   	} else if (is_failure (amn)) {
391   		amn->amn_success = 0;
392 		if (rix > 0) {
393   			if (amn->amn_recovery) {
394   				/* recovery failure. */
395   				amn->amn_success_threshold *= 2;
396   				amn->amn_success_threshold = min (amn->amn_success_threshold,
397 								  (u_int)ath_rate_max_success_threshold);
398 				IEEE80211_NOTE(ni->ni_vap,
399 				    IEEE80211_MSG_RATECTL, ni,
400 				    "decrease rate recovery thr: %d",
401 				    amn->amn_success_threshold);
402   			} else {
403   				/* simple failure. */
404  				amn->amn_success_threshold = ath_rate_min_success_threshold;
405 				IEEE80211_NOTE(ni->ni_vap,
406 				    IEEE80211_MSG_RATECTL, ni,
407 				    "decrease rate normal thr: %d",
408 				    amn->amn_success_threshold);
409   			}
410 			amn->amn_recovery = 0;
411   			rix--;
412    		} else {
413 			amn->amn_recovery = 0;
414 		}
415    	}
416 	if (is_enough (amn) || rix != amn->amn_rix) {
417 		/* reset counters. */
418 		amn->amn_tx_try0_cnt = 0;
419 		amn->amn_tx_try1_cnt = 0;
420 		amn->amn_tx_try2_cnt = 0;
421 		amn->amn_tx_try3_cnt = 0;
422 		amn->amn_tx_failure_cnt = 0;
423 	}
424 	if (rix != amn->amn_rix) {
425 		ath_rate_update(sc, ni, rix);
426 	}
427 }
428 
429 static int
430 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
431     struct ath_rateioctl *re)
432 {
433 
434 	return (EINVAL);
435 }
436 
437 static void
438 ath_rate_sysctlattach(struct ath_softc *sc)
439 {
440 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
441 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
442 
443 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
444 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
445 		"rate control: operation interval (ms)");
446 	/* XXX bounds check values */
447 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
448 		"max_sucess_threshold", CTLFLAG_RW,
449 		&ath_rate_max_success_threshold, 0, "");
450 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
451 		"min_sucess_threshold", CTLFLAG_RW,
452 		&ath_rate_min_success_threshold, 0, "");
453 }
454 
455 struct ath_ratectrl *
456 ath_rate_attach(struct ath_softc *sc)
457 {
458 	struct amrr_softc *asc;
459 
460 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
461 	if (asc == NULL)
462 		return NULL;
463 	asc->arc.arc_space = sizeof(struct amrr_node);
464 	ath_rate_sysctlattach(sc);
465 
466 	return &asc->arc;
467 }
468 
469 void
470 ath_rate_detach(struct ath_ratectrl *arc)
471 {
472 	struct amrr_softc *asc = (struct amrr_softc *) arc;
473 
474 	free(asc, M_DEVBUF);
475 }
476