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
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)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
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)99 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
100 {
101 }
102
103 void
ath_rate_findrate(struct ath_softc * sc,struct ath_node * an,int shortPreamble,size_t frameLen,int tid,int is_aggr,u_int8_t * rix,int * try0,u_int8_t * txrate,int * maxdur,int * maxpktlen)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
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,int is_aggr,struct ath_rc_series * rc)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
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)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
ath_rate_tx_complete(struct ath_softc * sc,struct ath_node * an,const struct ath_rc_series * rc,const struct ath_tx_status * ts,int frame_size,int rc_framesize,int nframes,int nbad)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
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)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
ath_rate_update_rx_rssi(struct ath_softc * sc,struct ath_node * an,int rssi)200 ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
201 {
202 }
203
204 static void
node_reset(struct amrr_node * amn)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
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)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 uint8_t dot11rate;
249
250 dot11rate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
251 amn->amn_tx_rix0 = sc->sc_rixmap[dot11rate];
252 ieee80211_node_set_txrate_dot11rate(ni, dot11rate);
253 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
254 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
255 rt->info[amn->amn_tx_rix0].shortPreamble;
256 if (sc->sc_mrretry) {
257 amn->amn_tx_try0 = 1;
258 amn->amn_tx_try1 = 1;
259 amn->amn_tx_try2 = 1;
260 amn->amn_tx_try3 = 1;
261 if (--rate >= 0) {
262 rix = sc->sc_rixmap[
263 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
264 amn->amn_tx_rate1 = rt->info[rix].rateCode;
265 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
266 rt->info[rix].shortPreamble;
267 } else {
268 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
269 }
270 if (--rate >= 0) {
271 rix = sc->sc_rixmap[
272 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
273 amn->amn_tx_rate2 = rt->info[rix].rateCode;
274 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
275 rt->info[rix].shortPreamble;
276 } else {
277 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
278 }
279 if (rate > 0) {
280 /* NB: only do this if we didn't already do it above */
281 amn->amn_tx_rate3 = rt->info[0].rateCode;
282 amn->amn_tx_rate3sp =
283 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
284 } else {
285 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
286 }
287 } else {
288 amn->amn_tx_try0 = ATH_TXMAXTRY;
289 /* theorically, these statements are useless because
290 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
291 */
292 amn->amn_tx_try1 = 0;
293 amn->amn_tx_try2 = 0;
294 amn->amn_tx_try3 = 0;
295 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
296 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
297 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
298 }
299 }
300 node_reset(amn);
301
302 amn->amn_interval = ath_rateinterval;
303 if (vap->iv_opmode == IEEE80211_M_STA)
304 amn->amn_interval /= 2;
305 amn->amn_interval = (amn->amn_interval * hz) / 1000;
306 }
307
308 /*
309 * Set the starting transmit rate for a node.
310 */
311 static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)312 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
313 {
314 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
315 const struct ieee80211_txparam *tp = ni->ni_txparms;
316 int srate;
317
318 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
319 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
320 /*
321 * No fixed rate is requested. For 11b start with
322 * the highest negotiated rate; otherwise, for 11g
323 * and 11a, we start "in the middle" at 24Mb or 36Mb.
324 */
325 srate = ni->ni_rates.rs_nrates - 1;
326 if (sc->sc_curmode != IEEE80211_MODE_11B) {
327 /*
328 * Scan the negotiated rate set to find the
329 * closest rate.
330 */
331 /* NB: the rate set is assumed sorted */
332 for (; srate >= 0 && RATE(srate) > 72; srate--)
333 ;
334 }
335 } else {
336 /*
337 * A fixed rate is to be used; ic_fixed_rate is the
338 * IEEE code for this rate (sans basic bit). Convert this
339 * to the index into the negotiated rate set for
340 * the node. We know the rate is there because the
341 * rate set is checked when the station associates.
342 */
343 /* NB: the rate set is assumed sorted */
344 srate = ni->ni_rates.rs_nrates - 1;
345 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
346 ;
347 }
348 /*
349 * The selected rate may not be available due to races
350 * and mode settings. Also orphaned nodes created in
351 * adhoc mode may not have any rate set so this lookup
352 * can fail. This is not fatal.
353 */
354 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
355 #undef RATE
356 }
357
358 /*
359 * Examine and potentially adjust the transmit rate.
360 */
361 static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)362 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
363 {
364 struct ath_softc *sc = arg;
365 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
366 int rix;
367
368 #define is_success(amn) \
369 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
370 #define is_enough(amn) \
371 (amn->amn_tx_try0_cnt > 10)
372 #define is_failure(amn) \
373 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
374
375 rix = amn->amn_rix;
376
377 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
378 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
379 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
380 amn->amn_tx_try3_cnt, amn->amn_success_threshold);
381 if (is_success (amn) && is_enough (amn)) {
382 amn->amn_success++;
383 if (amn->amn_success == amn->amn_success_threshold &&
384 rix + 1 < ni->ni_rates.rs_nrates) {
385 amn->amn_recovery = 1;
386 amn->amn_success = 0;
387 rix++;
388 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
389 "increase rate to %d", rix);
390 } else {
391 amn->amn_recovery = 0;
392 }
393 } else if (is_failure (amn)) {
394 amn->amn_success = 0;
395 if (rix > 0) {
396 if (amn->amn_recovery) {
397 /* recovery failure. */
398 amn->amn_success_threshold *= 2;
399 amn->amn_success_threshold = min (amn->amn_success_threshold,
400 (u_int)ath_rate_max_success_threshold);
401 IEEE80211_NOTE(ni->ni_vap,
402 IEEE80211_MSG_RATECTL, ni,
403 "decrease rate recovery thr: %d",
404 amn->amn_success_threshold);
405 } else {
406 /* simple failure. */
407 amn->amn_success_threshold = ath_rate_min_success_threshold;
408 IEEE80211_NOTE(ni->ni_vap,
409 IEEE80211_MSG_RATECTL, ni,
410 "decrease rate normal thr: %d",
411 amn->amn_success_threshold);
412 }
413 amn->amn_recovery = 0;
414 rix--;
415 } else {
416 amn->amn_recovery = 0;
417 }
418 }
419 if (is_enough (amn) || rix != amn->amn_rix) {
420 /* reset counters. */
421 amn->amn_tx_try0_cnt = 0;
422 amn->amn_tx_try1_cnt = 0;
423 amn->amn_tx_try2_cnt = 0;
424 amn->amn_tx_try3_cnt = 0;
425 amn->amn_tx_failure_cnt = 0;
426 }
427 if (rix != amn->amn_rix) {
428 ath_rate_update(sc, ni, rix);
429 }
430 }
431
432 static int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * re)433 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
434 struct ath_rateioctl *re)
435 {
436
437 return (EINVAL);
438 }
439
440 static void
ath_rate_sysctlattach(struct ath_softc * sc)441 ath_rate_sysctlattach(struct ath_softc *sc)
442 {
443 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
444 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
445
446 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
447 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
448 "rate control: operation interval (ms)");
449 /* XXX bounds check values */
450 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
451 "max_sucess_threshold", CTLFLAG_RW,
452 &ath_rate_max_success_threshold, 0, "");
453 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
454 "min_sucess_threshold", CTLFLAG_RW,
455 &ath_rate_min_success_threshold, 0, "");
456 }
457
458 struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)459 ath_rate_attach(struct ath_softc *sc)
460 {
461 struct amrr_softc *asc;
462
463 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
464 if (asc == NULL)
465 return NULL;
466 asc->arc.arc_space = sizeof(struct amrr_node);
467 ath_rate_sysctlattach(sc);
468
469 return &asc->arc;
470 }
471
472 void
ath_rate_detach(struct ath_ratectrl * arc)473 ath_rate_detach(struct ath_ratectrl *arc)
474 {
475 struct amrr_softc *asc = (struct amrr_softc *) arc;
476
477 free(asc, M_DEVBUF);
478 }
479