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 ieee80211vap *, 91 const struct ieee80211_node *, int, 92 void *, void *); 93 static void rssadapt_sysctlattach(struct ieee80211vap *, 94 struct sysctl_ctx_list *, struct sysctl_oid *); 95 96 /* number of references from net80211 layer */ 97 static int nrefs = 0; 98 99 static const struct ieee80211_ratectl rssadapt = { 100 .ir_name = "rssadapt", 101 .ir_attach = NULL, 102 .ir_detach = NULL, 103 .ir_init = rssadapt_init, 104 .ir_deinit = rssadapt_deinit, 105 .ir_node_init = rssadapt_node_init, 106 .ir_node_deinit = rssadapt_node_deinit, 107 .ir_rate = rssadapt_rate, 108 .ir_tx_complete = rssadapt_tx_complete, 109 .ir_tx_update = NULL, 110 .ir_setinterval = rssadapt_setinterval, 111 }; 112 IEEE80211_RATECTL_MODULE(rssadapt, 1); 113 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt); 114 115 static void 116 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs) 117 { 118 struct ieee80211_rssadapt *rs = vap->iv_rs; 119 int t; 120 121 if (msecs < 100) 122 msecs = 100; 123 t = msecs_to_ticks(msecs); 124 rs->interval = (t < 1) ? 1 : t; 125 } 126 127 static void 128 rssadapt_init(struct ieee80211vap *vap) 129 { 130 struct ieee80211_rssadapt *rs; 131 132 KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized", 133 __func__)); 134 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 146 static void 147 rssadapt_deinit(struct ieee80211vap *vap) 148 { 149 free(vap->iv_rs, M_80211_RATECTL); 150 } 151 152 static void 153 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra) 154 { 155 long interval; 156 157 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2; 158 ra->ra_nfail = ra->ra_nok = 0; 159 160 /* 161 * A node is eligible for its rate to be raised every 1/10 to 10 162 * seconds, more eligible in proportion to recent packet rates. 163 */ 164 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate)); 165 ra->ra_raise_interval = msecs_to_ticks(interval); 166 } 167 168 static void 169 rssadapt_node_init(struct ieee80211_node *ni) 170 { 171 struct ieee80211_rssadapt_node *ra; 172 struct ieee80211vap *vap = ni->ni_vap; 173 struct ieee80211_rssadapt *rsa = vap->iv_rs; 174 const struct ieee80211_rateset *rs = &ni->ni_rates; 175 176 if (ni->ni_rctls == NULL) { 177 ni->ni_rctls = ra = 178 malloc(sizeof(struct ieee80211_rssadapt_node), 179 M_80211_RATECTL, M_NOWAIT|M_ZERO); 180 if (ra == NULL) { 181 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl " 182 "structure\n"); 183 return; 184 } 185 } else 186 ra = ni->ni_rctls; 187 ra->ra_rs = rsa; 188 ra->ra_rates = *rs; 189 rssadapt_updatestats(ra); 190 191 /* pick initial rate */ 192 for (ra->ra_rix = rs->rs_nrates - 1; 193 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72; 194 ra->ra_rix--) 195 ; 196 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL; 197 ra->ra_ticks = ticks; 198 199 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 200 "RSSADAPT initial rate %d", ni->ni_txrate); 201 } 202 203 static void 204 rssadapt_node_deinit(struct ieee80211_node *ni) 205 { 206 207 free(ni->ni_rctls, M_80211_RATECTL); 208 } 209 210 static __inline int 211 bucket(int pktlen) 212 { 213 int i, top, thridx; 214 215 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 216 i < IEEE80211_RSSADAPT_BKTS; 217 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 218 thridx = i; 219 if (pktlen <= top) 220 break; 221 } 222 return thridx; 223 } 224 225 static int 226 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg) 227 { 228 struct ieee80211_rssadapt_node *ra = ni->ni_rctls; 229 u_int pktlen = iarg; 230 const struct ieee80211_rateset *rs = &ra->ra_rates; 231 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 232 int rix, rssi; 233 234 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) { 235 rssadapt_updatestats(ra); 236 ra->ra_ticks = ticks; 237 } 238 239 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 240 241 /* XXX this is average rssi, should be using last value */ 242 rssi = ni->ni_ic->ic_node_getrssi(ni); 243 for (rix = rs->rs_nrates-1; rix >= 0; rix--) 244 if ((*thrs)[rix] < (rssi << 8)) 245 break; 246 if (rix != ra->ra_rix) { 247 /* update public rate */ 248 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL; 249 ra->ra_rix = rix; 250 251 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 252 "RSSADAPT new rate %d (pktlen %d rssi %d)", 253 ni->ni_txrate, pktlen, rssi); 254 } 255 return rix; 256 } 257 258 /* 259 * Adapt the data rate to suit the conditions. When a transmitted 260 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions, 261 * raise the RSS threshold for transmitting packets of similar length at 262 * the same data rate. 263 */ 264 static void 265 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi) 266 { 267 uint16_t last_thr; 268 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 269 u_int rix; 270 271 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 272 273 rix = ra->ra_rix; 274 last_thr = (*thrs)[rix]; 275 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh, 276 last_thr, (rssi << 8)); 277 278 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL, 279 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n", 280 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL, 281 last_thr, (*thrs)[rix], rssi); 282 } 283 284 static void 285 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi) 286 { 287 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 288 uint16_t newthr, oldthr; 289 int rix; 290 291 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 292 293 rix = ra->ra_rix; 294 if ((*thrs)[rix + 1] > (*thrs)[rix]) { 295 oldthr = (*thrs)[rix + 1]; 296 if ((*thrs)[rix] == 0) 297 newthr = (rssi << 8); 298 else 299 newthr = (*thrs)[rix]; 300 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay, 301 oldthr, newthr); 302 303 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL, 304 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n", 305 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL, 306 oldthr, newthr, rssi); 307 308 ra->ra_last_raise = ticks; 309 } 310 } 311 312 static void 313 rssadapt_tx_complete(const struct ieee80211vap *vap, 314 const struct ieee80211_node *ni, int success, void *arg1, void *arg2) 315 { 316 struct ieee80211_rssadapt_node *ra = ni->ni_rctls; 317 int pktlen = *(int *)arg1, rssi = *(int *)arg2; 318 319 if (success) { 320 ra->ra_nok++; 321 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates && 322 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval) 323 rssadapt_raise_rate(ra, pktlen, rssi); 324 } else { 325 ra->ra_nfail++; 326 rssadapt_lower_rate(ra, pktlen, rssi); 327 } 328 } 329 330 static int 331 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS) 332 { 333 struct ieee80211vap *vap = arg1; 334 struct ieee80211_rssadapt *rs = vap->iv_rs; 335 int msecs = ticks_to_msecs(rs->interval); 336 int error; 337 338 error = sysctl_handle_int(oidp, &msecs, 0, req); 339 if (error || !req->newptr) 340 return error; 341 rssadapt_setinterval(vap, msecs); 342 return 0; 343 } 344 345 static void 346 rssadapt_sysctlattach(struct ieee80211vap *vap, 347 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree) 348 { 349 350 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 351 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap, 352 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)"); 353 } 354