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 vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
135 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
137 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
141 rssadapt_setinterval(vap, 500 /* msecs */);
142 rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
146 rssadapt_deinit(struct ieee80211vap *vap)
148 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
152 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
156 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
157 ra->ra_nfail = ra->ra_nok = 0;
160 * A node is eligible for its rate to be raised every 1/10 to 10
161 * seconds, more eligible in proportion to recent packet rates.
163 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
164 ra->ra_raise_interval = msecs_to_ticks(interval);
168 rssadapt_node_init(struct ieee80211_node *ni)
170 struct ieee80211_rssadapt_node *ra;
171 struct ieee80211vap *vap = ni->ni_vap;
172 struct ieee80211_rssadapt *rsa = vap->iv_rs;
173 const struct ieee80211_rateset *rs = &ni->ni_rates;
175 if (ni->ni_rctls == NULL) {
177 IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
178 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
180 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
188 rssadapt_updatestats(ra);
190 /* pick initial rate */
191 for (ra->ra_rix = rs->rs_nrates - 1;
192 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
195 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
196 ra->ra_ticks = ticks;
198 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
199 "RSSADAPT initial rate %d", ni->ni_txrate);
203 rssadapt_node_deinit(struct ieee80211_node *ni)
206 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
214 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
215 i < IEEE80211_RSSADAPT_BKTS;
216 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
225 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
227 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
229 const struct ieee80211_rateset *rs = &ra->ra_rates;
230 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
233 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
234 rssadapt_updatestats(ra);
235 ra->ra_ticks = ticks;
238 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
240 /* XXX this is average rssi, should be using last value */
241 rssi = ni->ni_ic->ic_node_getrssi(ni);
242 for (rix = rs->rs_nrates-1; rix >= 0; rix--)
243 if ((*thrs)[rix] < (rssi << 8))
245 if (rix != ra->ra_rix) {
246 /* update public rate */
247 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
250 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
251 "RSSADAPT new rate %d (pktlen %d rssi %d)",
252 ni->ni_txrate, pktlen, rssi);
258 * Adapt the data rate to suit the conditions. When a transmitted
259 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
260 * raise the RSS threshold for transmitting packets of similar length at
261 * the same data rate.
264 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
267 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
270 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
273 last_thr = (*thrs)[rix];
274 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
275 last_thr, (rssi << 8));
277 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
278 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
279 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
280 last_thr, (*thrs)[rix], rssi);
284 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
286 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
287 uint16_t newthr, oldthr;
290 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
293 if ((*thrs)[rix + 1] > (*thrs)[rix]) {
294 oldthr = (*thrs)[rix + 1];
295 if ((*thrs)[rix] == 0)
296 newthr = (rssi << 8);
298 newthr = (*thrs)[rix];
299 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
302 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
303 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
304 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
305 oldthr, newthr, rssi);
307 ra->ra_last_raise = ticks;
312 rssadapt_tx_complete(const struct ieee80211_node *ni,
313 const struct ieee80211_ratectl_tx_status *status)
315 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
319 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
320 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
323 pktlen = status->pktlen;
326 if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
328 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
329 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
330 rssadapt_raise_rate(ra, pktlen, rssi);
333 rssadapt_lower_rate(ra, pktlen, rssi);
338 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
340 struct ieee80211vap *vap = arg1;
341 struct ieee80211_rssadapt *rs = vap->iv_rs;
342 int msecs = ticks_to_msecs(rs->interval);
345 error = sysctl_handle_int(oidp, &msecs, 0, req);
346 if (error || !req->newptr)
348 rssadapt_setinterval(vap, msecs);
353 rssadapt_sysctlattach(struct ieee80211vap *vap,
354 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
357 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
358 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
359 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");