FreeBSD kernel ATH device code
amrr.c
Go to the documentation of this file.
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__FBSDID("$FreeBSD$");
43
44/*
45 * AMRR rate control. See:
46 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
47 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
48 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
49 */
50#include "opt_ath.h"
51#include "opt_inet.h"
52#include "opt_wlan.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/sysctl.h>
57#include <sys/kernel.h>
58#include <sys/lock.h>
59#include <sys/mutex.h>
60#include <sys/errno.h>
61
62#include <machine/bus.h>
63#include <machine/resource.h>
64#include <sys/bus.h>
65
66#include <sys/socket.h>
67
68#include <net/if.h>
69#include <net/if_media.h>
70#include <net/if_arp.h>
71
72#include <net80211/ieee80211_var.h>
73
74#include <net/bpf.h>
75
76#ifdef INET
77#include <netinet/in.h>
78#include <netinet/if_ether.h>
79#endif
80
81#include <dev/ath/if_athvar.h>
84
85static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
88
89static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
90 int rate);
91static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
92static void ath_rate_ctl(void *, struct ieee80211_node *);
93
94void
95ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
96{
97 /* NB: assumed to be zero'd by caller */
98}
99
100void
102{
103}
104
105void
106ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
107 int shortPreamble, size_t frameLen, int tid, int is_aggr,
108 u_int8_t *rix, int *try0, u_int8_t *txrate, int *maxdur,
109 int *maxpktlen)
110{
111 struct amrr_node *amn = ATH_NODE_AMRR(an);
112
113 *rix = amn->amn_tx_rix0;
114 *try0 = amn->amn_tx_try0;
115 if (shortPreamble)
116 *txrate = amn->amn_tx_rate0sp;
117 else
118 *txrate = amn->amn_tx_rate0;
119 maxdur = -1;
120 maxpktlen = -1;
121}
122
123/*
124 * Get the TX rates.
125 *
126 * The short preamble bits aren't set here; the caller should augment
127 * the returned rate with the relevant preamble rate flag.
128 */
129void
131 uint8_t rix0, int is_aggr, struct ath_rc_series *rc)
132{
133 struct amrr_node *amn = ATH_NODE_AMRR(an);
134
135 rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
136
137 rc[0].rix = amn->amn_tx_rate0;
138 rc[1].rix = amn->amn_tx_rate1;
139 rc[2].rix = amn->amn_tx_rate2;
140 rc[3].rix = amn->amn_tx_rate3;
141
142 rc[0].tries = amn->amn_tx_try0;
143 rc[1].tries = amn->amn_tx_try1;
144 rc[2].tries = amn->amn_tx_try2;
145 rc[3].tries = amn->amn_tx_try3;
146}
147
148void
150 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
151{
152 struct amrr_node *amn = ATH_NODE_AMRR(an);
153
155 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
156 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
157 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
158 );
159}
160
161void
163 const struct ath_rc_series *rc, const struct ath_tx_status *ts,
164 int frame_size, int rc_framesize, int nframes, int nbad)
165{
166 struct amrr_node *amn = ATH_NODE_AMRR(an);
167 int sr = ts->ts_shortretry;
168 int lr = ts->ts_longretry;
169 int retry_count = sr + lr;
170
171 amn->amn_tx_try0_cnt++;
172 if (retry_count == 1) {
173 amn->amn_tx_try1_cnt++;
174 } else if (retry_count == 2) {
175 amn->amn_tx_try1_cnt++;
176 amn->amn_tx_try2_cnt++;
177 } else if (retry_count == 3) {
178 amn->amn_tx_try1_cnt++;
179 amn->amn_tx_try2_cnt++;
180 amn->amn_tx_try3_cnt++;
181 } else if (retry_count > 3) {
182 amn->amn_tx_try1_cnt++;
183 amn->amn_tx_try2_cnt++;
184 amn->amn_tx_try3_cnt++;
185 amn->amn_tx_failure_cnt++;
186 }
187 if (amn->amn_interval != 0 &&
188 ticks - amn->amn_ticks > amn->amn_interval) {
189 ath_rate_ctl(sc, &an->an_node);
190 amn->amn_ticks = ticks;
191 }
192}
193
194void
195ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
196{
197 if (isnew)
198 ath_rate_ctl_start(sc, &an->an_node);
199}
200
201void
202ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
203{
204}
205
206static void
208{
209 amn->amn_tx_try0_cnt = 0;
210 amn->amn_tx_try1_cnt = 0;
211 amn->amn_tx_try2_cnt = 0;
212 amn->amn_tx_try3_cnt = 0;
213 amn->amn_tx_failure_cnt = 0;
214 amn->amn_success = 0;
215 amn->amn_recovery = 0;
217}
218
225static void
226ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
227{
228 struct ath_node *an = ATH_NODE(ni);
229 struct amrr_node *amn = ATH_NODE_AMRR(an);
230 struct ieee80211vap *vap = ni->ni_vap;
231 const HAL_RATE_TABLE *rt = sc->sc_currates;
232 u_int8_t rix;
233
234 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
235
236 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
237 "%s: set xmit rate to %dM", __func__,
238 ni->ni_rates.rs_nrates > 0 ?
239 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
240
241 amn->amn_rix = rate;
242 /*
243 * Before associating a node has no rate set setup
244 * so we can't calculate any transmit codes to use.
245 * This is ok since we should never be sending anything
246 * but management frames and those always go at the
247 * lowest hardware rate.
248 */
249 if (ni->ni_rates.rs_nrates > 0) {
250 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
251 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
252 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
253 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
255 if (sc->sc_mrretry) {
256 amn->amn_tx_try0 = 1;
257 amn->amn_tx_try1 = 1;
258 amn->amn_tx_try2 = 1;
259 amn->amn_tx_try3 = 1;
260 if (--rate >= 0) {
261 rix = sc->sc_rixmap[
262 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
263 amn->amn_tx_rate1 = rt->info[rix].rateCode;
264 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
265 rt->info[rix].shortPreamble;
266 } else {
267 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
268 }
269 if (--rate >= 0) {
270 rix = sc->sc_rixmap[
271 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
272 amn->amn_tx_rate2 = rt->info[rix].rateCode;
273 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
274 rt->info[rix].shortPreamble;
275 } else {
276 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
277 }
278 if (rate > 0) {
279 /* NB: only do this if we didn't already do it above */
280 amn->amn_tx_rate3 = rt->info[0].rateCode;
281 amn->amn_tx_rate3sp =
282 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
283 } else {
284 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
285 }
286 } else {
288 /* theorically, these statements are useless because
289 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
290 */
291 amn->amn_tx_try1 = 0;
292 amn->amn_tx_try2 = 0;
293 amn->amn_tx_try3 = 0;
294 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
295 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
296 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
297 }
298 }
299 node_reset(amn);
300
302 if (vap->iv_opmode == IEEE80211_M_STA)
303 amn->amn_interval /= 2;
304 amn->amn_interval = (amn->amn_interval * hz) / 1000;
305}
306
307/*
308 * Set the starting transmit rate for a node.
309 */
310static void
311ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
312{
313#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
314 const struct ieee80211_txparam *tp = ni->ni_txparms;
315 int srate;
316
317 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
318 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
319 /*
320 * No fixed rate is requested. For 11b start with
321 * the highest negotiated rate; otherwise, for 11g
322 * and 11a, we start "in the middle" at 24Mb or 36Mb.
323 */
324 srate = ni->ni_rates.rs_nrates - 1;
325 if (sc->sc_curmode != IEEE80211_MODE_11B) {
326 /*
327 * Scan the negotiated rate set to find the
328 * closest rate.
329 */
330 /* NB: the rate set is assumed sorted */
331 for (; srate >= 0 && RATE(srate) > 72; srate--)
332 ;
333 }
334 } else {
335 /*
336 * A fixed rate is to be used; ic_fixed_rate is the
337 * IEEE code for this rate (sans basic bit). Convert this
338 * to the index into the negotiated rate set for
339 * the node. We know the rate is there because the
340 * rate set is checked when the station associates.
341 */
342 /* NB: the rate set is assumed sorted */
343 srate = ni->ni_rates.rs_nrates - 1;
344 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
345 ;
346 }
347 /*
348 * The selected rate may not be available due to races
349 * and mode settings. Also orphaned nodes created in
350 * adhoc mode may not have any rate set so this lookup
351 * can fail. This is not fatal.
352 */
353 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
354#undef RATE
355}
356
357/*
358 * Examine and potentially adjust the transmit rate.
359 */
360static void
361ath_rate_ctl(void *arg, struct ieee80211_node *ni)
362{
363 struct ath_softc *sc = arg;
364 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
365 int rix;
366
367#define is_success(amn) \
368(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
369#define is_enough(amn) \
370(amn->amn_tx_try0_cnt > 10)
371#define is_failure(amn) \
372(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
373
374 rix = amn->amn_rix;
375
376 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
377 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
380 if (is_success (amn) && is_enough (amn)) {
381 amn->amn_success++;
382 if (amn->amn_success == amn->amn_success_threshold &&
383 rix + 1 < ni->ni_rates.rs_nrates) {
384 amn->amn_recovery = 1;
385 amn->amn_success = 0;
386 rix++;
387 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
388 "increase rate to %d", rix);
389 } else {
390 amn->amn_recovery = 0;
391 }
392 } else if (is_failure (amn)) {
393 amn->amn_success = 0;
394 if (rix > 0) {
395 if (amn->amn_recovery) {
396 /* recovery failure. */
397 amn->amn_success_threshold *= 2;
400 IEEE80211_NOTE(ni->ni_vap,
401 IEEE80211_MSG_RATECTL, ni,
402 "decrease rate recovery thr: %d",
404 } else {
405 /* simple failure. */
407 IEEE80211_NOTE(ni->ni_vap,
408 IEEE80211_MSG_RATECTL, ni,
409 "decrease rate normal thr: %d",
411 }
412 amn->amn_recovery = 0;
413 rix--;
414 } else {
415 amn->amn_recovery = 0;
416 }
417 }
418 if (is_enough (amn) || rix != amn->amn_rix) {
419 /* reset counters. */
420 amn->amn_tx_try0_cnt = 0;
421 amn->amn_tx_try1_cnt = 0;
422 amn->amn_tx_try2_cnt = 0;
423 amn->amn_tx_try3_cnt = 0;
424 amn->amn_tx_failure_cnt = 0;
425 }
426 if (rix != amn->amn_rix) {
427 ath_rate_update(sc, ni, rix);
428 }
429}
430
431static int
433 struct ath_rateioctl *re)
434{
435
436 return (EINVAL);
437}
438
439static void
441{
442 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
443 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
444
445 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
446 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
447 "rate control: operation interval (ms)");
448 /* XXX bounds check values */
449 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
450 "max_sucess_threshold", CTLFLAG_RW,
452 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
453 "min_sucess_threshold", CTLFLAG_RW,
455}
456
457struct ath_ratectrl *
459{
460 struct amrr_softc *asc;
461
462 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
463 if (asc == NULL)
464 return NULL;
465 asc->arc.arc_space = sizeof(struct amrr_node);
467
468 return &asc->arc;
469}
470
471void
473{
474 struct amrr_softc *asc = (struct amrr_softc *) arc;
475
476 free(asc, M_DEVBUF);
477}
static int ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an, struct ath_rateioctl *re)
Definition: amrr.c:432
void ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
Definition: amrr.c:95
static void node_reset(struct amrr_node *amn)
Definition: amrr.c:207
static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *)
Definition: amrr.c:311
static int ath_rate_min_success_threshold
Definition: amrr.c:87
static void ath_rate_sysctlattach(struct ath_softc *sc)
Definition: amrr.c:440
#define is_failure(amn)
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)
Definition: amrr.c:106
void ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
Definition: amrr.c:195
#define RATE(_ix)
#define is_enough(amn)
void ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an, uint8_t rix0, int is_aggr, struct ath_rc_series *rc)
Definition: amrr.c:130
void ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
Definition: amrr.c:202
void ath_rate_detach(struct ath_ratectrl *arc)
Definition: amrr.c:472
struct ath_ratectrl * ath_rate_attach(struct ath_softc *sc)
Definition: amrr.c:458
static void ath_rate_ctl(void *, struct ieee80211_node *)
Definition: amrr.c:361
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)
Definition: amrr.c:162
static int ath_rate_max_success_threshold
Definition: amrr.c:86
__FBSDID("$FreeBSD$")
#define is_success(amn)
static int ath_rateinterval
Definition: amrr.c:85
void ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, struct ath_desc *ds, int shortPreamble, u_int8_t rix)
Definition: amrr.c:149
void ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Definition: amrr.c:101
static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, int rate)
Definition: amrr.c:226
#define ATH_NODE_AMRR(an)
Definition: amrr.h:80
#define ATH_TXMAXTRY
Definition: if_athvar.h:78
#define ath_hal_setupxtxdesc(_ah, _ds, _txr1, _txtr1, _txr2, _txtr2, _txr3, _txtr3)
Definition: if_athvar.h:1396
#define ATH_NODE(ni)
Definition: if_athvar.h:210
uint8_t shortPreamble
Definition: ah.h:689
uint8_t rateCode
Definition: ah.h:688
struct HAL_RATE_TABLE::@3 info[64]
Definition: amrr.h:51
u_int8_t amn_tx_try0
Definition: amrr.h:75
u_int amn_tx_try2
Definition: amrr.h:77
u_int amn_success
Definition: amrr.h:63
u_int8_t amn_tx_rate0sp
Definition: amrr.h:71
u_int8_t amn_tx_rate3
Definition: amrr.h:70
u_int amn_recovery
Definition: amrr.h:64
u_int8_t amn_tx_rate2
Definition: amrr.h:69
u_int amn_tx_try1
Definition: amrr.h:76
u_int amn_tx_try2_cnt
Definition: amrr.h:58
u_int8_t amn_tx_rate1
Definition: amrr.h:68
u_int amn_tx_try3_cnt
Definition: amrr.h:59
u_int8_t amn_tx_rate1sp
Definition: amrr.h:72
int amn_ticks
Definition: amrr.h:53
u_int amn_tx_try0_cnt
Definition: amrr.h:56
u_int amn_tx_failure_cnt
Definition: amrr.h:60
u_int8_t amn_tx_rix0
Definition: amrr.h:66
int amn_rix
Definition: amrr.h:52
u_int amn_success_threshold
Definition: amrr.h:62
u_int amn_tx_try3
Definition: amrr.h:78
int amn_interval
Definition: amrr.h:54
u_int8_t amn_tx_rate3sp
Definition: amrr.h:74
u_int8_t amn_tx_rate2sp
Definition: amrr.h:73
u_int8_t amn_tx_rate0
Definition: amrr.h:67
u_int amn_tx_try1_cnt
Definition: amrr.h:57
struct ath_ratectrl arc
Definition: amrr.h:47
struct ieee80211_node an_node
Definition: if_athvar.h:193
size_t arc_space
Definition: if_athrate.h:74
uint16_t flags
Definition: if_athrate.h:97
uint8_t tries
Definition: if_athrate.h:95
uint8_t rix
Definition: if_athrate.h:93
const HAL_RATE_TABLE * sc_currates
Definition: if_athvar.h:678
enum ieee80211_phymode sc_curmode
Definition: if_athvar.h:679
u_int8_t sc_rixmap[256]
Definition: if_athvar.h:685
device_t sc_dev
Definition: if_athvar.h:598
struct ath_hal * sc_ah
Definition: if_athvar.h:612
uint32_t sc_mrretry
Definition: if_athvar.h:621
uint8_t ts_longretry
Definition: ah_desc.h:42
uint8_t ts_shortretry
Definition: ah_desc.h:41