1 /* $NetBSD: ieee80211_rssadapt.c,v 1.8 2004/07/23 06:44:56 mycroft Exp $ */ 2 /*- 3 * Copyright (c) 2003, 2004 David Young. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the following 7 * conditions are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 14 * 3. The name of David Young may not be used to endorse or promote 15 * products derived from this software without specific prior 16 * written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David 22 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 29 * OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/kernel.h> /* for hz */ 35 #include <sys/sysctl.h> 36 37 #include <net/if.h> 38 #include <net/if_media.h> 39 #include <net/if_ether.h> 40 41 #include <net80211/ieee80211_var.h> 42 #include <net80211/ieee80211.h> 43 #include <net80211/ieee80211_compat.h> 44 #include <net80211/ieee80211_rssadapt.h> 45 46 #ifdef interpolate 47 #undef interpolate 48 #endif 49 #define interpolate(parm, old, new) ((parm##_old * (old) + \ 50 (parm##_denom - parm##_old) * (new)) / \ 51 parm##_denom) 52 53 #ifdef IEEE80211_DEBUG 54 static struct timeval lastrateadapt; /* time of last rate adaptation msg */ 55 static int currssadaptps = 0; /* rate-adaptation msgs this second */ 56 static int ieee80211_adaptrate = 4; /* rate-adaptation max msgs/sec */ 57 58 #define RSSADAPT_DO_PRINT() \ 59 ((ieee80211_rssadapt_debug > 0) && \ 60 ppsratecheck(&lastrateadapt, &currssadaptps, ieee80211_adaptrate)) 61 #define RSSADAPT_PRINTF(X) \ 62 if (RSSADAPT_DO_PRINT()) \ 63 printf X 64 65 int ieee80211_rssadapt_debug = 0; 66 67 #else 68 #define RSSADAPT_DO_PRINT() (0) 69 #define RSSADAPT_PRINTF(X) 70 #endif 71 72 static struct ieee80211_rssadapt_expavgctl master_expavgctl = { 73 rc_decay_denom : 16, 74 rc_decay_old : 15, 75 rc_thresh_denom : 8, 76 rc_thresh_old : 4, 77 rc_avgrssi_denom : 8, 78 rc_avgrssi_old : 4 79 }; 80 81 #ifdef __NetBSD__ 82 #ifdef IEEE80211_DEBUG 83 /* TBD factor with sysctl_ath_verify, sysctl_ieee80211_verify. */ 84 static int 85 sysctl_ieee80211_rssadapt_debug(SYSCTLFN_ARGS) 86 { 87 int error, t; 88 struct sysctlnode node; 89 90 node = *rnode; 91 t = *(int*)rnode->sysctl_data; 92 node.sysctl_data = &t; 93 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 94 if (error || newp == NULL) 95 return (error); 96 97 if (t < 0 || t > 2) 98 return (EINVAL); 99 *(int*)rnode->sysctl_data = t; 100 101 return (0); 102 } 103 #endif /* IEEE80211_DEBUG */ 104 105 /* TBD factor with sysctl_ath_verify, sysctl_ieee80211_verify. */ 106 static int 107 sysctl_ieee80211_rssadapt_expavgctl(SYSCTLFN_ARGS) 108 { 109 struct ieee80211_rssadapt_expavgctl rc; 110 int error; 111 struct sysctlnode node; 112 113 node = *rnode; 114 rc = *(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data; 115 node.sysctl_data = &rc; 116 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 117 if (error || newp == NULL) 118 return (error); 119 120 if (rc.rc_decay_old < 0 || 121 rc.rc_decay_denom < rc.rc_decay_old) 122 return (EINVAL); 123 124 if (rc.rc_thresh_old < 0 || 125 rc.rc_thresh_denom < rc.rc_thresh_old) 126 return (EINVAL); 127 128 if (rc.rc_avgrssi_old < 0 || 129 rc.rc_avgrssi_denom < rc.rc_avgrssi_old) 130 return (EINVAL); 131 132 *(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data = rc; 133 134 return (0); 135 } 136 137 /* 138 * Setup sysctl(3) MIB, net.ieee80211.* 139 * 140 * TBD condition CTLFLAG_PERMANENT on being an LKM or not 141 */ 142 SYSCTL_SETUP(sysctl_ieee80211_rssadapt, 143 "sysctl ieee80211 rssadapt subtree setup") 144 { 145 int rc; 146 struct sysctlnode *node; 147 148 if ((rc = sysctl_createv(clog, 0, NULL, &node, 149 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL, 150 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0) 151 goto err; 152 153 if ((rc = sysctl_createv(clog, 0, &node, &node, 154 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link", NULL, 155 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0) 156 goto err; 157 158 if ((rc = sysctl_createv(clog, 0, &node, &node, 159 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211", NULL, 160 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 161 goto err; 162 163 if ((rc = sysctl_createv(clog, 0, &node, &node, 164 CTLFLAG_PERMANENT, CTLTYPE_NODE, "rssadapt", 165 SYSCTL_DESCR("Received Signal Strength adaptation controls"), 166 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 167 goto err; 168 169 #ifdef IEEE80211_DEBUG 170 /* control debugging printfs */ 171 if ((rc = sysctl_createv(clog, 0, &node, NULL, 172 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug", 173 SYSCTL_DESCR("Enable RSS adaptation debugging output"), 174 sysctl_ieee80211_rssadapt_debug, 0, &ieee80211_rssadapt_debug, 0, 175 CTL_CREATE, CTL_EOL)) != 0) 176 goto err; 177 #endif /* IEEE80211_DEBUG */ 178 179 /* control rate of decay for exponential averages */ 180 if ((rc = sysctl_createv(clog, 0, &node, NULL, 181 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRUCT, 182 "expavgctl", SYSCTL_DESCR("RSS exponential averaging control"), 183 sysctl_ieee80211_rssadapt_expavgctl, 0, 184 &master_expavgctl, sizeof(master_expavgctl), CTL_CREATE, 185 CTL_EOL)) != 0) 186 goto err; 187 188 return; 189 err: 190 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 191 } 192 #endif /* __NetBSD__ */ 193 194 int 195 ieee80211_rssadapt_choose(struct ieee80211_rssadapt *ra, 196 struct ieee80211_rateset *rs, struct ieee80211_frame *wh, u_int len, 197 int fixed_rate, const char *dvname, int do_not_adapt) 198 { 199 u_int16_t (*thrs)[IEEE80211_RATE_SIZE]; 200 int flags = 0, i, rateidx = 0, thridx, top; 201 202 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 203 flags |= IEEE80211_RATE_BASIC; 204 205 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 206 i < IEEE80211_RSSADAPT_BKTS; 207 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 208 thridx = i; 209 if (len <= top) 210 break; 211 } 212 213 thrs = &ra->ra_rate_thresh[thridx]; 214 215 if (fixed_rate != -1) { 216 if ((rs->rs_rates[fixed_rate] & flags) == flags) { 217 rateidx = fixed_rate; 218 goto out; 219 } 220 flags |= IEEE80211_RATE_BASIC; 221 i = fixed_rate; 222 } else 223 i = rs->rs_nrates; 224 225 while (--i >= 0) { 226 rateidx = i; 227 if ((rs->rs_rates[i] & flags) != flags) 228 continue; 229 if (do_not_adapt) 230 break; 231 if ((*thrs)[i] < ra->ra_avg_rssi) 232 break; 233 } 234 235 out: 236 #ifdef IEEE80211_DEBUG 237 if (ieee80211_rssadapt_debug && dvname != NULL) { 238 printf("%s: dst %s threshold[%d, %d.%d] %d < %d\n", 239 dvname, ether_sprintf(wh->i_addr1), len, 240 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) / 2, 241 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) * 5 % 10, 242 (*thrs)[rateidx], ra->ra_avg_rssi); 243 } 244 #endif /* IEEE80211_DEBUG */ 245 return rateidx; 246 } 247 248 void 249 ieee80211_rssadapt_updatestats(struct ieee80211_rssadapt *ra) 250 { 251 long interval; 252 253 ra->ra_pktrate = 254 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2; 255 ra->ra_nfail = ra->ra_nok = 0; 256 257 /* a node is eligible for its rate to be raised every 1/10 to 10 258 * seconds, more eligible in proportion to recent packet rates. 259 */ 260 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate)); 261 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000); 262 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000); 263 } 264 265 void 266 ieee80211_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni, 267 struct ieee80211_rssadapt *ra, int rssi) 268 { 269 #ifdef IEEE80211_DEBUG 270 int last_avg_rssi = ra->ra_avg_rssi; 271 #endif 272 273 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi, 274 ra->ra_avg_rssi, (rssi << 8)); 275 276 RSSADAPT_PRINTF(("%s: src %s rssi %d avg %d -> %d\n", 277 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr), 278 rssi, last_avg_rssi, ra->ra_avg_rssi)); 279 } 280 281 /* 282 * Adapt the data rate to suit the conditions. When a transmitted 283 * packet is dropped after IEEE80211_RSSADAPT_RETRY_LIMIT retransmissions, 284 * raise the RSS threshold for transmitting packets of similar length at 285 * the same data rate. 286 */ 287 void 288 ieee80211_rssadapt_lower_rate(struct ieee80211com *ic, 289 struct ieee80211_node *ni, struct ieee80211_rssadapt *ra, 290 struct ieee80211_rssdesc *id) 291 { 292 struct ieee80211_rateset *rs = &ni->ni_rates; 293 u_int16_t last_thr; 294 u_int i, thridx, top; 295 296 ra->ra_nfail++; 297 298 if (id->id_rateidx >= rs->rs_nrates) { 299 RSSADAPT_PRINTF(("ieee80211_rssadapt_lower_rate: " 300 "%s rate #%d > #%d out of bounds\n", 301 ether_sprintf(ni->ni_macaddr), id->id_rateidx, 302 rs->rs_nrates - 1)); 303 return; 304 } 305 306 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 307 i < IEEE80211_RSSADAPT_BKTS; 308 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 309 thridx = i; 310 if (id->id_len <= top) 311 break; 312 } 313 314 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx]; 315 ra->ra_rate_thresh[thridx][id->id_rateidx] = 316 interpolate(master_expavgctl.rc_thresh, last_thr, 317 (id->id_rssi << 8)); 318 319 RSSADAPT_PRINTF(("%s: dst %s rssi %d threshold[%d, %d.%d] %d -> %d\n", 320 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr), 321 id->id_rssi, id->id_len, 322 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) / 2, 323 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) * 5 % 10, 324 last_thr, ra->ra_rate_thresh[thridx][id->id_rateidx])); 325 } 326 327 void 328 ieee80211_rssadapt_raise_rate(struct ieee80211com *ic, 329 struct ieee80211_rssadapt *ra, struct ieee80211_rssdesc *id) 330 { 331 u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr; 332 struct ieee80211_node *ni = id->id_node; 333 struct ieee80211_rateset *rs = &ni->ni_rates; 334 int i, rate, top; 335 #ifdef IEEE80211_DEBUG 336 int j; 337 #endif 338 339 ra->ra_nok++; 340 341 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval)) 342 return; 343 344 for (i = 0, top = IEEE80211_RSSADAPT_BKT0; 345 i < IEEE80211_RSSADAPT_BKTS; 346 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) { 347 thrs = &ra->ra_rate_thresh[i]; 348 if (id->id_len <= top) 349 break; 350 } 351 352 if (id->id_rateidx + 1 < rs->rs_nrates && 353 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) { 354 rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL); 355 356 RSSADAPT_PRINTF(("%s: threshold[%d, %d.%d] decay %d ", 357 ic->ic_if.if_xname, 358 IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER* i), 359 rate / 2, rate * 5 % 10, (*thrs)[id->id_rateidx + 1])); 360 oldthr = (*thrs)[id->id_rateidx + 1]; 361 if ((*thrs)[id->id_rateidx] == 0) 362 newthr = ra->ra_avg_rssi; 363 else 364 newthr = (*thrs)[id->id_rateidx]; 365 (*thrs)[id->id_rateidx + 1] = 366 interpolate(master_expavgctl.rc_decay, oldthr, newthr); 367 368 RSSADAPT_PRINTF(("-> %d\n", (*thrs)[id->id_rateidx + 1])); 369 } 370 371 #ifdef IEEE80211_DEBUG 372 if (RSSADAPT_DO_PRINT()) { 373 printf("%s: dst %s thresholds\n", ic->ic_if.if_xname, 374 ether_sprintf(ni->ni_macaddr)); 375 for (i = 0; i < IEEE80211_RSSADAPT_BKTS; i++) { 376 printf("%d-byte", IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER * i)); 377 for (j = 0; j < rs->rs_nrates; j++) { 378 rate = (rs->rs_rates[j] & IEEE80211_RATE_VAL); 379 printf(", T[%d.%d] = %d", rate / 2, 380 rate * 5 % 10, ra->ra_rate_thresh[i][j]); 381 } 382 printf("\n"); 383 } 384 } 385 #endif /* IEEE80211_DEBUG */ 386 } 387