2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
4 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
5 * Copyright (c) 2003, 2004 David Young. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. The name of David Young may not be used to endorse or promote
17 * products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
24 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_rssadapt.h>
50 #include <net80211/ieee80211_ratectl.h>
52 struct rssadapt_expavgctl {
53 /* RSS threshold decay. */
56 /* RSS threshold update. */
57 u_int rc_thresh_denom;
59 /* RSS average update. */
60 u_int rc_avgrssi_denom;
64 static struct rssadapt_expavgctl master_expavgctl = {
69 .rc_avgrssi_denom = 8,
76 #define interpolate(parm, old, new) ((parm##_old * (old) + \
77 (parm##_denom - parm##_old) * (new)) / \
80 static void rssadapt_setinterval(const struct ieee80211vap *, int);
81 static void rssadapt_init(struct ieee80211vap *);
82 static void rssadapt_deinit(struct ieee80211vap *);
83 static void rssadapt_updatestats(struct ieee80211_rssadapt_node *);
84 static void rssadapt_node_init(struct ieee80211_node *);
85 static void rssadapt_node_deinit(struct ieee80211_node *);
86 static int rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
87 static void rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
88 static void rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
90 static void rssadapt_tx_complete(const struct ieee80211_node *,
91 const struct ieee80211_ratectl_tx_status *);
92 static void rssadapt_sysctlattach(struct ieee80211vap *,
93 struct sysctl_ctx_list *, struct sysctl_oid *);
95 /* number of references from net80211 layer */
98 static const struct ieee80211_ratectl rssadapt = {
99 .ir_name = "rssadapt",
102 .ir_init = rssadapt_init,
103 .ir_deinit = rssadapt_deinit,
104 .ir_node_init = rssadapt_node_init,
105 .ir_node_deinit = rssadapt_node_deinit,
106 .ir_rate = rssadapt_rate,
107 .ir_tx_complete = rssadapt_tx_complete,
108 .ir_tx_update = NULL,
109 .ir_setinterval = rssadapt_setinterval,
111 IEEE80211_RATECTL_MODULE(rssadapt, 1);
112 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
115 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
117 struct ieee80211_rssadapt *rs = vap->iv_rs;
122 t = msecs_to_ticks(msecs);
123 rs->interval = (t < 1) ? 1 : t;
127 rssadapt_init(struct ieee80211vap *vap)
129 struct ieee80211_rssadapt *rs;
131 KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
134 nrefs++; /* XXX locking */
135 vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
136 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
138 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
142 rssadapt_setinterval(vap, 500 /* msecs */);
143 rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
147 rssadapt_deinit(struct ieee80211vap *vap)
149 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
150 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
151 nrefs--; /* XXX locking */
155 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
159 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
160 ra->ra_nfail = ra->ra_nok = 0;
163 * A node is eligible for its rate to be raised every 1/10 to 10
164 * seconds, more eligible in proportion to recent packet rates.
166 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
167 ra->ra_raise_interval = msecs_to_ticks(interval);
171 rssadapt_node_init(struct ieee80211_node *ni)
173 struct ieee80211_rssadapt_node *ra;
174 struct ieee80211vap *vap = ni->ni_vap;
175 struct ieee80211_rssadapt *rsa = vap->iv_rs;
176 const struct ieee80211_rateset *rs = &ni->ni_rates;
178 if (ni->ni_rctls == NULL) {
180 IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
181 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
183 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
191 rssadapt_updatestats(ra);
193 /* pick initial rate */
194 for (ra->ra_rix = rs->rs_nrates - 1;
195 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
198 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
199 ra->ra_ticks = ticks;
201 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
202 "RSSADAPT initial rate %d", ni->ni_txrate);
206 rssadapt_node_deinit(struct ieee80211_node *ni)
209 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
217 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
218 i < IEEE80211_RSSADAPT_BKTS;
219 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
228 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
230 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
232 const struct ieee80211_rateset *rs = &ra->ra_rates;
233 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
236 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
237 rssadapt_updatestats(ra);
238 ra->ra_ticks = ticks;
241 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
243 /* XXX this is average rssi, should be using last value */
244 rssi = ni->ni_ic->ic_node_getrssi(ni);
245 for (rix = rs->rs_nrates-1; rix >= 0; rix--)
246 if ((*thrs)[rix] < (rssi << 8))
248 if (rix != ra->ra_rix) {
249 /* update public rate */
250 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
253 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
254 "RSSADAPT new rate %d (pktlen %d rssi %d)",
255 ni->ni_txrate, pktlen, rssi);
261 * Adapt the data rate to suit the conditions. When a transmitted
262 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
263 * raise the RSS threshold for transmitting packets of similar length at
264 * the same data rate.
267 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
270 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
273 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
276 last_thr = (*thrs)[rix];
277 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
278 last_thr, (rssi << 8));
280 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
281 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
282 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
283 last_thr, (*thrs)[rix], rssi);
287 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
289 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
290 uint16_t newthr, oldthr;
293 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
296 if ((*thrs)[rix + 1] > (*thrs)[rix]) {
297 oldthr = (*thrs)[rix + 1];
298 if ((*thrs)[rix] == 0)
299 newthr = (rssi << 8);
301 newthr = (*thrs)[rix];
302 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
305 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
306 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
307 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
308 oldthr, newthr, rssi);
310 ra->ra_last_raise = ticks;
315 rssadapt_tx_complete(const struct ieee80211_node *ni,
316 const struct ieee80211_ratectl_tx_status *status)
318 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
322 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
323 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
326 pktlen = status->pktlen;
329 if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
331 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
332 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
333 rssadapt_raise_rate(ra, pktlen, rssi);
336 rssadapt_lower_rate(ra, pktlen, rssi);
341 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
343 struct ieee80211vap *vap = arg1;
344 struct ieee80211_rssadapt *rs = vap->iv_rs;
345 int msecs = ticks_to_msecs(rs->interval);
348 error = sysctl_handle_int(oidp, &msecs, 0, req);
349 if (error || !req->newptr)
351 rssadapt_setinterval(vap, msecs);
356 rssadapt_sysctlattach(struct ieee80211vap *vap,
357 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
360 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
361 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
362 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");