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.3 2006/09/05 03:48:13 dillon 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_ratectl_onoe.h>
61 #define DPRINTF(osc, lv, fmt, ...) do { \
62 if ((osc)->debug >= lv) \
63 printf(fmt, __VA_ARGS__); \
66 #define DPRINTF(osc, lv, fmt, ...)
70 * Default parameters for the rate control algorithm. These are
71 * all tunable with sysctls. The rate controller runs periodically
72 * (each ath_rateinterval ms) analyzing transmit statistics for each
73 * neighbor/station (when operating in station mode this is only the AP).
74 * If transmits look to be working well over a sampling period then
75 * it gives a "raise rate credit". If transmits look to not be working
76 * well than it deducts a credit. If the credits cross a threshold then
77 * the transmit rate is raised. Various error conditions force the
78 * the transmit rate to be dropped.
80 * The decision to issue/deduct a credit is based on the errors and
81 * retries accumulated over the sampling period. ath_rate_raise defines
82 * the percent of retransmits for which a credit is issued/deducted.
83 * ath_rate_raise_threshold defines the threshold on credits at which
84 * the transmit rate is increased.
86 * XXX this algorithm is flawed.
89 static void *onoe_attach(struct ieee80211com *);
90 static void onoe_detach(void *);
91 static void onoe_data_free(struct ieee80211_node *);
92 static void onoe_data_alloc(struct ieee80211_node *);
93 static void onoe_data_dup(const struct ieee80211_node *,
94 struct ieee80211_node *);
95 static void onoe_newstate(void *, enum ieee80211_state);
96 static void onoe_tx_complete(void *, struct ieee80211_node *, int,
97 const struct ieee80211_ratectl_res[],
99 static void onoe_newassoc(void *, struct ieee80211_node *, int);
100 static int onoe_findrate(void *, struct ieee80211_node *, int,
103 static void onoe_sysctl_attach(struct onoe_softc *);
104 static void onoe_update(struct onoe_softc *, struct ieee80211_node *, int);
105 static void onoe_start(struct onoe_softc *, struct ieee80211_node *);
106 static void onoe_tick(void *);
107 static void onoe_ratectl(void *, struct ieee80211_node *);
109 static const struct ieee80211_ratectl onoe = {
111 .rc_ratectl = IEEE80211_RATECTL_ONOE,
112 .rc_attach = onoe_attach,
113 .rc_detach = onoe_detach,
114 .rc_data_alloc = onoe_data_alloc,
115 .rc_data_free = onoe_data_free,
116 .rc_data_dup = onoe_data_dup,
117 .rc_newstate = onoe_newstate,
118 .rc_tx_complete = onoe_tx_complete,
119 .rc_newassoc = onoe_newassoc,
120 .rc_findrate = onoe_findrate
123 static u_int onoe_nrefs;
125 MALLOC_DEFINE(M_ONOE_RATECTL_DATA, "onoe_ratectl_data",
126 "onoe rate control data");
129 onoe_tx_complete(void *arg __unused, struct ieee80211_node *ni,
130 int frame_len __unused,
131 const struct ieee80211_ratectl_res res[] __unused,
132 int res_len __unused,
133 int short_retries, int long_retries, int is_fail)
135 struct onoe_data *od = ni->ni_rate_data;
145 od->od_tx_retr += long_retries + short_retries;
149 onoe_newassoc(void *arg, struct ieee80211_node *ni, int is_new)
156 onoe_findrate(void *arg __unused, struct ieee80211_node *ni,
157 int frame_len __unused, int rateidx[], int rateidx_len)
159 int i, rate_idx = ni->ni_txrate;
161 for (i = 0; i < rateidx_len; ++i) {
164 rateidx[i] = rate_idx--;
167 rateidx[rateidx_len - 1] = 0;
172 onoe_update(struct onoe_softc *osc, struct ieee80211_node *ni, int nrate)
174 struct onoe_data *od = ni->ni_rate_data;
176 DPRINTF(osc, 1, "%s: set xmit rate for %6D to %dM\n", __func__,
178 ni->ni_rates.rs_nrates > 0 ?
179 IEEE80211_RS_RATE(&ni->ni_rates, nrate) / 2 : 0);
181 ni->ni_txrate = nrate;
194 * Set the starting transmit rate for a node.
197 onoe_start(struct onoe_softc *osc, struct ieee80211_node *ni)
199 #define RATE(_ix) IEEE80211_RS_RATE(&ni->ni_rates, (_ix))
200 struct ieee80211com *ic = osc->ic;
203 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
204 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
206 * For adhoc or ibss mode, start from the lowest rate.
208 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
209 ic->ic_opmode == IEEE80211_M_IBSS) {
210 onoe_update(osc, ni, 0);
215 * No fixed rate is requested. For 11b start with
216 * the highest negotiated rate; otherwise, for 11g
217 * and 11a, we start "in the middle" at 24Mb or 36Mb.
219 srate = ni->ni_rates.rs_nrates - 1;
220 if (ic->ic_curmode != IEEE80211_MODE_11B) {
222 * Scan the negotiated rate set to find the
225 /* NB: the rate set is assumed sorted */
226 for (; srate >= 0 && RATE(srate) > 72; srate--)
228 KASSERT(srate >= 0, ("bogus rate set"));
232 * A fixed rate is to be used; ic_fixed_rate is an
233 * index into the supported rate set. Convert this
234 * to the index into the negotiated rate set for
235 * the node. We know the rate is there because the
236 * rate set is checked when the station associates.
238 const struct ieee80211_rateset *rs =
239 &ic->ic_sup_rates[ic->ic_curmode];
240 int r = IEEE80211_RS_RATE(rs, ic->ic_fixed_rate);
242 /* NB: the rate set is assumed sorted */
243 srate = ni->ni_rates.rs_nrates - 1;
244 for (; srate >= 0 && RATE(srate) != r; srate--)
247 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
249 onoe_update(osc, ni, srate);
254 onoe_rate_cb(void *arg, struct ieee80211_node *ni)
256 onoe_update(arg, ni, 0);
260 onoe_newstate(void *arg, enum ieee80211_state state)
262 struct onoe_softc *osc = arg;
263 struct ieee80211com *ic = osc->ic;
264 struct ieee80211_node *ni;
266 if (state == IEEE80211_S_INIT) {
267 callout_stop(&osc->timer);
271 if (ic->ic_opmode == IEEE80211_M_STA) {
273 * Reset local xmit state; this is really only
274 * meaningful when operating in station mode.
277 if (state == IEEE80211_S_RUN)
280 onoe_update(osc, ni, 0);
283 * When operating as a station the node table holds
284 * the AP's that were discovered during scanning.
285 * For any other operating mode we want to reset the
286 * tx rate state of each node.
288 ieee80211_iterate_nodes(&ic->ic_sta, onoe_rate_cb, osc);
289 onoe_update(osc, ic->ic_bss, 0);
292 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
293 state == IEEE80211_S_RUN) {
297 * Start the background rate control thread if we
298 * are not configured to use a fixed xmit rate.
300 interval = osc->interval;
301 if (ic->ic_opmode == IEEE80211_M_STA)
303 callout_reset(&osc->timer, (interval * hz) / 1000,
309 onoe_ratectl(void *arg, struct ieee80211_node *ni)
311 struct onoe_softc *osc = arg;
312 struct onoe_data *od = ni->ni_rate_data;
313 struct ieee80211_rateset *rs = &ni->ni_rates;
314 int dir = 0, nrate, enough;
317 /* We are no ready to go, set TX rate to lowest one */
324 * XXX: very primitive version.
326 enough = (od->od_tx_ok + od->od_tx_err >= 10);
328 /* no packet reached -> down */
329 if (od->od_tx_err > 0 && od->od_tx_ok == 0)
332 /* all packets needs retry in average -> down */
333 if (enough && od->od_tx_ok < od->od_tx_retr)
336 /* no error and less than rate_raise% of packets need retry -> up */
337 if (enough && od->od_tx_err == 0 &&
338 od->od_tx_retr < (od->od_tx_ok * osc->raise) / 100)
341 DPRINTF(osc, 10, "%6D: ok %d err %d retr %d upper %d dir %d\n",
343 od->od_tx_ok, od->od_tx_err, od->od_tx_retr,
344 od->od_tx_upper, dir);
346 nrate = ni->ni_txrate;
349 if (enough && od->od_tx_upper > 0)
358 /* raise rate if we hit rate_raise_threshold */
359 if (++od->od_tx_upper < osc->raise_threshold)
362 if (nrate + 1 < rs->rs_nrates)
367 if (nrate != ni->ni_txrate) {
368 DPRINTF(osc, 5, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
370 IEEE80211_RS_RATE(rs, ni->ni_txrate) / 2,
371 IEEE80211_RS_RATE(rs, nrate) / 2,
372 od->od_tx_ok, od->od_tx_err, od->od_tx_retr);
373 onoe_update(osc, ni, nrate);
375 od->od_tx_ok = od->od_tx_err = od->od_tx_retr = 0;
382 struct onoe_softc *osc = arg;
383 struct ieee80211com *ic = osc->ic;
384 struct ifnet *ifp = &ic->ic_if;
387 lwkt_serialize_enter(ifp->if_serializer);
389 if (ifp->if_flags & IFF_RUNNING) {
390 if (ic->ic_opmode == IEEE80211_M_STA)
391 onoe_ratectl(osc, ic->ic_bss); /* NB: no reference */
393 ieee80211_iterate_nodes(&ic->ic_sta, onoe_ratectl, osc);
396 interval = osc->interval;
397 if (ic->ic_opmode == IEEE80211_M_STA)
399 callout_reset(&osc->timer, (interval * hz) / 1000, onoe_tick, osc);
401 lwkt_serialize_exit(ifp->if_serializer);
405 onoe_sysctl_attach(struct onoe_softc *osc)
407 osc->interval = 1000;
409 osc->raise_threshold = 10;
412 sysctl_ctx_init(&osc->sysctl_ctx);
413 osc->sysctl_oid = SYSCTL_ADD_NODE(&osc->sysctl_ctx,
414 SYSCTL_CHILDREN(osc->ic->ic_sysctl_oid),
415 OID_AUTO, "onoe_ratectl", CTLFLAG_RD, 0, "");
416 if (osc->sysctl_oid == NULL) {
417 printf("wlan_ratectl_onoe: create sysctl tree failed\n");
421 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
422 OID_AUTO, "interval", CTLFLAG_RW, &osc->interval, 0,
423 "rate control: operation interval (ms)");
425 /* XXX bounds check values */
426 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
427 OID_AUTO, "raise", CTLFLAG_RW, &osc->raise, 0,
429 "retry threshold to credit rate raise (%%)");
431 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
432 OID_AUTO, "raise_threshold", CTLFLAG_RW,
433 &osc->raise_threshold, 0,
434 "rate control: # good periods before raising rate");
436 SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
437 OID_AUTO, "debug", CTLFLAG_RW, &osc->debug, 0,
438 "rate control: debug level");
442 onoe_attach(struct ieee80211com *ic)
444 struct onoe_softc *osc;
448 osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK | M_ZERO);
451 callout_init(&osc->timer);
452 onoe_sysctl_attach(osc);
454 onoe_newstate(osc, ic->ic_state);
460 _onoe_data_free(void *arg __unused, struct ieee80211_node *ni)
466 onoe_detach(void *arg)
468 struct onoe_softc *osc = arg;
469 struct ieee80211com *ic = osc->ic;
471 onoe_newstate(osc, IEEE80211_S_INIT);
473 ieee80211_iterate_nodes(&ic->ic_sta, _onoe_data_free, NULL);
474 ieee80211_iterate_nodes(&ic->ic_scan, _onoe_data_free, NULL);
476 if (osc->sysctl_oid != NULL)
477 sysctl_ctx_free(&osc->sysctl_ctx);
478 kfree(osc, M_DEVBUF);
484 onoe_data_free(struct ieee80211_node *ni)
486 if (ni->ni_rate_data != NULL) {
487 kfree(ni->ni_rate_data, M_ONOE_RATECTL_DATA);
488 ni->ni_rate_data = NULL;
493 onoe_data_alloc(struct ieee80211_node *ni)
495 KKASSERT(ni->ni_rate_data == NULL);
496 ni->ni_rate_data = kmalloc(sizeof(struct onoe_data),
497 M_ONOE_RATECTL_DATA, M_NOWAIT | M_ZERO);
501 onoe_data_dup(const struct ieee80211_node *oni, struct ieee80211_node *nni)
503 if (oni->ni_rate_data == NULL || nni->ni_rate_data == NULL)
506 bcopy(oni->ni_rate_data, nni->ni_rate_data, sizeof(struct onoe_data));
510 onoe_modevent(module_t mod, int type, void *unused)
514 ieee80211_ratectl_register(&onoe);
518 printf("wlan_ratectl_onoe: still in use "
519 "(%u dynamic refs)\n", onoe_nrefs);
522 ieee80211_ratectl_unregister(&onoe);
528 static moduledata_t onoe_mod = {
533 DECLARE_MODULE(wlan_ratectl_onoe, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
534 MODULE_VERSION(wlan_ratectl_onoe, 1);
535 MODULE_DEPEND(wlan_ratectl_onoe, wlan, 1, 1, 1);