athrate-onoe.c revision 1.3 1 /*-
2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
15 * 3. Neither the names of the above-listed copyright holders nor the names
16 * of any contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * Alternatively, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") version 2 as published by the Free
21 * Software Foundation.
22 *
23 * NO WARRANTY
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGES.
35 */
36
37 #include <sys/cdefs.h>
38 #ifdef __FreeBSD__
39 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.4 2005/01/24 19:32:10 sam Exp $");
40 #endif
41 #ifdef __NetBSD__
42 __KERNEL_RCSID(0, "$NetBSD: athrate-onoe.c,v 1.3 2005/06/22 22:07:48 martin Exp $");
43 #endif
44
45 /*
46 * Atsushi Onoe's rate control algorithm.
47 */
48 #include "opt_inet.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/kernel.h>
54 #include <sys/lock.h>
55 #include <sys/errno.h>
56 #include <sys/device.h>
57
58 #include <machine/bus.h>
59
60 #include <sys/socket.h>
61
62 #include <net/if.h>
63 #include <net/if_media.h>
64 #include <net/if_arp.h>
65 #include <net/if_ether.h> /* XXX for ether_sprintf */
66
67 #include <net80211/ieee80211_var.h>
68
69 #include <net/bpf.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #endif
74
75 #include <dev/ic/ath_netbsd.h>
76 #include <dev/ic/athvar.h>
77 #include <dev/ic/athrate-onoe.h>
78 #include <contrib/dev/ic/athhal_desc.h>
79
80 #define ONOE_DEBUG
81 #ifdef ONOE_DEBUG
82 enum {
83 ATH_DEBUG_RATE = 0x00000010, /* rate control */
84 };
85 #define DPRINTF(sc, _fmt, ...) do { \
86 if (sc->sc_debug & ATH_DEBUG_RATE) \
87 printf(_fmt, __VA_ARGS__); \
88 } while (0)
89 #else
90 #define DPRINTF(sc, _fmt, ...)
91 #endif
92
93 /*
94 * Default parameters for the rate control algorithm. These are
95 * all tunable with sysctls. The rate controller runs periodically
96 * (each ath_rateinterval ms) analyzing transmit statistics for each
97 * neighbor/station (when operating in station mode this is only the AP).
98 * If transmits look to be working well over a sampling period then
99 * it gives a "raise rate credit". If transmits look to not be working
100 * well than it deducts a credit. If the credits cross a threshold then
101 * the transmit rate is raised. Various error conditions force the
102 * the transmit rate to be dropped.
103 *
104 * The decision to issue/deduct a credit is based on the errors and
105 * retries accumulated over the sampling period. ath_rate_raise defines
106 * the percent of retransmits for which a credit is issued/deducted.
107 * ath_rate_raise_threshold defines the threshold on credits at which
108 * the transmit rate is increased.
109 *
110 * XXX this algorithm is flawed.
111 */
112 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
113 static int ath_rate_raise = 10; /* add credit threshold */
114 static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */
115
116 static void ath_ratectl(void *);
117 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
118 int rate);
119 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
120 static void ath_rate_ctl(void *, struct ieee80211_node *);
121
122 void
123 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
124 {
125 /* NB: assumed to be zero'd by caller */
126 ath_rate_update(sc, &an->an_node, 0);
127 }
128
129 void
130 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
131 {
132 }
133
134 void
135 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
136 int shortPreamble, size_t frameLen,
137 u_int8_t *rix, int *try0, u_int8_t *txrate)
138 {
139 struct onoe_node *on = ATH_NODE_ONOE(an);
140
141 *rix = on->on_tx_rix0;
142 *try0 = on->on_tx_try0;
143 if (shortPreamble)
144 *txrate = on->on_tx_rate0sp;
145 else
146 *txrate = on->on_tx_rate0;
147 }
148
149 void
150 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
151 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
152 {
153 struct onoe_node *on = ATH_NODE_ONOE(an);
154
155 ath_hal_setupxtxdesc(sc->sc_ah, ds
156 , on->on_tx_rate1sp, 2 /* series 1 */
157 , on->on_tx_rate2sp, 2 /* series 2 */
158 , on->on_tx_rate3sp, 2 /* series 3 */
159 );
160 }
161
162 void
163 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
164 const struct ath_desc *ds, const struct ath_desc *ds0)
165 {
166 struct onoe_node *on = ATH_NODE_ONOE(an);
167
168 if (ds->ds_txstat.ts_status == 0)
169 on->on_tx_ok++;
170 else
171 on->on_tx_err++;
172 on->on_tx_retr += ds->ds_txstat.ts_shortretry
173 + ds->ds_txstat.ts_longretry;
174 }
175
176 void
177 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
178 {
179 if (isnew)
180 ath_rate_ctl_start(sc, &an->an_node);
181 }
182
183 static void
184 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
185 {
186 struct ath_node *an = ATH_NODE(ni);
187 struct onoe_node *on = ATH_NODE_ONOE(an);
188 const HAL_RATE_TABLE *rt = sc->sc_currates;
189 u_int8_t rix;
190
191 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
192
193 DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
194 __func__, ether_sprintf(ni->ni_macaddr),
195 ni->ni_rates.rs_nrates > 0 ?
196 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
197
198 ni->ni_txrate = rate;
199 /* XXX management/control frames always go at the lowest speed */
200 an->an_tx_mgtrate = rt->info[0].rateCode;
201 an->an_tx_mgtratesp = an->an_tx_mgtrate | rt->info[0].shortPreamble;
202 /*
203 * Before associating a node has no rate set setup
204 * so we can't calculate any transmit codes to use.
205 * This is ok since we should never be sending anything
206 * but management frames and those always go at the
207 * lowest hardware rate.
208 */
209 if (ni->ni_rates.rs_nrates == 0)
210 goto done;
211 on->on_tx_rix0 = sc->sc_rixmap[
212 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
213 on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
214
215 on->on_tx_rate0sp = on->on_tx_rate0 |
216 rt->info[on->on_tx_rix0].shortPreamble;
217 if (sc->sc_mrretry) {
218 /*
219 * Hardware supports multi-rate retry; setup two
220 * step-down retry rates and make the lowest rate
221 * be the ``last chance''. We use 4, 2, 2, 2 tries
222 * respectively (4 is set here, the rest are fixed
223 * in the xmit routine).
224 */
225 on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */
226 if (--rate >= 0) {
227 rix = sc->sc_rixmap[
228 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
229 on->on_tx_rate1 = rt->info[rix].rateCode;
230 on->on_tx_rate1sp = on->on_tx_rate1 |
231 rt->info[rix].shortPreamble;
232 } else {
233 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
234 }
235 if (--rate >= 0) {
236 rix = sc->sc_rixmap[
237 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
238 on->on_tx_rate2 = rt->info[rix].rateCode;
239 on->on_tx_rate2sp = on->on_tx_rate2 |
240 rt->info[rix].shortPreamble;
241 } else {
242 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
243 }
244 if (rate > 0) {
245 /* NB: only do this if we didn't already do it above */
246 on->on_tx_rate3 = rt->info[0].rateCode;
247 on->on_tx_rate3sp =
248 an->an_tx_mgtrate | rt->info[0].shortPreamble;
249 } else {
250 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
251 }
252 } else {
253 on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */
254 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
255 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
256 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
257 }
258 done:
259 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
260 }
261
262 /*
263 * Set the starting transmit rate for a node.
264 */
265 static void
266 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
267 {
268 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
269 struct ieee80211com *ic = &sc->sc_ic;
270 int srate;
271
272 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
273 if (ic->ic_fixed_rate == -1) {
274 /*
275 * No fixed rate is requested. For 11b start with
276 * the highest negotiated rate; otherwise, for 11g
277 * and 11a, we start "in the middle" at 24Mb or 36Mb.
278 */
279 srate = ni->ni_rates.rs_nrates - 1;
280 if (sc->sc_curmode != IEEE80211_MODE_11B) {
281 /*
282 * Scan the negotiated rate set to find the
283 * closest rate.
284 */
285 /* NB: the rate set is assumed sorted */
286 for (; srate >= 0 && RATE(srate) > 72; srate--)
287 ;
288 KASSERT(srate >= 0, ("bogus rate set"));
289 }
290 } else {
291 /*
292 * A fixed rate is to be used; ic_fixed_rate is an
293 * index into the supported rate set. Convert this
294 * to the index into the negotiated rate set for
295 * the node. We know the rate is there because the
296 * rate set is checked when the station associates.
297 */
298 const struct ieee80211_rateset *rs =
299 &ic->ic_sup_rates[ic->ic_curmode];
300 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
301 /* NB: the rate set is assumed sorted */
302 srate = ni->ni_rates.rs_nrates - 1;
303 for (; srate >= 0 && RATE(srate) != r; srate--)
304 ;
305 KASSERT(srate >= 0,
306 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
307 }
308 ath_rate_update(sc, ni, srate);
309 #undef RATE
310 }
311
312 static void
313 ath_rate_cb(void *arg, struct ieee80211_node *ni)
314 {
315 struct ath_softc *sc = arg;
316
317 ath_rate_update(sc, ni, 0);
318 }
319
320 /*
321 * Reset the rate control state for each 802.11 state transition.
322 */
323 void
324 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
325 {
326 struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
327 struct ieee80211com *ic = &sc->sc_ic;
328 struct ieee80211_node *ni;
329
330 if (state == IEEE80211_S_INIT) {
331 callout_stop(&osc->timer);
332 return;
333 }
334 if (ic->ic_opmode == IEEE80211_M_STA) {
335 /*
336 * Reset local xmit state; this is really only
337 * meaningful when operating in station mode.
338 */
339 ni = ic->ic_bss;
340 if (state == IEEE80211_S_RUN) {
341 ath_rate_ctl_start(sc, ni);
342 } else {
343 ath_rate_update(sc, ni, 0);
344 }
345 } else {
346 /*
347 * When operating as a station the node table holds
348 * the AP's that were discovered during scanning.
349 * For any other operating mode we want to reset the
350 * tx rate state of each node.
351 */
352 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
353 ath_rate_update(sc, ic->ic_bss, 0);
354 }
355 if (ic->ic_fixed_rate == -1 && state == IEEE80211_S_RUN) {
356 int interval;
357 /*
358 * Start the background rate control thread if we
359 * are not configured to use a fixed xmit rate.
360 */
361 interval = ath_rateinterval;
362 if (ic->ic_opmode == IEEE80211_M_STA)
363 interval /= 2;
364 callout_reset(&osc->timer, (interval * hz) / 1000,
365 ath_ratectl, &sc->sc_if);
366 }
367 }
368
369 /*
370 * Examine and potentially adjust the transmit rate.
371 */
372 static void
373 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
374 {
375 struct ath_softc *sc = arg;
376 struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
377 struct ieee80211_rateset *rs = &ni->ni_rates;
378 int dir = 0, nrate, enough;
379
380 /*
381 * Rate control
382 * XXX: very primitive version.
383 */
384 enough = (on->on_tx_ok + on->on_tx_err >= 10);
385
386 /* no packet reached -> down */
387 if (on->on_tx_err > 0 && on->on_tx_ok == 0)
388 dir = -1;
389
390 /* all packets needs retry in average -> down */
391 if (enough && on->on_tx_ok < on->on_tx_retr)
392 dir = -1;
393
394 /* no error and less than rate_raise% of packets need retry -> up */
395 if (enough && on->on_tx_err == 0 &&
396 on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
397 dir = 1;
398
399 DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
400 ether_sprintf(ni->ni_macaddr),
401 on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
402 on->on_tx_upper, dir);
403
404 nrate = ni->ni_txrate;
405 switch (dir) {
406 case 0:
407 if (enough && on->on_tx_upper > 0)
408 on->on_tx_upper--;
409 break;
410 case -1:
411 if (nrate > 0) {
412 nrate--;
413 sc->sc_stats.ast_rate_drop++;
414 }
415 on->on_tx_upper = 0;
416 break;
417 case 1:
418 /* raise rate if we hit rate_raise_threshold */
419 if (++on->on_tx_upper < ath_rate_raise_threshold)
420 break;
421 on->on_tx_upper = 0;
422 if (nrate + 1 < rs->rs_nrates) {
423 nrate++;
424 sc->sc_stats.ast_rate_raise++;
425 }
426 break;
427 }
428
429 if (nrate != ni->ni_txrate) {
430 DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
431 __func__,
432 (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
433 (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
434 on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
435 ath_rate_update(sc, ni, nrate);
436 } else if (enough)
437 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
438 }
439
440 static void
441 ath_ratectl(void *arg)
442 {
443 struct ifnet *ifp = arg;
444 struct ath_softc *sc = ifp->if_softc;
445 struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
446 struct ieee80211com *ic = &sc->sc_ic;
447 int interval;
448
449 if (ifp->if_flags & IFF_RUNNING) {
450 sc->sc_stats.ast_rate_calls++;
451
452 if (ic->ic_opmode == IEEE80211_M_STA)
453 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
454 else
455 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
456 }
457 interval = ath_rateinterval;
458 if (ic->ic_opmode == IEEE80211_M_STA)
459 interval /= 2;
460 callout_reset(&osc->timer, (interval * hz) / 1000,
461 ath_ratectl, &sc->sc_if);
462 }
463
464 static void
465 ath_rate_sysctlattach(struct ath_softc *sc)
466 {
467 struct sysctllog **clog = &sc->sc_sysctllog;
468 const struct sysctlnode *cnode, *rnode;
469
470 if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
471 return;
472
473 SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_interval",
474 "rate control: operation interval (ms)", rateinterval);
475 /* XXX bounds check values */
476 SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise",
477 "rate control: retry threshold to credit rate raise (%%)",
478 rate_raise);
479 SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise_threshold",
480 "rate control: # good periods before raising rate",
481 rate_raise_threshold);
482 }
483
484 struct ath_ratectrl *
485 ath_rate_attach(struct ath_softc *sc)
486 {
487 struct onoe_softc *osc;
488
489 osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
490 if (osc == NULL)
491 return NULL;
492 osc->arc.arc_space = sizeof(struct onoe_node);
493 ATH_CALLOUT_INIT(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
494 ath_rate_sysctlattach(sc);
495
496 return &osc->arc;
497 }
498
499 void
500 ath_rate_detach(struct ath_ratectrl *arc)
501 {
502 struct onoe_softc *osc = (struct onoe_softc *) arc;
503
504 callout_stop(&osc->timer);
505 free(osc, M_DEVBUF);
506 }
507