1 /* $FreeBSD$ */ 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/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> 42 43 #include <net/if.h> 44 #include <net/if_var.h> 45 #include <net/if_media.h> 46 #include <net/ethernet.h> 47 48 #include <net80211/ieee80211_var.h> 49 #include <net80211/ieee80211_rssadapt.h> 50 #include <net80211/ieee80211_ratectl.h> 51 52 struct rssadapt_expavgctl { 53 /* RSS threshold decay. */ 54 u_int rc_decay_denom; 55 u_int rc_decay_old; 56 /* RSS threshold update. */ 57 u_int rc_thresh_denom; 58 u_int rc_thresh_old; 59 /* RSS average update. */ 60 u_int rc_avgrssi_denom; 61 u_int rc_avgrssi_old; 62 }; 63 64 static struct rssadapt_expavgctl master_expavgctl = { 65 .rc_decay_denom = 16, 66 .rc_decay_old = 15, 67 .rc_thresh_denom = 8, 68 .rc_thresh_old = 4, 69 .rc_avgrssi_denom = 8, 70 .rc_avgrssi_old = 4 71 }; 72 73 #ifdef interpolate 74 #undef interpolate 75 #endif 76 #define interpolate(parm, old, new) ((parm##_old * (old) + \ 77 (parm##_denom - parm##_old) * (new)) / \ 78 parm##_denom) 79 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 *, 89 int, int); 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 *); 94 95 /* number of references from net80211 layer */ 96 static int nrefs = 0; 97 98 static const struct ieee80211_ratectl rssadapt = { 99 .ir_name = "rssadapt", 100 .ir_attach = NULL, 101 .ir_detach = NULL, 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, 110 }; 111 IEEE80211_RATECTL_MODULE(rssadapt, 1); 112 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt); 113 114 static void 115 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs) 116 { 117 struct ieee80211_rssadapt *rs = vap->iv_rs; 118 int t; 119 120 if (msecs < 100) 121 msecs = 100; 122 t = msecs_to_ticks(msecs); 123 rs->interval = (t < 1) ? 1 : t; 124 } 125 126 static void 127 rssadapt_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 = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt), 136 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_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 146 static void 147 rssadapt_deinit(struct ieee80211vap *vap) 148 { 149 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL); 150 KASSERT(nrefs > 0, ("imbalanced attach/detach")); 151 nrefs--; /* XXX locking */ 152 } 153 154 static void 155 rssadapt_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 170 static void 171 rssadapt_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 (ni->ni_rctls == NULL) { 179 ni->ni_rctls = ra = 180 IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node), 181 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 182 if (ra == NULL) { 183 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl " 184 "structure\n"); 185 return; 186 } 187 } else 188 ra = ni->ni_rctls; 189 ra->ra_rs = rsa; 190 ra->ra_rates = *rs; 191 rssadapt_updatestats(ra); 192 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; 196 ra->ra_rix--) 197 ; 198 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL; 199 ra->ra_ticks = ticks; 200 201 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 202 "RSSADAPT initial rate %d", ni->ni_txrate); 203 } 204 205 static void 206 rssadapt_node_deinit(struct ieee80211_node *ni) 207 { 208 209 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL); 210 } 211 212 static __inline int 213 bucket(int pktlen) 214 { 215 int i, top, thridx; 216 217 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 218 i < IEEE80211_RSSADAPT_BKTS; 219 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 220 thridx = i; 221 if (pktlen <= top) 222 break; 223 } 224 return thridx; 225 } 226 227 static int 228 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg) 229 { 230 struct ieee80211_rssadapt_node *ra = ni->ni_rctls; 231 u_int pktlen = iarg; 232 const struct ieee80211_rateset *rs = &ra->ra_rates; 233 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 234 int rix, rssi; 235 236 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) { 237 rssadapt_updatestats(ra); 238 ra->ra_ticks = ticks; 239 } 240 241 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 242 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)) 247 break; 248 if (rix != ra->ra_rix) { 249 /* update public rate */ 250 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL; 251 ra->ra_rix = rix; 252 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); 256 } 257 return rix; 258 } 259 260 /* 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. 265 */ 266 static void 267 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi) 268 { 269 uint16_t last_thr; 270 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 271 u_int rix; 272 273 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 274 275 rix = ra->ra_rix; 276 last_thr = (*thrs)[rix]; 277 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh, 278 last_thr, (rssi << 8)); 279 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); 284 } 285 286 static void 287 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi) 288 { 289 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 290 uint16_t newthr, oldthr; 291 int rix; 292 293 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 294 295 rix = ra->ra_rix; 296 if ((*thrs)[rix + 1] > (*thrs)[rix]) { 297 oldthr = (*thrs)[rix + 1]; 298 if ((*thrs)[rix] == 0) 299 newthr = (rssi << 8); 300 else 301 newthr = (*thrs)[rix]; 302 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay, 303 oldthr, newthr); 304 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); 309 310 ra->ra_last_raise = ticks; 311 } 312 } 313 314 static void 315 rssadapt_tx_complete(const struct ieee80211_node *ni, 316 const struct ieee80211_ratectl_tx_status *status) 317 { 318 struct ieee80211_rssadapt_node *ra = ni->ni_rctls; 319 int pktlen, rssi; 320 321 if ((status->flags & 322 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) != 323 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) 324 return; 325 326 pktlen = status->pktlen; 327 rssi = status->rssi; 328 329 if (status->status == IEEE80211_RATECTL_TX_SUCCESS) { 330 ra->ra_nok++; 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); 334 } else { 335 ra->ra_nfail++; 336 rssadapt_lower_rate(ra, pktlen, rssi); 337 } 338 } 339 340 static int 341 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS) 342 { 343 struct ieee80211vap *vap = arg1; 344 struct ieee80211_rssadapt *rs = vap->iv_rs; 345 int msecs = ticks_to_msecs(rs->interval); 346 int error; 347 348 error = sysctl_handle_int(oidp, &msecs, 0, req); 349 if (error || !req->newptr) 350 return error; 351 rssadapt_setinterval(vap, msecs); 352 return 0; 353 } 354 355 static void 356 rssadapt_sysctlattach(struct ieee80211vap *vap, 357 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree) 358 { 359 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)"); 363 } 364