2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
4 * Copyright (c) 2003, 2004 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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
33 #include <sys/param.h>
34 #include <sys/sockio.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
40 #include <net/if_arp.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_types.h>
46 #include <net80211/ieee80211_var.h>
48 #include <dev/ral/if_ralrate.h>
53 #define interpolate(parm, old, new) ((parm##_old * (old) + \
54 (parm##_denom - parm##_old) * (new)) / \
57 static struct ral_rssadapt_expavgctl master_expavgctl = {
67 ral_rssadapt_choose(struct ral_rssadapt *ra, struct ieee80211_rateset *rs,
68 struct ieee80211_frame *wh, u_int len, const char *dvname, int do_not_adapt)
70 u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
71 int flags = 0, i, rateidx = 0, thridx, top;
73 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
74 flags |= IEEE80211_RATE_BASIC;
76 for (i = 0, top = RAL_RSSADAPT_BKT0;
77 i < RAL_RSSADAPT_BKTS;
78 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
84 thrs = &ra->ra_rate_thresh[thridx];
89 if ((rs->rs_rates[i] & flags) != flags)
93 if ((*thrs)[i] < ra->ra_avg_rssi)
101 ral_rssadapt_updatestats(struct ral_rssadapt *ra)
106 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
107 ra->ra_nfail = ra->ra_nok = 0;
109 /* a node is eligible for its rate to be raised every 1/10 to 10
110 * seconds, more eligible in proportion to recent packet rates.
112 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
113 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
114 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
118 ral_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
119 struct ral_rssadapt *ra, int rssi)
121 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
122 ra->ra_avg_rssi, (rssi << 8));
126 * Adapt the data rate to suit the conditions. When a transmitted
127 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
128 * raise the RSS threshold for transmitting packets of similar length at
129 * the same data rate.
132 ral_rssadapt_lower_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
133 struct ral_rssadapt *ra, struct ral_rssdesc *id)
135 struct ieee80211_rateset *rs = &ni->ni_rates;
137 u_int i, thridx, top;
141 if (id->id_rateidx >= rs->rs_nrates)
144 for (i = 0, top = RAL_RSSADAPT_BKT0;
145 i < RAL_RSSADAPT_BKTS;
146 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
148 if (id->id_len <= top)
152 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
153 ra->ra_rate_thresh[thridx][id->id_rateidx] =
154 interpolate(master_expavgctl.rc_thresh, last_thr,
159 ral_rssadapt_raise_rate(struct ieee80211com *ic, struct ral_rssadapt *ra,
160 struct ral_rssdesc *id)
162 u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
163 struct ieee80211_node *ni = id->id_node;
164 struct ieee80211_rateset *rs = &ni->ni_rates;
169 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
172 for (i = 0, top = RAL_RSSADAPT_BKT0;
173 i < RAL_RSSADAPT_BKTS;
174 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
175 thrs = &ra->ra_rate_thresh[i];
176 if (id->id_len <= top)
180 if (id->id_rateidx + 1 < rs->rs_nrates &&
181 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
182 rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
184 oldthr = (*thrs)[id->id_rateidx + 1];
185 if ((*thrs)[id->id_rateidx] == 0)
186 newthr = ra->ra_avg_rssi;
188 newthr = (*thrs)[id->id_rateidx];
189 (*thrs)[id->id_rateidx + 1] =
190 interpolate(master_expavgctl.rc_decay, oldthr, newthr);