1 /* $FreeBSD$ */ 2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */ 3 /*- 4 * Copyright (c) 2003, 2004 David Young. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or 7 * without modification, are permitted provided that the following 8 * conditions are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following 13 * disclaimer in the documentation and/or other materials provided 14 * with the distribution. 15 * 3. The name of David Young may not be used to endorse or promote 16 * products derived from this software without specific prior 17 * written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David 23 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 25 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 */ 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/socket.h> 38 #include <sys/sysctl.h> 39 40 #include <net/if.h> 41 #include <net/if_media.h> 42 43 #include <net80211/ieee80211_var.h> 44 #include <net80211/ieee80211_rssadapt.h> 45 46 struct rssadapt_expavgctl { 47 /* RSS threshold decay. */ 48 u_int rc_decay_denom; 49 u_int rc_decay_old; 50 /* RSS threshold update. */ 51 u_int rc_thresh_denom; 52 u_int rc_thresh_old; 53 /* RSS average update. */ 54 u_int rc_avgrssi_denom; 55 u_int rc_avgrssi_old; 56 }; 57 58 static struct rssadapt_expavgctl master_expavgctl = { 59 rc_decay_denom : 16, 60 rc_decay_old : 15, 61 rc_thresh_denom : 8, 62 rc_thresh_old : 4, 63 rc_avgrssi_denom : 8, 64 rc_avgrssi_old : 4 65 }; 66 67 #ifdef interpolate 68 #undef interpolate 69 #endif 70 #define interpolate(parm, old, new) ((parm##_old * (old) + \ 71 (parm##_denom - parm##_old) * (new)) / \ 72 parm##_denom) 73 74 static void rssadapt_sysctlattach(struct ieee80211_rssadapt *rs, 75 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree); 76 77 /* number of references from net80211 layer */ 78 static int nrefs = 0; 79 80 void 81 ieee80211_rssadapt_setinterval(struct ieee80211_rssadapt *rs, int msecs) 82 { 83 int t; 84 85 if (msecs < 100) 86 msecs = 100; 87 t = msecs_to_ticks(msecs); 88 rs->interval = (t < 1) ? 1 : t; 89 } 90 91 void 92 ieee80211_rssadapt_init(struct ieee80211_rssadapt *rs, struct ieee80211vap *vap, int interval) 93 { 94 rs->vap = vap; 95 ieee80211_rssadapt_setinterval(rs, interval); 96 97 rssadapt_sysctlattach(rs, vap->iv_sysctl, vap->iv_oid); 98 } 99 100 void 101 ieee80211_rssadapt_cleanup(struct ieee80211_rssadapt *rs) 102 { 103 } 104 105 static void 106 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra) 107 { 108 long interval; 109 110 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2; 111 ra->ra_nfail = ra->ra_nok = 0; 112 113 /* 114 * A node is eligible for its rate to be raised every 1/10 to 10 115 * seconds, more eligible in proportion to recent packet rates. 116 */ 117 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate)); 118 ra->ra_raise_interval = msecs_to_ticks(interval); 119 } 120 121 void 122 ieee80211_rssadapt_node_init(struct ieee80211_rssadapt *rsa, 123 struct ieee80211_rssadapt_node *ra, struct ieee80211_node *ni) 124 { 125 const struct ieee80211_rateset *rs = &ni->ni_rates; 126 127 ra->ra_rs = rsa; 128 ra->ra_rates = *rs; 129 rssadapt_updatestats(ra); 130 131 /* pick initial rate */ 132 for (ra->ra_rix = rs->rs_nrates - 1; 133 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72; 134 ra->ra_rix--) 135 ; 136 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL; 137 ra->ra_ticks = ticks; 138 139 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 140 "RSSADAPT initial rate %d", ni->ni_txrate); 141 } 142 143 static __inline int 144 bucket(int pktlen) 145 { 146 int i, top, thridx; 147 148 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 149 i < IEEE80211_RSSADAPT_BKTS; 150 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 151 thridx = i; 152 if (pktlen <= top) 153 break; 154 } 155 return thridx; 156 } 157 158 int 159 ieee80211_rssadapt_choose(struct ieee80211_node *ni, 160 struct ieee80211_rssadapt_node *ra, u_int pktlen) 161 { 162 const struct ieee80211_rateset *rs = &ra->ra_rates; 163 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 164 int rix, rssi; 165 166 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) { 167 rssadapt_updatestats(ra); 168 ra->ra_ticks = ticks; 169 } 170 171 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 172 173 /* XXX this is average rssi, should be using last value */ 174 rssi = ni->ni_ic->ic_node_getrssi(ni); 175 for (rix = rs->rs_nrates-1; rix >= 0; rix--) 176 if ((*thrs)[rix] < (rssi << 8)) 177 break; 178 if (rix != ra->ra_rix) { 179 /* update public rate */ 180 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL; 181 ra->ra_rix = rix; 182 183 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 184 "RSSADAPT new rate %d (pktlen %d rssi %d)", 185 ni->ni_txrate, pktlen, rssi); 186 } 187 return rix; 188 } 189 190 /* 191 * Adapt the data rate to suit the conditions. When a transmitted 192 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions, 193 * raise the RSS threshold for transmitting packets of similar length at 194 * the same data rate. 195 */ 196 void 197 ieee80211_rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, 198 int pktlen, int rssi) 199 { 200 uint16_t last_thr; 201 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 202 u_int rix; 203 204 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 205 206 rix = ra->ra_rix; 207 last_thr = (*thrs)[rix]; 208 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh, 209 last_thr, (rssi << 8)); 210 211 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL, 212 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n", 213 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL, 214 last_thr, (*thrs)[rix], rssi); 215 } 216 217 void 218 ieee80211_rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, 219 int pktlen, int rssi) 220 { 221 uint16_t (*thrs)[IEEE80211_RATE_SIZE]; 222 uint16_t newthr, oldthr; 223 int rix; 224 225 thrs = &ra->ra_rate_thresh[bucket(pktlen)]; 226 227 rix = ra->ra_rix; 228 if ((*thrs)[rix + 1] > (*thrs)[rix]) { 229 oldthr = (*thrs)[rix + 1]; 230 if ((*thrs)[rix] == 0) 231 newthr = (rssi << 8); 232 else 233 newthr = (*thrs)[rix]; 234 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay, 235 oldthr, newthr); 236 237 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL, 238 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n", 239 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL, 240 oldthr, newthr, rssi); 241 242 ra->ra_last_raise = ticks; 243 } 244 } 245 246 static int 247 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS) 248 { 249 struct ieee80211_rssadapt *rs = arg1; 250 int msecs = ticks_to_msecs(rs->interval); 251 int error; 252 253 error = sysctl_handle_int(oidp, &msecs, 0, req); 254 if (error || !req->newptr) 255 return error; 256 ieee80211_rssadapt_setinterval(rs, msecs); 257 return 0; 258 } 259 260 static void 261 rssadapt_sysctlattach(struct ieee80211_rssadapt *rs, 262 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree) 263 { 264 265 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 266 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, rs, 267 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)"); 268 } 269 270 /* 271 * Module glue. 272 */ 273 IEEE80211_RATE_MODULE(rssadapt, 1); 274