Home | History | Annotate | Line # | Download | only in net80211
ieee80211_rssadapt.c revision 1.21.16.3
      1 /*	$NetBSD: ieee80211_rssadapt.c,v 1.21.16.3 2018/07/16 20:11:11 phil Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-3-Clause
      5  *
      6  * Copyright (c) 2010 Rui Paulo <rpaulo (at) FreeBSD.org>
      7  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or
     10  * without modification, are permitted provided that the following
     11  * conditions are met:
     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
     20  *    written permission.
     21  *
     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
     33  * OF SUCH DAMAGE.
     34  */
     35 #include "opt_wlan.h"
     36 
     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 #ifdef __NetBSD__
     43 /* Why doesn't FreeBSD have this?  */
     44 #include <sys/sbuf.h>
     45 #endif
     46 #include <sys/socket.h>
     47 #include <sys/sysctl.h>
     48 
     49 #include <net/if.h>
     50 #ifdef __FreeBSD__
     51 #include <net/if_var.h>
     52 #endif
     53 #include <net/if_media.h>
     54 #ifdef __FreeBSD__
     55 #include <net/ethernet.h>
     56 #endif
     57 #ifdef __NetBSD__
     58 #include <net/route.h>
     59 #endif
     60 
     61 #include <net80211/ieee80211_var.h>
     62 #include <net80211/ieee80211_rssadapt.h>
     63 #include <net80211/ieee80211_ratectl.h>
     64 
     65 #ifdef __NetBSD__
     66 #undef  KASSERT
     67 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     68 #endif
     69 
     70 struct rssadapt_expavgctl {
     71 	/* RSS threshold decay. */
     72 	u_int rc_decay_denom;
     73 	u_int rc_decay_old;
     74 	/* RSS threshold update. */
     75 	u_int rc_thresh_denom;
     76 	u_int rc_thresh_old;
     77 	/* RSS average update. */
     78 	u_int rc_avgrssi_denom;
     79 	u_int rc_avgrssi_old;
     80 };
     81 
     82 static struct rssadapt_expavgctl master_expavgctl = {
     83 	.rc_decay_denom = 16,
     84 	.rc_decay_old = 15,
     85 	.rc_thresh_denom = 8,
     86 	.rc_thresh_old = 4,
     87 	.rc_avgrssi_denom = 8,
     88 	.rc_avgrssi_old = 4
     89 };
     90 
     91 #ifdef interpolate
     92 #undef interpolate
     93 #endif
     94 #define interpolate(parm, old, new) ((parm##_old * (old) + \
     95                                      (parm##_denom - parm##_old) * (new)) / \
     96 				    parm##_denom)
     97 
     98 static void	rssadapt_setinterval(const struct ieee80211vap *, int);
     99 static void	rssadapt_init(struct ieee80211vap *);
    100 static void	rssadapt_deinit(struct ieee80211vap *);
    101 static void	rssadapt_updatestats(struct ieee80211_rssadapt_node *);
    102 static void	rssadapt_node_init(struct ieee80211_node *);
    103 static void	rssadapt_node_deinit(struct ieee80211_node *);
    104 static int	rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
    105 static void	rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
    106 static void	rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
    107 			int, int);
    108 static void	rssadapt_tx_complete(const struct ieee80211_node *,
    109 			const struct ieee80211_ratectl_tx_status *);
    110 static void	rssadapt_sysctlattach(struct ieee80211vap *,
    111 			struct sysctl_ctx_list *, struct sysctl_oid *);
    112 
    113 /* number of references from net80211 layer */
    114 static	int nrefs = 0;
    115 
    116 #if __FreeBSD__
    117 static const struct ieee80211_ratectl rssadapt = {
    118 #elif __NetBSD__
    119 const struct ieee80211_ratectl ratectl_rssadapt = {
    120 #endif
    121 	.ir_name	= "rssadapt",
    122 	.ir_attach	= NULL,
    123 	.ir_detach	= NULL,
    124 	.ir_init	= rssadapt_init,
    125 	.ir_deinit	= rssadapt_deinit,
    126 	.ir_node_init	= rssadapt_node_init,
    127 	.ir_node_deinit	= rssadapt_node_deinit,
    128 	.ir_rate	= rssadapt_rate,
    129 	.ir_tx_complete	= rssadapt_tx_complete,
    130 	.ir_tx_update	= NULL,
    131 	.ir_setinterval	= rssadapt_setinterval,
    132 };
    133 #if __FreeBSD__
    134 IEEE80211_RATECTL_MODULE(rssadapt, 1);
    135 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
    136 #endif
    137 
    138 static void
    139 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
    140 {
    141 	struct ieee80211_rssadapt *rs = vap->iv_rs;
    142 	int t;
    143 
    144 	if (msecs < 100)
    145 		msecs = 100;
    146 	t = msecs_to_ticks(msecs);
    147 	rs->interval = (t < 1) ? 1 : t;
    148 }
    149 
    150 static void
    151 rssadapt_init(struct ieee80211vap *vap)
    152 {
    153 	struct ieee80211_rssadapt *rs;
    154 
    155 	KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
    156 	    __func__));
    157 
    158 	nrefs++;		/* XXX locking */
    159 	vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
    160 	    M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    161 	if (rs == NULL) {
    162 		if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
    163 		return;
    164 	}
    165 	rs->vap = vap;
    166 	rssadapt_setinterval(vap, 500 /* msecs */);
    167 	rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
    168 }
    169 
    170 static void
    171 rssadapt_deinit(struct ieee80211vap *vap)
    172 {
    173 	IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
    174 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
    175 	nrefs--;		/* XXX locking */
    176 }
    177 
    178 static void
    179 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
    180 {
    181 	long interval;
    182 
    183 	ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
    184 	ra->ra_nfail = ra->ra_nok = 0;
    185 
    186 	/*
    187 	 * A node is eligible for its rate to be raised every 1/10 to 10
    188 	 * seconds, more eligible in proportion to recent packet rates.
    189 	 */
    190 	interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
    191 	ra->ra_raise_interval = msecs_to_ticks(interval);
    192 }
    193 
    194 static void
    195 rssadapt_node_init(struct ieee80211_node *ni)
    196 {
    197 	struct ieee80211_rssadapt_node *ra;
    198 	struct ieee80211vap *vap = ni->ni_vap;
    199 	struct ieee80211_rssadapt *rsa = vap->iv_rs;
    200 	const struct ieee80211_rateset *rs = &ni->ni_rates;
    201 
    202 	if (ni->ni_rctls == NULL) {
    203 		ni->ni_rctls = ra =
    204 		    IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
    205 		        M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    206 		if (ra == NULL) {
    207 			if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
    208 			    "structure\n");
    209 			return;
    210 		}
    211 	} else
    212 		ra = ni->ni_rctls;
    213 	ra->ra_rs = rsa;
    214 	ra->ra_rates = *rs;
    215 	rssadapt_updatestats(ra);
    216 
    217 	/* pick initial rate */
    218 	for (ra->ra_rix = rs->rs_nrates - 1;
    219 	     ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
    220 	     ra->ra_rix--)
    221 		;
    222 	ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
    223 	ra->ra_ticks = ticks;
    224 
    225 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
    226 	    "RSSADAPT initial rate %d", ni->ni_txrate);
    227 }
    228 
    229 static void
    230 rssadapt_node_deinit(struct ieee80211_node *ni)
    231 {
    232 
    233 	IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
    234 }
    235 
    236 static __inline int
    237 bucket(int pktlen)
    238 {
    239 	int i, top, thridx;
    240 
    241 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    242 	     i < IEEE80211_RSSADAPT_BKTS;
    243 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    244 		thridx = i;
    245 		if (pktlen <= top)
    246 			break;
    247 	}
    248 	return thridx;
    249 }
    250 
    251 static int
    252 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
    253 {
    254 	struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
    255 	u_int pktlen = iarg;
    256 	const struct ieee80211_rateset *rs = &ra->ra_rates;
    257 	uint16_t (*thrs)[IEEE80211_RATE_SIZE];
    258 	int rix, rssi;
    259 
    260 	if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
    261 		rssadapt_updatestats(ra);
    262 		ra->ra_ticks = ticks;
    263 	}
    264 
    265 	thrs = &ra->ra_rate_thresh[bucket(pktlen)];
    266 
    267 	/* XXX this is average rssi, should be using last value */
    268 	rssi = ni->ni_ic->ic_node_getrssi(ni);
    269 	for (rix = rs->rs_nrates-1; rix >= 0; rix--)
    270 		if ((*thrs)[rix] < (rssi << 8))
    271 			break;
    272 	if (rix != ra->ra_rix) {
    273 		/* update public rate */
    274 		ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
    275 		ra->ra_rix = rix;
    276 
    277 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
    278 		    "RSSADAPT new rate %d (pktlen %d rssi %d)",
    279 		    ni->ni_txrate, pktlen, rssi);
    280 	}
    281 	return rix;
    282 }
    283 
    284 /*
    285  * Adapt the data rate to suit the conditions.  When a transmitted
    286  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
    287  * raise the RSS threshold for transmitting packets of similar length at
    288  * the same data rate.
    289  */
    290 static void
    291 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
    292 {
    293 	uint16_t last_thr;
    294 	uint16_t (*thrs)[IEEE80211_RATE_SIZE];
    295 	u_int rix;
    296 
    297 	thrs = &ra->ra_rate_thresh[bucket(pktlen)];
    298 
    299 	rix = ra->ra_rix;
    300 	last_thr = (*thrs)[rix];
    301 	(*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
    302 	    last_thr, (rssi << 8));
    303 
    304 	IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
    305 	    "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
    306 	    ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
    307 	    last_thr, (*thrs)[rix], rssi);
    308 }
    309 
    310 static void
    311 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
    312 {
    313 	uint16_t (*thrs)[IEEE80211_RATE_SIZE];
    314 	uint16_t newthr, oldthr;
    315 	int rix;
    316 
    317 	thrs = &ra->ra_rate_thresh[bucket(pktlen)];
    318 
    319 	rix = ra->ra_rix;
    320 	if ((*thrs)[rix + 1] > (*thrs)[rix]) {
    321 		oldthr = (*thrs)[rix + 1];
    322 		if ((*thrs)[rix] == 0)
    323 			newthr = (rssi << 8);
    324 		else
    325 			newthr = (*thrs)[rix];
    326 		(*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
    327 		    oldthr, newthr);
    328 
    329 		IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
    330 		    "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
    331 		    ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
    332 		    oldthr, newthr, rssi);
    333 
    334 		ra->ra_last_raise = ticks;
    335 	}
    336 }
    337 
    338 static void
    339 rssadapt_tx_complete(const struct ieee80211_node *ni,
    340     const struct ieee80211_ratectl_tx_status *status)
    341 {
    342 	struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
    343 	int pktlen, rssi;
    344 
    345 	if ((status->flags &
    346 	    (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
    347 	    (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
    348 		return;
    349 
    350 	pktlen = status->pktlen;
    351 	rssi = status->rssi;
    352 
    353 	if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
    354 		ra->ra_nok++;
    355 		if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
    356 		    (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
    357 			rssadapt_raise_rate(ra, pktlen, rssi);
    358 	} else {
    359 		ra->ra_nfail++;
    360 		rssadapt_lower_rate(ra, pktlen, rssi);
    361 	}
    362 }
    363 
    364 #ifdef notyet
    365 static int
    366 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
    367 {
    368 	struct ieee80211vap *vap = arg1;
    369 	struct ieee80211_rssadapt *rs = vap->iv_rs;
    370 	int msecs = ticks_to_msecs(rs->interval);
    371 	int error;
    372 
    373 	error = sysctl_handle_int(oidp, &msecs, 0, req);
    374 	if (error || !req->newptr)
    375 		return error;
    376 	rssadapt_setinterval(vap, msecs);
    377 	return 0;
    378 }
    379 #endif
    380 
    381 static void
    382 rssadapt_sysctlattach(struct ieee80211vap *vap,
    383     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
    384 {
    385 #ifdef notyet
    386 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    387 	    "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
    388 	    0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
    389 #endif
    390 }
    391