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