1 /* $NetBSD: ieee80211_rssadapt.c,v 1.3 2004/03/17 17:00:34 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
36 #include <net/if.h>
37 #include <net/if_media.h>
38 #include <net/if_ether.h>
39
40 #include <net80211/ieee80211_var.h>
41 #include <net80211/ieee80211.h>
42 #include <net80211/ieee80211_compat.h>
43 #include <net80211/ieee80211_rssadapt.h>
44
45 #ifdef interpolate
46 #undef interpolate
47 #endif
48 #define interpolate(parm, old, new) ((parm##_old * (old) + \
49 (parm##_denom - parm##_old) * (new)) / \
50 parm##_denom)
51
52 #ifdef IEEE80211_DEBUG
53 static struct timeval lastrateadapt; /* time of last rate adaptation msg */
54 static int currssadaptps = 0; /* rate-adaptation msgs this second */
55 static int ieee80211_adaptrate = 4; /* rate-adaptation max msgs/sec */
56
57 #define RSSADAPT_DO_PRINT() \
58 ((ieee80211_debug > 0) && \
59 ppsratecheck(&lastrateadapt, &currssadaptps, ieee80211_adaptrate))
60 #define RSSADAPT_PRINTF(X) \
61 if (RSSADAPT_DO_PRINT()) \
62 printf X
63
64 #else
65 #define RSSADAPT_DO_PRINT() (0)
66 #define RSSADAPT_PRINTF(X)
67 #endif
68
69 struct ieee80211_rssadapt_expavgctl master_expavgctl = {
70 rc_decay_denom : 16,
71 rc_decay_old : 15,
72 rc_thresh_denom : 8,
73 rc_thresh_old : 4,
74 rc_avgrssi_denom : 8,
75 rc_avgrssi_old : 4
76 };
77
78 int
79 ieee80211_rssadapt_choose(struct ieee80211_rssadapt *ra,
80 struct ieee80211_rateset *rs, struct ieee80211_frame *wh, u_int len,
81 int fixed_rate, const char *dvname, int do_not_adapt)
82 {
83 u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
84 int flags = 0, i, rateidx = 0, thridx, top;
85
86 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
87 flags |= IEEE80211_RATE_BASIC;
88
89 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
90 i < IEEE80211_RSSADAPT_BKTS;
91 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
92 thridx = i;
93 if (len <= top)
94 break;
95 }
96
97 thrs = &ra->ra_rate_thresh[thridx];
98
99 if (fixed_rate != -1) {
100 if ((rs->rs_rates[fixed_rate] & flags) == flags) {
101 rateidx = fixed_rate;
102 goto out;
103 }
104 flags |= IEEE80211_RATE_BASIC;
105 i = fixed_rate;
106 } else
107 i = rs->rs_nrates;
108
109 while (--i >= 0) {
110 rateidx = i;
111 if ((rs->rs_rates[i] & flags) != flags)
112 continue;
113 if (do_not_adapt)
114 break;
115 if ((*thrs)[i] < ra->ra_avg_rssi)
116 break;
117 }
118
119 out:
120 if (dvname != NULL) {
121 printf("%s: dst %s threshold[%d, %d.%d] %d < %d\n",
122 dvname, ether_sprintf(wh->i_addr1), len,
123 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) / 2,
124 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
125 (*thrs)[rateidx], ra->ra_avg_rssi);
126 }
127 return rateidx;
128 }
129
130 void
131 ieee80211_rssadapt_updatestats(struct ieee80211_rssadapt *ra)
132 {
133 long interval;
134
135 ra->ra_pktrate =
136 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
137 ra->ra_nfail = ra->ra_nok = 0;
138
139 /* a node is eligible for its rate to be raised every 1/10 to 10
140 * seconds, more eligible in proportion to recent packet rates.
141 */
142 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
143 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
144 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
145 }
146
147 void
148 ieee80211_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
149 struct ieee80211_rssadapt *ra, int rssi)
150 {
151 #ifdef IEEE80211_DEBUG
152 int last_avg_rssi = ra->ra_avg_rssi;
153 #endif
154
155 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
156 ra->ra_avg_rssi, (rssi << 8));
157
158 RSSADAPT_PRINTF(("%s: src %s rssi %d avg %d -> %d\n",
159 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
160 rssi, last_avg_rssi, ra->ra_avg_rssi));
161 }
162
163 /*
164 * Adapt the data rate to suit the conditions. When a transmitted
165 * packet is dropped after IEEE80211_RSSADAPT_RETRY_LIMIT retransmissions,
166 * raise the RSS threshold for transmitting packets of similar length at
167 * the same data rate.
168 */
169 void
170 ieee80211_rssadapt_lower_rate(struct ieee80211com *ic,
171 struct ieee80211_node *ni, struct ieee80211_rssadapt *ra,
172 struct ieee80211_rssdesc *id)
173 {
174 struct ieee80211_rateset *rs = &ni->ni_rates;
175 u_int16_t last_thr;
176 u_int i, thridx, top;
177
178 ra->ra_nok++;
179
180 if (id->id_rateidx >= rs->rs_nrates) {
181 RSSADAPT_PRINTF(("ieee80211_rssadapt_lower_rate: "
182 "%s rate #%d > #%d out of bounds\n",
183 ether_sprintf(ni->ni_macaddr), id->id_rateidx,
184 rs->rs_nrates - 1));
185 return;
186 }
187
188 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
189 i < IEEE80211_RSSADAPT_BKTS;
190 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
191 thridx = i;
192 if (id->id_len <= top)
193 break;
194 }
195
196 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
197 ra->ra_rate_thresh[thridx][id->id_rateidx] =
198 interpolate(master_expavgctl.rc_thresh, last_thr,
199 (id->id_rssi << 8));
200
201 RSSADAPT_PRINTF(("%s: dst %s rssi %d threshold[%d, %d.%d] %d -> %d\n",
202 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr),
203 id->id_rssi, id->id_len,
204 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) / 2,
205 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
206 last_thr, ra->ra_rate_thresh[thridx][id->id_rateidx]));
207 }
208
209 void
210 ieee80211_rssadapt_raise_rate(struct ieee80211com *ic,
211 struct ieee80211_rssadapt *ra, struct ieee80211_rssdesc *id)
212 {
213 u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
214 struct ieee80211_node *ni = id->id_node;
215 struct ieee80211_rateset *rs = &ni->ni_rates;
216 int i, rate, top;
217 #ifdef IEEE80211_DEBUG
218 int j;
219 #endif
220
221 ra->ra_nfail++;
222
223 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
224 return;
225
226 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
227 i < IEEE80211_RSSADAPT_BKTS;
228 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
229 thrs = &ra->ra_rate_thresh[i];
230 if (id->id_len <= top)
231 break;
232 }
233
234 if (id->id_rateidx + 1 < rs->rs_nrates &&
235 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
236 rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
237
238 RSSADAPT_PRINTF(("%s: threshold[%d, %d.%d] decay %d ",
239 ic->ic_if.if_xname,
240 IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER* i),
241 rate / 2, rate * 5 % 10, (*thrs)[id->id_rateidx + 1]));
242 oldthr = (*thrs)[id->id_rateidx + 1];
243 if ((*thrs)[id->id_rateidx] == 0)
244 newthr = ra->ra_avg_rssi;
245 else
246 newthr = (*thrs)[id->id_rateidx];
247 (*thrs)[id->id_rateidx + 1] =
248 interpolate(master_expavgctl.rc_decay, oldthr, newthr);
249
250 RSSADAPT_PRINTF(("-> %d\n", (*thrs)[id->id_rateidx + 1]));
251 }
252
253 #ifdef IEEE80211_DEBUG
254 if (RSSADAPT_DO_PRINT()) {
255 printf("%s: dst %s thresholds\n", ic->ic_if.if_xname,
256 ether_sprintf(ni->ni_macaddr));
257 for (i = 0; i < IEEE80211_RSSADAPT_BKTS; i++) {
258 printf("%d-byte", IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER * i));
259 for (j = 0; j < rs->rs_nrates; j++) {
260 rate = (rs->rs_rates[j] & IEEE80211_RATE_VAL);
261 printf(", T[%d.%d] = %d", rate / 2,
262 rate * 5 % 10, ra->ra_rate_thresh[i][j]);
263 }
264 printf("\n");
265 }
266 }
267 #endif /* IEEE80211_DEBUG */
268 }
Cache object: d18d9930992e314c6dc21b2dc3f01879
|