Home | History | Annotate | Line # | Download | only in net80211
ieee80211_rssadapt.c revision 1.7
      1 /* $NetBSD: ieee80211_rssadapt.c,v 1.7 2004/05/25 04:33:59 atatat 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 	IEEE80211_DPRINTF(("%s: t = %d, nodenum = %d, rnodenum = %d\n",
     98 	    __func__, t, node.sysctl_num, rnode->sysctl_num));
     99 
    100 	if (t < 0 || t > 2)
    101 		return (EINVAL);
    102 	*(int*)rnode->sysctl_data = t;
    103 
    104 	return (0);
    105 }
    106 #endif /* IEEE80211_DEBUG */
    107 
    108 /* TBD factor with sysctl_ath_verify, sysctl_ieee80211_verify. */
    109 static int
    110 sysctl_ieee80211_rssadapt_expavgctl(SYSCTLFN_ARGS)
    111 {
    112 	struct ieee80211_rssadapt_expavgctl rc;
    113 	int error;
    114 	struct sysctlnode node;
    115 
    116 	node = *rnode;
    117 	rc = *(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data;
    118 	node.sysctl_data = &rc;
    119 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    120 	if (error || newp == NULL)
    121 		return (error);
    122 
    123 	IEEE80211_DPRINTF(("%s: decay = %d/%d, thresh = %d/%d, "
    124 	    "avgrssi = %d/%d, nodenum = %d, rnodenum = %d\n",
    125 	    __func__, rc.rc_decay_old, rc.rc_decay_denom,
    126 	    rc.rc_thresh_old, rc.rc_thresh_denom,
    127 	    rc.rc_avgrssi_old, rc.rc_avgrssi_denom,
    128 	    node.sysctl_num, rnode->sysctl_num));
    129 
    130 	if (rc.rc_decay_old < 0 ||
    131 	    rc.rc_decay_denom < rc.rc_decay_old)
    132 		return (EINVAL);
    133 
    134 	if (rc.rc_thresh_old < 0 ||
    135 	    rc.rc_thresh_denom < rc.rc_thresh_old)
    136 		return (EINVAL);
    137 
    138 	if (rc.rc_avgrssi_old < 0 ||
    139 	    rc.rc_avgrssi_denom < rc.rc_avgrssi_old)
    140 		return (EINVAL);
    141 
    142 	*(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data = rc;
    143 
    144 	return (0);
    145 }
    146 
    147 /*
    148  * Setup sysctl(3) MIB, net.ieee80211.*
    149  *
    150  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
    151  */
    152 SYSCTL_SETUP(sysctl_ieee80211_rssadapt,
    153     "sysctl ieee80211 rssadapt subtree setup")
    154 {
    155 	int rc;
    156 	struct sysctlnode *node;
    157 
    158 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    159 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
    160 	    NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
    161 		goto err;
    162 
    163 	if ((rc = sysctl_createv(clog, 0, &node, &node,
    164 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "link", NULL,
    165 	    NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
    166 		goto err;
    167 
    168 	if ((rc = sysctl_createv(clog, 0, &node, &node,
    169 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211", NULL,
    170 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    171 		goto err;
    172 
    173 	if ((rc = sysctl_createv(clog, 0, &node, &node,
    174 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "rssadapt",
    175 	    SYSCTL_DESCR("Received Signal Strength adaptation controls"),
    176 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    177 		goto err;
    178 
    179 #ifdef IEEE80211_DEBUG
    180 	/* control debugging printfs */
    181 	if ((rc = sysctl_createv(clog, 0, &node, NULL,
    182 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
    183 	    SYSCTL_DESCR("Enable RSS adaptation debugging output"),
    184 	    sysctl_ieee80211_rssadapt_debug, 0, &ieee80211_rssadapt_debug, 0,
    185 	    CTL_CREATE, CTL_EOL)) != 0)
    186 		goto err;
    187 #endif /* IEEE80211_DEBUG */
    188 
    189 	/* control rate of decay for exponential averages */
    190 	if ((rc = sysctl_createv(clog, 0, &node, NULL,
    191 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRUCT,
    192 	    "expavgctl", SYSCTL_DESCR("RSS exponential averaging control"),
    193 	    sysctl_ieee80211_rssadapt_expavgctl, 0,
    194 	    &master_expavgctl, sizeof(master_expavgctl), CTL_CREATE,
    195 	    CTL_EOL)) != 0)
    196 		goto err;
    197 
    198 	return;
    199 err:
    200 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    201 }
    202 #endif /* __NetBSD__ */
    203 
    204 int
    205 ieee80211_rssadapt_choose(struct ieee80211_rssadapt *ra,
    206     struct ieee80211_rateset *rs, struct ieee80211_frame *wh, u_int len,
    207     int fixed_rate, const char *dvname, int do_not_adapt)
    208 {
    209 	u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
    210 	int flags = 0, i, rateidx = 0, thridx, top;
    211 
    212 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
    213 		flags |= IEEE80211_RATE_BASIC;
    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 (len <= top)
    220 			break;
    221 	}
    222 
    223 	thrs = &ra->ra_rate_thresh[thridx];
    224 
    225 	if (fixed_rate != -1) {
    226 		if ((rs->rs_rates[fixed_rate] & flags) == flags) {
    227 			rateidx = fixed_rate;
    228 			goto out;
    229 		}
    230 		flags |= IEEE80211_RATE_BASIC;
    231 		i = fixed_rate;
    232 	} else
    233 		i = rs->rs_nrates;
    234 
    235 	while (--i >= 0) {
    236 		rateidx = i;
    237 		if ((rs->rs_rates[i] & flags) != flags)
    238 			continue;
    239 		if (do_not_adapt)
    240 			break;
    241 		if ((*thrs)[i] < ra->ra_avg_rssi)
    242 			break;
    243 	}
    244 
    245 out:
    246 #ifdef IEEE80211_DEBUG
    247 	if (ieee80211_rssadapt_debug && dvname != NULL) {
    248 		printf("%s: dst %s threshold[%d, %d.%d] %d < %d\n",
    249 		    dvname, ether_sprintf(wh->i_addr1), len,
    250 		    (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) / 2,
    251 		    (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
    252 		    (*thrs)[rateidx], ra->ra_avg_rssi);
    253 	}
    254 #endif /* IEEE80211_DEBUG */
    255 	return rateidx;
    256 }
    257 
    258 void
    259 ieee80211_rssadapt_updatestats(struct ieee80211_rssadapt *ra)
    260 {
    261 	long interval;
    262 
    263 	ra->ra_pktrate =
    264 	    (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
    265 	ra->ra_nfail = ra->ra_nok = 0;
    266 
    267 	/* a node is eligible for its rate to be raised every 1/10 to 10
    268 	 * seconds, more eligible in proportion to recent packet rates.
    269 	 */
    270 	interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
    271 	ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
    272 	ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
    273 }
    274 
    275 void
    276 ieee80211_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
    277     struct ieee80211_rssadapt *ra, int rssi)
    278 {
    279 #ifdef IEEE80211_DEBUG
    280 	int last_avg_rssi = ra->ra_avg_rssi;
    281 #endif
    282 
    283 	ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
    284 	                              ra->ra_avg_rssi, (rssi << 8));
    285 
    286 	RSSADAPT_PRINTF(("%s: src %s rssi %d avg %d -> %d\n",
    287 	    ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
    288 	    rssi, last_avg_rssi, ra->ra_avg_rssi));
    289 }
    290 
    291 /*
    292  * Adapt the data rate to suit the conditions.  When a transmitted
    293  * packet is dropped after IEEE80211_RSSADAPT_RETRY_LIMIT retransmissions,
    294  * raise the RSS threshold for transmitting packets of similar length at
    295  * the same data rate.
    296  */
    297 void
    298 ieee80211_rssadapt_lower_rate(struct ieee80211com *ic,
    299     struct ieee80211_node *ni, struct ieee80211_rssadapt *ra,
    300     struct ieee80211_rssdesc *id)
    301 {
    302 	struct ieee80211_rateset *rs = &ni->ni_rates;
    303 	u_int16_t last_thr;
    304 	u_int i, thridx, top;
    305 
    306 	ra->ra_nfail++;
    307 
    308 	if (id->id_rateidx >= rs->rs_nrates) {
    309 		RSSADAPT_PRINTF(("ieee80211_rssadapt_lower_rate: "
    310 		    "%s rate #%d > #%d out of bounds\n",
    311 		    ether_sprintf(ni->ni_macaddr), id->id_rateidx,
    312 		        rs->rs_nrates - 1));
    313 		return;
    314 	}
    315 
    316 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    317 	     i < IEEE80211_RSSADAPT_BKTS;
    318 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    319 		thridx = i;
    320 		if (id->id_len <= top)
    321 			break;
    322 	}
    323 
    324 	last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
    325 	ra->ra_rate_thresh[thridx][id->id_rateidx] =
    326 	    interpolate(master_expavgctl.rc_thresh, last_thr,
    327 	                (id->id_rssi << 8));
    328 
    329 	RSSADAPT_PRINTF(("%s: dst %s rssi %d threshold[%d, %d.%d] %d -> %d\n",
    330 	    ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
    331 	    id->id_rssi, id->id_len,
    332 	    (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) / 2,
    333 	    (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
    334 	    last_thr, ra->ra_rate_thresh[thridx][id->id_rateidx]));
    335 }
    336 
    337 void
    338 ieee80211_rssadapt_raise_rate(struct ieee80211com *ic,
    339     struct ieee80211_rssadapt *ra, struct ieee80211_rssdesc *id)
    340 {
    341 	u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
    342 	struct ieee80211_node *ni = id->id_node;
    343 	struct ieee80211_rateset *rs = &ni->ni_rates;
    344 	int i, rate, top;
    345 #ifdef IEEE80211_DEBUG
    346 	int j;
    347 #endif
    348 
    349 	ra->ra_nok++;
    350 
    351 	if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
    352 		return;
    353 
    354 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    355 	     i < IEEE80211_RSSADAPT_BKTS;
    356 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    357 		thrs = &ra->ra_rate_thresh[i];
    358 		if (id->id_len <= top)
    359 			break;
    360 	}
    361 
    362 	if (id->id_rateidx + 1 < rs->rs_nrates &&
    363 	    (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
    364 		rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
    365 
    366 		RSSADAPT_PRINTF(("%s: threshold[%d, %d.%d] decay %d ",
    367 		    ic->ic_if.if_xname,
    368 		    IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER* i),
    369 		    rate / 2, rate * 5 % 10, (*thrs)[id->id_rateidx + 1]));
    370 		oldthr = (*thrs)[id->id_rateidx + 1];
    371 		if ((*thrs)[id->id_rateidx] == 0)
    372 			newthr = ra->ra_avg_rssi;
    373 		else
    374 			newthr = (*thrs)[id->id_rateidx];
    375 		(*thrs)[id->id_rateidx + 1] =
    376 		    interpolate(master_expavgctl.rc_decay, oldthr, newthr);
    377 
    378 		RSSADAPT_PRINTF(("-> %d\n", (*thrs)[id->id_rateidx + 1]));
    379 	}
    380 
    381 #ifdef IEEE80211_DEBUG
    382 	if (RSSADAPT_DO_PRINT()) {
    383 		printf("%s: dst %s thresholds\n", ic->ic_if.if_xname,
    384 		    ether_sprintf(ni->ni_macaddr));
    385 		for (i = 0; i < IEEE80211_RSSADAPT_BKTS; i++) {
    386 			printf("%d-byte", IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER * i));
    387 			for (j = 0; j < rs->rs_nrates; j++) {
    388 				rate = (rs->rs_rates[j] & IEEE80211_RATE_VAL);
    389 				printf(", T[%d.%d] = %d", rate / 2,
    390 				    rate * 5 % 10, ra->ra_rate_thresh[i][j]);
    391 			}
    392 			printf("\n");
    393 		}
    394 	}
    395 #endif /* IEEE80211_DEBUG */
    396 }
    397