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