2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
7 * Copyright (c) 2003, 2004 David Young. All rights reserved.
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * 3. The name of David Young may not be used to endorse or promote
19 * products derived from this software without specific prior
22 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
26 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_rssadapt.h>
52 #include <net80211/ieee80211_ratectl.h>
54 struct rssadapt_expavgctl {
55 /* RSS threshold decay. */
58 /* RSS threshold update. */
59 u_int rc_thresh_denom;
61 /* RSS average update. */
62 u_int rc_avgrssi_denom;
66 static struct rssadapt_expavgctl master_expavgctl = {
71 .rc_avgrssi_denom = 8,
78 #define interpolate(parm, old, new) ((parm##_old * (old) + \
79 (parm##_denom - parm##_old) * (new)) / \
82 static void rssadapt_setinterval(const struct ieee80211vap *, int);
83 static void rssadapt_init(struct ieee80211vap *);
84 static void rssadapt_deinit(struct ieee80211vap *);
85 static void rssadapt_updatestats(struct ieee80211_rssadapt_node *);
86 static void rssadapt_node_init(struct ieee80211_node *);
87 static void rssadapt_node_deinit(struct ieee80211_node *);
88 static int rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
89 static void rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
90 static void rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
92 static void rssadapt_tx_complete(const struct ieee80211_node *,
93 const struct ieee80211_ratectl_tx_status *);
94 static void rssadapt_sysctlattach(struct ieee80211vap *,
95 struct sysctl_ctx_list *, struct sysctl_oid *);
97 /* number of references from net80211 layer */
100 static const struct ieee80211_ratectl rssadapt = {
101 .ir_name = "rssadapt",
104 .ir_init = rssadapt_init,
105 .ir_deinit = rssadapt_deinit,
106 .ir_node_init = rssadapt_node_init,
107 .ir_node_deinit = rssadapt_node_deinit,
108 .ir_rate = rssadapt_rate,
109 .ir_tx_complete = rssadapt_tx_complete,
110 .ir_tx_update = NULL,
111 .ir_setinterval = rssadapt_setinterval,
113 IEEE80211_RATECTL_MODULE(rssadapt, 1);
114 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
117 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
119 struct ieee80211_rssadapt *rs = vap->iv_rs;
126 rs->interval = msecs_to_ticks(msecs);
130 rssadapt_init(struct ieee80211vap *vap)
132 struct ieee80211_rssadapt *rs;
134 KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
137 nrefs++; /* XXX locking */
138 vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
139 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
141 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
145 rssadapt_setinterval(vap, 500 /* msecs */);
146 rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
150 rssadapt_deinit(struct ieee80211vap *vap)
152 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
153 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
154 nrefs--; /* XXX locking */
158 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
162 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
163 ra->ra_nfail = ra->ra_nok = 0;
166 * A node is eligible for its rate to be raised every 1/10 to 10
167 * seconds, more eligible in proportion to recent packet rates.
169 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
170 ra->ra_raise_interval = msecs_to_ticks(interval);
174 rssadapt_node_init(struct ieee80211_node *ni)
176 struct ieee80211_rssadapt_node *ra;
177 struct ieee80211vap *vap = ni->ni_vap;
178 struct ieee80211_rssadapt *rsa = vap->iv_rs;
179 const struct ieee80211_rateset *rs = &ni->ni_rates;
182 if_printf(vap->iv_ifp, "ratectl structure was not allocated, "
183 "per-node structure allocation skipped\n");
187 if (ni->ni_rctls == NULL) {
189 IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
190 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
192 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
200 rssadapt_updatestats(ra);
202 /* pick initial rate */
203 for (ra->ra_rix = rs->rs_nrates - 1;
204 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
207 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
208 ra->ra_ticks = ticks;
210 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
211 "RSSADAPT initial rate %d", ni->ni_txrate);
215 rssadapt_node_deinit(struct ieee80211_node *ni)
218 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
226 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
227 i < IEEE80211_RSSADAPT_BKTS;
228 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
237 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
239 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
241 const struct ieee80211_rateset *rs;
242 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
245 /* XXX should return -1 here, but drivers may not expect this... */
248 ni->ni_txrate = ni->ni_rates.rs_rates[0];
253 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
254 rssadapt_updatestats(ra);
255 ra->ra_ticks = ticks;
258 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
260 /* XXX this is average rssi, should be using last value */
261 rssi = ni->ni_ic->ic_node_getrssi(ni);
262 for (rix = rs->rs_nrates-1; rix >= 0; rix--)
263 if ((*thrs)[rix] < (rssi << 8))
265 if (rix != ra->ra_rix) {
266 /* update public rate */
267 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
270 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
271 "RSSADAPT new rate %d (pktlen %d rssi %d)",
272 ni->ni_txrate, pktlen, rssi);
278 * Adapt the data rate to suit the conditions. When a transmitted
279 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
280 * raise the RSS threshold for transmitting packets of similar length at
281 * the same data rate.
284 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
287 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
290 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
293 last_thr = (*thrs)[rix];
294 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
295 last_thr, (rssi << 8));
297 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
298 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
299 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
300 last_thr, (*thrs)[rix], rssi);
304 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
306 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
307 uint16_t newthr, oldthr;
310 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
313 if ((*thrs)[rix + 1] > (*thrs)[rix]) {
314 oldthr = (*thrs)[rix + 1];
315 if ((*thrs)[rix] == 0)
316 newthr = (rssi << 8);
318 newthr = (*thrs)[rix];
319 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
322 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
323 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
324 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
325 oldthr, newthr, rssi);
327 ra->ra_last_raise = ticks;
332 rssadapt_tx_complete(const struct ieee80211_node *ni,
333 const struct ieee80211_ratectl_tx_status *status)
335 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
342 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
343 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
346 pktlen = status->pktlen;
349 if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
351 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
352 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
353 rssadapt_raise_rate(ra, pktlen, rssi);
356 rssadapt_lower_rate(ra, pktlen, rssi);
361 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
363 struct ieee80211vap *vap = arg1;
364 struct ieee80211_rssadapt *rs = vap->iv_rs;
370 msecs = ticks_to_msecs(rs->interval);
371 error = sysctl_handle_int(oidp, &msecs, 0, req);
372 if (error || !req->newptr)
374 rssadapt_setinterval(vap, msecs);
379 rssadapt_sysctlattach(struct ieee80211vap *vap,
380 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
383 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
384 "rssadapt_rate_interval",
385 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vap, 0,
386 rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");