Home | History | Annotate | Line # | Download | only in net80211
ieee80211_rssadapt.c revision 1.6
      1 /* $NetBSD: ieee80211_rssadapt.c,v 1.6 2004/05/06 07:11:40 dyoung 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", NULL,
    175 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    176 		goto err;
    177 
    178 #ifdef IEEE80211_DEBUG
    179 	/* control debugging printfs */
    180 	if ((rc = sysctl_createv(clog, 0, &node, NULL,
    181 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug", NULL,
    182 	    sysctl_ieee80211_rssadapt_debug, 0, &ieee80211_rssadapt_debug, 0,
    183 	    CTL_CREATE, CTL_EOL)) != 0)
    184 		goto err;
    185 #endif /* IEEE80211_DEBUG */
    186 
    187 	/* control rate of decay for exponential averages */
    188 	if ((rc = sysctl_createv(clog, 0, &node, NULL,
    189 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRUCT,
    190 	    "expavgctl", NULL, sysctl_ieee80211_rssadapt_expavgctl, 0,
    191 	    &master_expavgctl, sizeof(master_expavgctl), CTL_CREATE,
    192 	    CTL_EOL)) != 0)
    193 		goto err;
    194 
    195 	return;
    196 err:
    197 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    198 }
    199 #endif /* __NetBSD__ */
    200 
    201 int
    202 ieee80211_rssadapt_choose(struct ieee80211_rssadapt *ra,
    203     struct ieee80211_rateset *rs, struct ieee80211_frame *wh, u_int len,
    204     int fixed_rate, const char *dvname, int do_not_adapt)
    205 {
    206 	u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
    207 	int flags = 0, i, rateidx = 0, thridx, top;
    208 
    209 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
    210 		flags |= IEEE80211_RATE_BASIC;
    211 
    212 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    213 	     i < IEEE80211_RSSADAPT_BKTS;
    214 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    215 		thridx = i;
    216 		if (len <= top)
    217 			break;
    218 	}
    219 
    220 	thrs = &ra->ra_rate_thresh[thridx];
    221 
    222 	if (fixed_rate != -1) {
    223 		if ((rs->rs_rates[fixed_rate] & flags) == flags) {
    224 			rateidx = fixed_rate;
    225 			goto out;
    226 		}
    227 		flags |= IEEE80211_RATE_BASIC;
    228 		i = fixed_rate;
    229 	} else
    230 		i = rs->rs_nrates;
    231 
    232 	while (--i >= 0) {
    233 		rateidx = i;
    234 		if ((rs->rs_rates[i] & flags) != flags)
    235 			continue;
    236 		if (do_not_adapt)
    237 			break;
    238 		if ((*thrs)[i] < ra->ra_avg_rssi)
    239 			break;
    240 	}
    241 
    242 out:
    243 #ifdef IEEE80211_DEBUG
    244 	if (ieee80211_rssadapt_debug && dvname != NULL) {
    245 		printf("%s: dst %s threshold[%d, %d.%d] %d < %d\n",
    246 		    dvname, ether_sprintf(wh->i_addr1), len,
    247 		    (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) / 2,
    248 		    (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
    249 		    (*thrs)[rateidx], ra->ra_avg_rssi);
    250 	}
    251 #endif /* IEEE80211_DEBUG */
    252 	return rateidx;
    253 }
    254 
    255 void
    256 ieee80211_rssadapt_updatestats(struct ieee80211_rssadapt *ra)
    257 {
    258 	long interval;
    259 
    260 	ra->ra_pktrate =
    261 	    (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
    262 	ra->ra_nfail = ra->ra_nok = 0;
    263 
    264 	/* a node is eligible for its rate to be raised every 1/10 to 10
    265 	 * seconds, more eligible in proportion to recent packet rates.
    266 	 */
    267 	interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
    268 	ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
    269 	ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
    270 }
    271 
    272 void
    273 ieee80211_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
    274     struct ieee80211_rssadapt *ra, int rssi)
    275 {
    276 #ifdef IEEE80211_DEBUG
    277 	int last_avg_rssi = ra->ra_avg_rssi;
    278 #endif
    279 
    280 	ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
    281 	                              ra->ra_avg_rssi, (rssi << 8));
    282 
    283 	RSSADAPT_PRINTF(("%s: src %s rssi %d avg %d -> %d\n",
    284 	    ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
    285 	    rssi, last_avg_rssi, ra->ra_avg_rssi));
    286 }
    287 
    288 /*
    289  * Adapt the data rate to suit the conditions.  When a transmitted
    290  * packet is dropped after IEEE80211_RSSADAPT_RETRY_LIMIT retransmissions,
    291  * raise the RSS threshold for transmitting packets of similar length at
    292  * the same data rate.
    293  */
    294 void
    295 ieee80211_rssadapt_lower_rate(struct ieee80211com *ic,
    296     struct ieee80211_node *ni, struct ieee80211_rssadapt *ra,
    297     struct ieee80211_rssdesc *id)
    298 {
    299 	struct ieee80211_rateset *rs = &ni->ni_rates;
    300 	u_int16_t last_thr;
    301 	u_int i, thridx, top;
    302 
    303 	ra->ra_nfail++;
    304 
    305 	if (id->id_rateidx >= rs->rs_nrates) {
    306 		RSSADAPT_PRINTF(("ieee80211_rssadapt_lower_rate: "
    307 		    "%s rate #%d > #%d out of bounds\n",
    308 		    ether_sprintf(ni->ni_macaddr), id->id_rateidx,
    309 		        rs->rs_nrates - 1));
    310 		return;
    311 	}
    312 
    313 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    314 	     i < IEEE80211_RSSADAPT_BKTS;
    315 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    316 		thridx = i;
    317 		if (id->id_len <= top)
    318 			break;
    319 	}
    320 
    321 	last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
    322 	ra->ra_rate_thresh[thridx][id->id_rateidx] =
    323 	    interpolate(master_expavgctl.rc_thresh, last_thr,
    324 	                (id->id_rssi << 8));
    325 
    326 	RSSADAPT_PRINTF(("%s: dst %s rssi %d threshold[%d, %d.%d] %d -> %d\n",
    327 	    ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
    328 	    id->id_rssi, id->id_len,
    329 	    (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) / 2,
    330 	    (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
    331 	    last_thr, ra->ra_rate_thresh[thridx][id->id_rateidx]));
    332 }
    333 
    334 void
    335 ieee80211_rssadapt_raise_rate(struct ieee80211com *ic,
    336     struct ieee80211_rssadapt *ra, struct ieee80211_rssdesc *id)
    337 {
    338 	u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
    339 	struct ieee80211_node *ni = id->id_node;
    340 	struct ieee80211_rateset *rs = &ni->ni_rates;
    341 	int i, rate, top;
    342 #ifdef IEEE80211_DEBUG
    343 	int j;
    344 #endif
    345 
    346 	ra->ra_nok++;
    347 
    348 	if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
    349 		return;
    350 
    351 	for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
    352 	     i < IEEE80211_RSSADAPT_BKTS;
    353 	     i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
    354 		thrs = &ra->ra_rate_thresh[i];
    355 		if (id->id_len <= top)
    356 			break;
    357 	}
    358 
    359 	if (id->id_rateidx + 1 < rs->rs_nrates &&
    360 	    (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
    361 		rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
    362 
    363 		RSSADAPT_PRINTF(("%s: threshold[%d, %d.%d] decay %d ",
    364 		    ic->ic_if.if_xname,
    365 		    IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER* i),
    366 		    rate / 2, rate * 5 % 10, (*thrs)[id->id_rateidx + 1]));
    367 		oldthr = (*thrs)[id->id_rateidx + 1];
    368 		if ((*thrs)[id->id_rateidx] == 0)
    369 			newthr = ra->ra_avg_rssi;
    370 		else
    371 			newthr = (*thrs)[id->id_rateidx];
    372 		(*thrs)[id->id_rateidx + 1] =
    373 		    interpolate(master_expavgctl.rc_decay, oldthr, newthr);
    374 
    375 		RSSADAPT_PRINTF(("-> %d\n", (*thrs)[id->id_rateidx + 1]));
    376 	}
    377 
    378 #ifdef IEEE80211_DEBUG
    379 	if (RSSADAPT_DO_PRINT()) {
    380 		printf("%s: dst %s thresholds\n", ic->ic_if.if_xname,
    381 		    ether_sprintf(ni->ni_macaddr));
    382 		for (i = 0; i < IEEE80211_RSSADAPT_BKTS; i++) {
    383 			printf("%d-byte", IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER * i));
    384 			for (j = 0; j < rs->rs_nrates; j++) {
    385 				rate = (rs->rs_rates[j] & IEEE80211_RATE_VAL);
    386 				printf(", T[%d.%d] = %d", rate / 2,
    387 				    rate * 5 % 10, ra->ra_rate_thresh[i][j]);
    388 			}
    389 			printf("\n");
    390 		}
    391 	}
    392 #endif /* IEEE80211_DEBUG */
    393 }
    394