2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.
36 * $FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.8.2.3 2006/02/24 19:51:11 sam Exp $
37 * $DragonFly: src/sys/netproto/802_11/wlan_ratectl/onoe/ieee80211_ratectl_onoe.c,v 1.8 2007/03/19 13:38:43 sephe Exp $
41 * Atsushi Onoe's rate control algorithm.
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <sys/serialize.h>
52 #include <net/if_media.h>
53 #include <net/if_arp.h>
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
57 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_ratectl_onoe.h>
62 #define DPRINTF(osc, lv, fmt, ...) do { \
63 if ((osc)->debug >= lv) \
64 kprintf(fmt, __VA_ARGS__); \
67 #define DPRINTF(osc, lv, fmt, ...)
70 #define ONOE_REQUIRE_STATS (IEEE80211_RATECTL_STATS_PKT_OK | \
71 IEEE80211_RATECTL_STATS_PKT_ERR | \
72 IEEE80211_RATECTL_STATS_RETRIES)
73 #define ONOE_MEET_REQUIRE_STATS(stats_mask) \
74 (((stats_mask) & ONOE_REQUIRE_STATS) == ONOE_REQUIRE_STATS)
77 * Default parameters for the rate control algorithm. These are
78 * all tunable with sysctls. The rate controller runs periodically
79 * (each ath_rateinterval ms) analyzing transmit statistics for each
80 * neighbor/station (when operating in station mode this is only the AP).
81 * If transmits look to be working well over a sampling period then
82 * it gives a "raise rate credit". If transmits look to not be working
83 * well than it deducts a credit. If the credits cross a threshold then
84 * the transmit rate is raised. Various error conditions force the
85 * the transmit rate to be dropped.
87 * The decision to issue/deduct a credit is based on the errors and
88 * retries accumulated over the sampling period. ath_rate_raise defines
89 * the percent of retransmits for which a credit is issued/deducted.
90 * ath_rate_raise_threshold defines the threshold on credits at which
91 * the transmit rate is increased.
93 * XXX this algorithm is flawed.
96 static void *onoe_attach(struct ieee80211com *);
97 static void onoe_detach(void *);
98 static void onoe_data_free(struct ieee80211_node *);
99 static void onoe_data_alloc(struct ieee80211_node *);
100 static void onoe_data_dup(const struct ieee80211_node *,
101 struct ieee80211_node *);
102 static void onoe_newstate(void *, enum ieee80211_state);
103 static void onoe_tx_complete(void *, struct ieee80211_node *, int,
104 const struct ieee80211_ratectl_res[],
106 static void onoe_newassoc(void *, struct ieee80211_node *, int);
107 static int onoe_findrate(void *, struct ieee80211_node *, int,
110 static void onoe_sysctl_attach(struct onoe_softc *);
111 static void onoe_update(struct onoe_softc *, struct ieee80211_node *, int);
112 static void onoe_start(struct onoe_softc *, struct ieee80211_node *);
113 static void onoe_tick(void *);
114 static void onoe_ratectl(void *, struct ieee80211_node *);
115 static void onoe_gather_stats(struct onoe_softc *, struct ieee80211_node *);
117 static const struct ieee80211_ratectl onoe = {
119 .rc_ratectl = IEEE80211_RATECTL_ONOE,
120 .rc_attach = onoe_attach,
121 .rc_detach = onoe_detach,
122 .rc_data_alloc = onoe_data_alloc,
123 .rc_data_free = onoe_data_free,
124 .rc_data_dup = onoe_data_dup,
125 .rc_newstate = onoe_newstate,
126 .rc_tx_complete = onoe_tx_complete,
127 .rc_newassoc = onoe_newassoc,
128 .rc_findrate = onoe_findrate
131 static u_int onoe_nrefs;
133 MALLOC_DEFINE(M_ONOE_RATECTL_DATA, "onoe_ratectl_data",
134 "onoe rate control data");
137 onoe_tx_complete(void *arg __unused, struct ieee80211_node *ni,
138 int frame_len __unused,
139 const struct ieee80211_ratectl_res res[] __unused,
140 int res_len __unused,
141 int data_retries, int rts_retries __unused, int is_fail)
143 struct onoe_data *od = ni->ni_rate_data;
153 od->od_tx_retr += data_retries;
157 onoe_newassoc(void *arg, struct ieee80211_node *ni, int is_new)
164 onoe_findrate(void *arg, struct ieee80211_node *ni,
165 int frame_len __unused, int rateidx[], int rateidx_len)
167 struct onoe_softc *osc = arg;
170 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) {
171 DPRINTF(osc, 5, "%s: number of rates changed, restart\n",
175 rate_idx = ni->ni_txrate;
177 for (i = 0; i < rateidx_len; ++i) {
180 rateidx[i] = rate_idx--;
183 rateidx[rateidx_len - 1] = 0;
188 onoe_update(struct onoe_softc *osc, struct ieee80211_node *ni, int nrate)
190 struct onoe_data *od = ni->ni_rate_data;
192 DPRINTF(osc, 1, "%s: set xmit rate for %6D to %dM\n", __func__,
194 ni->ni_rates.rs_nrates > 0 ?
195 IEEE80211_RS_RATE(&ni->ni_rates, nrate) / 2 : 0);
197 ni->ni_txrate = nrate;
210 * Set the starting transmit rate for a node.
213 onoe_start(struct onoe_softc *osc, struct ieee80211_node *ni)
215 #define RATE(_ix) IEEE80211_RS_RATE(&ni->ni_rates, (_ix))
216 struct ieee80211com *ic = osc->ic;
219 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
220 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
222 * For adhoc or ibss mode, start from the lowest rate.
224 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
225 ic->ic_opmode == IEEE80211_M_IBSS) {
226 onoe_update(osc, ni, 0);
231 * No fixed rate is requested. For 11b start with
232 * the highest negotiated rate; otherwise, for 11g
233 * and 11a, we start "in the middle" at 24Mb or 36Mb.
235 srate = ni->ni_rates.rs_nrates - 1;
236 if (ic->ic_curmode != IEEE80211_MODE_11B) {
238 * Scan the negotiated rate set to find the
241 /* NB: the rate set is assumed sorted */
242 for (; srate >= 0 && RATE(srate) > 72; srate--)
244 KASSERT(srate >= 0, ("bogus rate set"));
248 * A fixed rate is to be used; ic_fixed_rate is an
249 * index into the supported rate set. Convert this
250 * to the index into the negotiated rate set for
251 * the node. We know the rate is there because the
252 * rate set is checked when the station associates.
254 const struct ieee80211_rateset *rs =
255 &ic->ic_sup_rates[ic->ic_curmode];
256 int r = IEEE80211_RS_RATE(rs, ic->ic_fixed_rate);
258 /* NB: the rate set is assumed sorted */
259 srate = ni->ni_rates.rs_nrates - 1;
260 for (; srate >= 0 && RATE(srate) != r; srate--)
263 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
265 onoe_update(osc, ni, srate);
270 onoe_rate_cb(void *arg, struct ieee80211_node *ni)
272 onoe_update(arg, ni, 0);
276 onoe_newstate(void *arg, enum ieee80211_state state)
278 struct onoe_softc *osc = arg;
279 struct ieee80211com *ic = osc->ic;
280 struct ieee80211_node *ni;
282 if (state == IEEE80211_S_INIT) {
283 callout_stop(&osc->timer);
287 if (ic->ic_opmode == IEEE80211_M_STA) {
289 * Reset local xmit state; this is really only
290 * meaningful when operating in station mode.
293 if (state == IEEE80211_S_RUN)
296 onoe_update(osc, ni, 0);
299 * When operating as a station the node table holds
300 * the AP's that were discovered during scanning.
301 * For any other operating mode we want to reset the
302 * tx rate state of each node.
304 ieee80211_iterate_nodes(&ic->ic_sta, onoe_rate_cb, osc);
305 onoe_update(osc, ic->ic_bss, 0);
308 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
309 state == IEEE80211_S_RUN) {
313 * Start the background rate control thread if we
314 * are not configured to use a fixed xmit rate.
316 interval = osc->interval;
317 if (ic->ic_opmode == IEEE80211_M_STA)
319 callout_reset(&osc->timer, (interval * hz) / 1000,
325 onoe_gather_stats(struct onoe_softc *osc, struct ieee80211_node *ni)
327 struct onoe_data *od = ni->ni_rate_data;
328 struct ieee80211com *ic = osc->ic;
329 const struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
330 struct ieee80211_ratectl_stats stats;
332 st->rc_st_stats(ic, ni, &stats);
334 od->od_tx_ok += stats.stats_pkt_ok;
335 od->od_tx_err += stats.stats_pkt_err;
336 od->od_tx_retr += stats.stats_retries;
340 onoe_ratectl(void *arg, struct ieee80211_node *ni)
342 struct onoe_softc *osc = arg;
343 struct onoe_data *od = ni->ni_rate_data;
344 const struct ieee80211_ratectl_state *st = &osc->ic->ic_ratectl;
345 struct ieee80211_rateset *rs = &ni->ni_rates;
346 int dir = 0, nrate, enough;
349 /* We are no ready to go, set TX rate to lowest one */
354 if (st->rc_st_stats != NULL) {
355 if (!ONOE_MEET_REQUIRE_STATS(st->rc_st_valid_stats))
357 onoe_gather_stats(osc, ni);
362 * XXX: very primitive version.
364 enough = (od->od_tx_ok + od->od_tx_err >= 10);
366 /* no packet reached -> down */
367 if (od->od_tx_err > 0 && od->od_tx_ok == 0)
370 /* all packets needs retry in average -> down */
371 if (enough && od->od_tx_ok < od->od_tx_retr)
374 /* no error and less than rate_raise% of packets need retry -> up */
375 if (enough && od->od_tx_err == 0 &&
376 od->od_tx_retr < (od->od_tx_ok * osc->raise) / 100)
379 DPRINTF(osc, 10, "%6D: ok %d err %d retr %d upper %d dir %d\n",
381 od->od_tx_ok, od->od_tx_err, od->od_tx_retr,
382 od->od_tx_upper, dir);
384 nrate = ni->ni_txrate;
387 if (enough && od->od_tx_upper > 0)
396 /* raise rate if we hit rate_raise_threshold */
397 if (++od->od_tx_upper < osc->raise_threshold)
400 if (nrate + 1 < rs->rs_nrates)
405 if (nrate != ni->ni_txrate) {
406 DPRINTF(osc, 5, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
408 IEEE80211_RS_RATE(rs, ni->ni_txrate) / 2,
409 IEEE80211_RS_RATE(rs, nrate) / 2,
410 od->od_tx_ok, od->od_tx_err, od->od_tx_retr);
411 onoe_update(osc, ni, nrate);
413 od->od_tx_ok = od->od_tx_err = od->od_tx_retr = 0;
420 struct onoe_softc *osc = arg;
421 struct ieee80211com *ic = osc->ic;
422 struct ifnet *ifp = &ic->ic_if;
425 lwkt_serialize_enter(ifp->if_serializer);
427 if (ifp->if_flags & IFF_RUNNING) {
428 if (ic->ic_opmode == IEEE80211_M_STA)
429 onoe_ratectl(osc, ic->ic_bss); /* NB: no reference */
431 ieee80211_iterate_nodes(&ic->ic_sta, onoe_ratectl, osc);
434 interval = osc->interval;
435 if (ic->ic_opmode == IEEE80211_M_STA)
437 callout_reset(&osc->timer, (interval * hz) / 1000, onoe_tick, osc);
439 lwkt_serialize_exit(ifp->if_serializer);
443 onoe_sysctl_attach(struct onoe_softc *osc)
445 struct ieee80211com *ic = osc->ic;
446 struct ieee80211_onoe_param *param;
448 param = ic->ic_ratectl.rc_st_param;
450 osc->interval = param->onoe_interval;
451 osc->raise = param->onoe_raise;
452 osc->raise_threshold = param->onoe_raise_threshold;
454 osc->interval = IEEE80211_ONOE_INTERVAL;
455 osc->raise = IEEE80211_ONOE_RAISE;
456 osc->raise_threshold = IEEE80211_ONOE_RAISE_THR;
460 sysctl_ctx_init(&osc->sysctl_ctx);
461 osc->sysctl_oid = SYSCTL_ADD_NODE(&osc->sysctl_ctx,
462 SYSCTL_CHILDREN(osc->ic->ic_sysctl_oid),
463 OID_AUTO, "onoe_ratectl", CTLFLAG_RD, 0, "");
464 if (osc->sysctl_oid == NULL) {
465 kprintf("wlan_ratectl_onoe: create sysctl tree failed\n");
469 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
470 OID_AUTO, "interval", CTLFLAG_RW, &osc->interval, 0,
471 "rate control: operation interval (ms)");
473 /* XXX bounds check values */
474 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
475 OID_AUTO, "raise", CTLFLAG_RW, &osc->raise, 0,
477 "retry threshold to credit rate raise (%%)");
479 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
480 OID_AUTO, "raise_threshold", CTLFLAG_RW,
481 &osc->raise_threshold, 0,
482 "rate control: # good periods before raising rate");
484 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
485 OID_AUTO, "debug", CTLFLAG_RW, &osc->debug, 0,
486 "rate control: debug level");
490 onoe_attach(struct ieee80211com *ic)
492 const struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
493 struct onoe_softc *osc;
495 if (st->rc_st_stats != NULL &&
496 !ONOE_MEET_REQUIRE_STATS(st->rc_st_valid_stats)) {
497 if_printf(&ic->ic_if, "WARNING: %s needs more average "
498 "statistics to work properly\n", onoe.rc_name);
503 osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK | M_ZERO);
506 callout_init(&osc->timer);
507 onoe_sysctl_attach(osc);
509 onoe_newstate(osc, ic->ic_state);
515 _onoe_data_free(void *arg __unused, struct ieee80211_node *ni)
521 onoe_detach(void *arg)
523 struct onoe_softc *osc = arg;
524 struct ieee80211com *ic = osc->ic;
526 onoe_newstate(osc, IEEE80211_S_INIT);
528 ieee80211_iterate_nodes(&ic->ic_sta, _onoe_data_free, NULL);
529 ieee80211_iterate_nodes(&ic->ic_scan, _onoe_data_free, NULL);
531 if (osc->sysctl_oid != NULL)
532 sysctl_ctx_free(&osc->sysctl_ctx);
533 kfree(osc, M_DEVBUF);
539 onoe_data_free(struct ieee80211_node *ni)
541 if (ni->ni_rate_data != NULL) {
542 kfree(ni->ni_rate_data, M_ONOE_RATECTL_DATA);
543 ni->ni_rate_data = NULL;
548 onoe_data_alloc(struct ieee80211_node *ni)
550 KKASSERT(ni->ni_rate_data == NULL);
551 ni->ni_rate_data = kmalloc(sizeof(struct onoe_data),
552 M_ONOE_RATECTL_DATA, M_NOWAIT | M_ZERO);
556 onoe_data_dup(const struct ieee80211_node *oni, struct ieee80211_node *nni)
558 if (oni->ni_rate_data == NULL || nni->ni_rate_data == NULL)
561 bcopy(oni->ni_rate_data, nni->ni_rate_data, sizeof(struct onoe_data));
565 onoe_modevent(module_t mod, int type, void *unused)
569 ieee80211_ratectl_register(&onoe);
573 kprintf("wlan_ratectl_onoe: still in use "
574 "(%u dynamic refs)\n", onoe_nrefs);
577 ieee80211_ratectl_unregister(&onoe);
583 static moduledata_t onoe_mod = {
588 DECLARE_MODULE(wlan_ratectl_onoe, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
589 MODULE_VERSION(wlan_ratectl_onoe, 1);
590 MODULE_DEPEND(wlan_ratectl_onoe, wlan, 1, 1, 1);