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.10 2008/01/15 09:01:13 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>
61 #define DPRINTF(osc, lv, fmt, ...) do { \
62 if ((osc)->param->onoe_debug >= lv) \
63 kprintf(fmt, __VA_ARGS__); \
66 #define DPRINTF(osc, lv, fmt, ...)
70 struct ieee80211com *ic;
71 struct callout timer; /* periodic timer */
73 struct sysctl_ctx_list sysctl_ctx;
74 struct sysctl_oid *sysctl_oid;
76 struct ieee80211_onoe_param *param;
77 #define raise param->onoe_raise
78 #define raise_threshold param->onoe_raise_threshold
82 u_int od_tx_ok; /* tx ok pkt */
83 u_int od_tx_err; /* tx !ok pkt */
84 u_int od_tx_retr; /* tx retry count */
85 int od_tx_upper; /* tx upper rate req cnt */
89 * Default parameters for the rate control algorithm. These are
90 * all tunable with sysctls. The rate controller runs periodically
91 * (each ath_rateinterval ms) analyzing transmit statistics for each
92 * neighbor/station (when operating in station mode this is only the AP).
93 * If transmits look to be working well over a sampling period then
94 * it gives a "raise rate credit". If transmits look to not be working
95 * well than it deducts a credit. If the credits cross a threshold then
96 * the transmit rate is raised. Various error conditions force the
97 * the transmit rate to be dropped.
99 * The decision to issue/deduct a credit is based on the errors and
100 * retries accumulated over the sampling period. ath_rate_raise defines
101 * the percent of retransmits for which a credit is issued/deducted.
102 * ath_rate_raise_threshold defines the threshold on credits at which
103 * the transmit rate is increased.
105 * XXX this algorithm is flawed.
108 static void *onoe_attach(struct ieee80211com *);
109 static void onoe_detach(void *);
110 static void onoe_data_free(struct ieee80211_node *);
111 static void onoe_data_alloc(struct ieee80211_node *);
112 static void onoe_data_dup(const struct ieee80211_node *,
113 struct ieee80211_node *);
114 static void onoe_newstate(void *, enum ieee80211_state);
115 static void onoe_tx_complete(void *, struct ieee80211_node *, int,
116 const struct ieee80211_ratectl_res[],
118 static void onoe_newassoc(void *, struct ieee80211_node *, int);
119 static int onoe_findrate(void *, struct ieee80211_node *, int,
122 static void onoe_sysctl_attach(struct onoe_softc *);
123 static void onoe_update(struct onoe_softc *, struct ieee80211_node *, int);
124 static void onoe_start(struct onoe_softc *, struct ieee80211_node *);
125 static void onoe_tick(void *);
126 static void onoe_ratectl(void *, struct ieee80211_node *);
127 static void onoe_gather_stats(struct onoe_softc *, struct ieee80211_node *);
129 static const struct ieee80211_ratectl onoe = {
131 .rc_ratectl = IEEE80211_RATECTL_ONOE,
132 .rc_attach = onoe_attach,
133 .rc_detach = onoe_detach,
134 .rc_data_alloc = onoe_data_alloc,
135 .rc_data_free = onoe_data_free,
136 .rc_data_dup = onoe_data_dup,
137 .rc_newstate = onoe_newstate,
138 .rc_tx_complete = onoe_tx_complete,
139 .rc_newassoc = onoe_newassoc,
140 .rc_findrate = onoe_findrate
143 static u_int onoe_nrefs;
145 MALLOC_DEFINE(M_ONOE_RATECTL_DATA, "onoe_ratectl_data",
146 "onoe rate control data");
149 onoe_tx_complete(void *arg __unused, struct ieee80211_node *ni,
150 int frame_len __unused,
151 const struct ieee80211_ratectl_res res[] __unused,
152 int res_len __unused,
153 int data_retries, int rts_retries __unused, int is_fail)
155 struct onoe_data *od = ni->ni_rate_data;
165 od->od_tx_retr += data_retries;
169 onoe_newassoc(void *arg, struct ieee80211_node *ni, int is_new)
176 onoe_findrate(void *arg, struct ieee80211_node *ni,
177 int frame_len __unused, int rateidx[], int rateidx_len)
179 struct onoe_softc *osc = arg;
182 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) {
183 DPRINTF(osc, 5, "%s: number of rates changed, restart\n",
187 rate_idx = ni->ni_txrate;
189 for (i = 0; i < rateidx_len; ++i) {
192 rateidx[i] = rate_idx--;
195 rateidx[rateidx_len - 1] = 0;
200 onoe_update(struct onoe_softc *osc, struct ieee80211_node *ni, int nrate)
202 struct onoe_data *od = ni->ni_rate_data;
204 DPRINTF(osc, 1, "%s: set xmit rate for %6D to %dM\n", __func__,
206 ni->ni_rates.rs_nrates > 0 ?
207 IEEE80211_RS_RATE(&ni->ni_rates, nrate) / 2 : 0);
209 ni->ni_txrate = nrate;
222 * Set the starting transmit rate for a node.
225 onoe_start(struct onoe_softc *osc, struct ieee80211_node *ni)
227 #define RATE(_ix) IEEE80211_RS_RATE(&ni->ni_rates, (_ix))
228 struct ieee80211com *ic = osc->ic;
231 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
232 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
234 * For adhoc or ibss mode, start from the lowest rate.
236 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
237 ic->ic_opmode == IEEE80211_M_IBSS) {
238 onoe_update(osc, ni, 0);
243 * No fixed rate is requested. For 11b start with
244 * the highest negotiated rate; otherwise, for 11g
245 * and 11a, we start "in the middle" at 24Mb or 36Mb.
247 srate = ni->ni_rates.rs_nrates - 1;
248 if (ic->ic_curmode != IEEE80211_MODE_11B) {
250 * Scan the negotiated rate set to find the
253 /* NB: the rate set is assumed sorted */
254 for (; srate >= 0 && RATE(srate) > 72; srate--)
256 KASSERT(srate >= 0, ("bogus rate set"));
260 * A fixed rate is to be used; ic_fixed_rate is an
261 * index into the supported rate set. Convert this
262 * to the index into the negotiated rate set for
263 * the node. We know the rate is there because the
264 * rate set is checked when the station associates.
266 const struct ieee80211_rateset *rs =
267 &ic->ic_sup_rates[ic->ic_curmode];
268 int r = IEEE80211_RS_RATE(rs, ic->ic_fixed_rate);
270 /* NB: the rate set is assumed sorted */
271 srate = ni->ni_rates.rs_nrates - 1;
272 for (; srate >= 0 && RATE(srate) != r; srate--)
275 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
277 onoe_update(osc, ni, srate);
282 onoe_rate_cb(void *arg, struct ieee80211_node *ni)
284 onoe_update(arg, ni, 0);
288 onoe_newstate(void *arg, enum ieee80211_state state)
290 struct onoe_softc *osc = arg;
291 struct ieee80211com *ic = osc->ic;
292 struct ieee80211_node *ni;
294 if (state == IEEE80211_S_INIT) {
295 callout_stop(&osc->timer);
299 if (ic->ic_opmode == IEEE80211_M_STA) {
301 * Reset local xmit state; this is really only
302 * meaningful when operating in station mode.
305 if (state == IEEE80211_S_RUN)
308 onoe_update(osc, ni, 0);
311 * When operating as a station the node table holds
312 * the AP's that were discovered during scanning.
313 * For any other operating mode we want to reset the
314 * tx rate state of each node.
316 ieee80211_iterate_nodes(&ic->ic_sta, onoe_rate_cb, osc);
317 onoe_update(osc, ic->ic_bss, 0);
320 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
321 state == IEEE80211_S_RUN) {
325 * Start the background rate control thread if we
326 * are not configured to use a fixed xmit rate.
328 interval = osc->param->onoe_interval;
329 if (ic->ic_opmode == IEEE80211_M_STA)
331 callout_reset(&osc->timer, (interval * hz) / 1000,
337 onoe_gather_stats(struct onoe_softc *osc, struct ieee80211_node *ni)
339 struct onoe_data *od = ni->ni_rate_data;
340 struct ieee80211com *ic = osc->ic;
341 const struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
342 struct ieee80211_ratectl_stats stats;
344 st->rc_st_stats(ic, ni, &stats);
346 od->od_tx_ok += stats.stats_pkt_ok;
347 od->od_tx_err += stats.stats_pkt_err;
348 od->od_tx_retr += stats.stats_retries;
352 onoe_ratectl(void *arg, struct ieee80211_node *ni)
354 struct onoe_softc *osc = arg;
355 struct onoe_data *od = ni->ni_rate_data;
356 const struct ieee80211_ratectl_state *st = &osc->ic->ic_ratectl;
357 struct ieee80211_rateset *rs = &ni->ni_rates;
358 int dir = 0, nrate, enough;
361 /* We are no ready to go, set TX rate to lowest one */
366 if (st->rc_st_stats != NULL)
367 onoe_gather_stats(osc, ni);
371 * XXX: very primitive version.
373 enough = (od->od_tx_ok + od->od_tx_err >= 10);
375 /* no packet reached -> down */
376 if (od->od_tx_err > 0 && od->od_tx_ok == 0)
379 /* all packets needs retry in average -> down */
380 if (enough && od->od_tx_ok < od->od_tx_retr)
383 /* no error and less than rate_raise% of packets need retry -> up */
384 if (enough && od->od_tx_err == 0 &&
385 od->od_tx_retr < (od->od_tx_ok * osc->raise) / 100)
388 DPRINTF(osc, 10, "%6D: ok %d err %d retr %d upper %d dir %d\n",
390 od->od_tx_ok, od->od_tx_err, od->od_tx_retr,
391 od->od_tx_upper, dir);
393 nrate = ni->ni_txrate;
396 if (enough && od->od_tx_upper > 0)
405 /* raise rate if we hit rate_raise_threshold */
406 if (++od->od_tx_upper < osc->raise_threshold)
409 if (nrate + 1 < rs->rs_nrates)
414 if (nrate != ni->ni_txrate) {
415 DPRINTF(osc, 5, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
417 IEEE80211_RS_RATE(rs, ni->ni_txrate) / 2,
418 IEEE80211_RS_RATE(rs, nrate) / 2,
419 od->od_tx_ok, od->od_tx_err, od->od_tx_retr);
420 onoe_update(osc, ni, nrate);
422 od->od_tx_ok = od->od_tx_err = od->od_tx_retr = 0;
429 struct onoe_softc *osc = arg;
430 struct ieee80211com *ic = osc->ic;
431 struct ifnet *ifp = &ic->ic_if;
434 ifnet_serialize_all(ifp);
436 if (ifp->if_flags & IFF_RUNNING) {
437 if (ic->ic_opmode == IEEE80211_M_STA)
438 onoe_ratectl(osc, ic->ic_bss); /* NB: no reference */
440 ieee80211_iterate_nodes(&ic->ic_sta, onoe_ratectl, osc);
443 interval = osc->param->onoe_interval;
444 if (ic->ic_opmode == IEEE80211_M_STA)
446 callout_reset(&osc->timer, (interval * hz) / 1000, onoe_tick, osc);
448 ifnet_deserialize_all(ifp);
452 onoe_sysctl_attach(struct onoe_softc *osc)
454 sysctl_ctx_init(&osc->sysctl_ctx);
455 osc->sysctl_oid = SYSCTL_ADD_NODE(&osc->sysctl_ctx,
456 SYSCTL_CHILDREN(osc->ic->ic_sysctl_oid),
457 OID_AUTO, "onoe_ratectl", CTLFLAG_RD, 0, "");
458 if (osc->sysctl_oid == NULL) {
459 kprintf("wlan_ratectl_onoe: create sysctl tree failed\n");
463 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
464 OID_AUTO, "interval", CTLFLAG_RW,
465 &osc->param->onoe_interval, 0,
466 "rate control: operation interval (ms)");
468 /* XXX bounds check values */
469 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
470 OID_AUTO, "raise", CTLFLAG_RW,
471 &osc->param->onoe_raise, 0,
473 "retry threshold to credit rate raise (%%)");
475 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
476 OID_AUTO, "raise_threshold", CTLFLAG_RW,
477 &osc->param->onoe_raise_threshold, 0,
478 "rate control: # good periods before raising rate");
480 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
481 OID_AUTO, "debug", CTLFLAG_RW,
482 &osc->param->onoe_debug, 0,
483 "rate control: debug level");
487 onoe_attach(struct ieee80211com *ic)
489 struct onoe_softc *osc;
493 osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK | M_ZERO);
496 callout_init(&osc->timer);
497 osc->param = ic->ic_ratectl.rc_st_attach(ic, IEEE80211_RATECTL_ONOE);
499 onoe_sysctl_attach(osc);
501 onoe_newstate(osc, ic->ic_state);
507 _onoe_data_free(void *arg __unused, struct ieee80211_node *ni)
513 onoe_detach(void *arg)
515 struct onoe_softc *osc = arg;
516 struct ieee80211com *ic = osc->ic;
518 onoe_newstate(osc, IEEE80211_S_INIT);
520 ieee80211_iterate_nodes(&ic->ic_sta, _onoe_data_free, NULL);
521 ieee80211_iterate_nodes(&ic->ic_scan, _onoe_data_free, NULL);
523 if (osc->sysctl_oid != NULL)
524 sysctl_ctx_free(&osc->sysctl_ctx);
525 kfree(osc, M_DEVBUF);
531 onoe_data_free(struct ieee80211_node *ni)
533 if (ni->ni_rate_data != NULL) {
534 kfree(ni->ni_rate_data, M_ONOE_RATECTL_DATA);
535 ni->ni_rate_data = NULL;
540 onoe_data_alloc(struct ieee80211_node *ni)
542 KKASSERT(ni->ni_rate_data == NULL);
543 ni->ni_rate_data = kmalloc(sizeof(struct onoe_data),
544 M_ONOE_RATECTL_DATA, M_NOWAIT | M_ZERO);
548 onoe_data_dup(const struct ieee80211_node *oni, struct ieee80211_node *nni)
550 if (oni->ni_rate_data == NULL || nni->ni_rate_data == NULL)
553 bcopy(oni->ni_rate_data, nni->ni_rate_data, sizeof(struct onoe_data));
557 onoe_modevent(module_t mod, int type, void *unused)
561 ieee80211_ratectl_register(&onoe);
565 kprintf("wlan_ratectl_onoe: still in use "
566 "(%u dynamic refs)\n", onoe_nrefs);
569 ieee80211_ratectl_unregister(&onoe);
575 static moduledata_t onoe_mod = {
580 DECLARE_MODULE(wlan_ratectl_onoe, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
581 MODULE_VERSION(wlan_ratectl_onoe, 1);
582 MODULE_DEPEND(wlan_ratectl_onoe, wlan, 1, 1, 1);