1 /* $NetBSD: ieee80211_rssadapt.c,v 1.14 2006/08/30 15:40:41 christos 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/cdefs.h>
33 #ifdef __NetBSD__
34 __KERNEL_RCSID(0, "$NetBSD: ieee80211_rssadapt.c,v 1.14 2006/08/30 15:40:41 christos Exp $");
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h> /* for hz */
40 #include <sys/sysctl.h>
41
42 #include <net/if.h>
43 #include <net/if_media.h>
44 #include <net/if_ether.h>
45
46 #include <net80211/ieee80211_netbsd.h>
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211.h>
49 #include <net80211/ieee80211_rssadapt.h>
50
51 #ifdef interpolate
52 #undef interpolate
53 #endif
54 #define interpolate(parm, old, new) ((parm##_old * (old) + \
55 (parm##_denom - parm##_old) * (new)) / \
56 parm##_denom)
57
58 #ifdef IEEE80211_DEBUG
59 static struct timeval lastrateadapt; /* time of last rate adaptation msg */
60 static int currssadaptps = 0; /* rate-adaptation msgs this second */
61 static int ieee80211_adaptrate = 4; /* rate-adaptation max msgs/sec */
62
63 #define RSSADAPT_DO_PRINT() \
64 ((ieee80211_rssadapt_debug > 0) && \
65 ppsratecheck(&lastrateadapt, &currssadaptps, ieee80211_adaptrate))
66 #define RSSADAPT_PRINTF(X) \
67 if (RSSADAPT_DO_PRINT()) \
68 printf X
69
70 int ieee80211_rssadapt_debug = 0;
71
72 #else
73 #define RSSADAPT_DO_PRINT() (0)
74 #define RSSADAPT_PRINTF(X)
75 #endif
76
77 static struct ieee80211_rssadapt_expavgctl master_expavgctl = {
78 rc_decay_denom : 16,
79 rc_decay_old : 15,
80 rc_thresh_denom : 8,
81 rc_thresh_old : 4,
82 rc_avgrssi_denom : 8,
83 rc_avgrssi_old : 4
84 };
85
86 #ifdef __NetBSD__
87 #ifdef IEEE80211_DEBUG
88 /* TBD factor with sysctl_ath_verify, sysctl_ieee80211_verify. */
89 static int
90 sysctl_ieee80211_rssadapt_debug(SYSCTLFN_ARGS)
91 {
92 int error, t;
93 struct sysctlnode node;
94
95 node = *rnode;
96 t = *(int*)rnode->sysctl_data;
97 node.sysctl_data = &t;
98 error = sysctl_lookup(SYSCTLFN_CALL(&node));
99 if (error || newp == NULL)
100 return (error);
101
102 if (t < 0 || t > 2)
103 return (EINVAL);
104 *(int*)rnode->sysctl_data = t;
105
106 return (0);
107 }
108 #endif /* IEEE80211_DEBUG */
109
110 /* TBD factor with sysctl_ath_verify, sysctl_ieee80211_verify. */
111 static int
112 sysctl_ieee80211_rssadapt_expavgctl(SYSCTLFN_ARGS)
113 {
114 struct ieee80211_rssadapt_expavgctl rc;
115 int error;
116 struct sysctlnode node;
117
118 node = *rnode;
119 rc = *(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data;
120 node.sysctl_data = &rc;
121 error = sysctl_lookup(SYSCTLFN_CALL(&node));
122 if (error || newp == NULL)
123 return (error);
124
125 if (/* rc.rc_decay_old < 0 || */
126 rc.rc_decay_denom < rc.rc_decay_old)
127 return (EINVAL);
128
129 if (/* rc.rc_thresh_old < 0 || */
130 rc.rc_thresh_denom < rc.rc_thresh_old)
131 return (EINVAL);
132
133 if (/* rc.rc_avgrssi_old < 0 || */
134 rc.rc_avgrssi_denom < rc.rc_avgrssi_old)
135 return (EINVAL);
136
137 *(struct ieee80211_rssadapt_expavgctl *)rnode->sysctl_data = rc;
138
139 return (0);
140 }
141
142 /*
143 * Setup sysctl(3) MIB, net.ieee80211.*
144 *
145 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
146 */
147 SYSCTL_SETUP(sysctl_ieee80211_rssadapt,
148 "sysctl ieee80211 rssadapt subtree setup")
149 {
150 int rc;
151 const struct sysctlnode *node;
152
153 if ((rc = sysctl_createv(clog, 0, NULL, &node,
154 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
155 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
156 goto err;
157
158 if ((rc = sysctl_createv(clog, 0, &node, &node,
159 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link", NULL,
160 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
161 goto err;
162
163 if ((rc = sysctl_createv(clog, 0, &node, &node,
164 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211", NULL,
165 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
166 goto err;
167
168 if ((rc = sysctl_createv(clog, 0, &node, &node,
169 CTLFLAG_PERMANENT, CTLTYPE_NODE, "rssadapt",
170 SYSCTL_DESCR("Received Signal Strength adaptation controls"),
171 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
172 goto err;
173
174 #ifdef IEEE80211_DEBUG
175 /* control debugging printfs */
176 if ((rc = sysctl_createv(clog, 0, &node, NULL,
177 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
178 SYSCTL_DESCR("Enable RSS adaptation debugging output"),
179 sysctl_ieee80211_rssadapt_debug, 0, &ieee80211_rssadapt_debug, 0,
180 CTL_CREATE, CTL_EOL)) != 0)
181 goto err;
182 #endif /* IEEE80211_DEBUG */
183
184 /* control rate of decay for exponential averages */
185 if ((rc = sysctl_createv(clog, 0, &node, NULL,
186 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRUCT,
187 "expavgctl", SYSCTL_DESCR("RSS exponential averaging control"),
188 sysctl_ieee80211_rssadapt_expavgctl, 0,
189 &master_expavgctl, sizeof(master_expavgctl), CTL_CREATE,
190 CTL_EOL)) != 0)
191 goto err;
192
193 return;
194 err:
195 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
196 }
197 #endif /* __NetBSD__ */
198
199 int
200 ieee80211_rssadapt_choose(struct ieee80211_rssadapt *ra,
201 struct ieee80211_rateset *rs, struct ieee80211_frame *wh, u_int len,
202 int fixed_rate, const char *dvname, int do_not_adapt)
203 {
204 u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
205 int flags = 0, i, rateidx = 0, thridx, top;
206
207 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
208 flags |= IEEE80211_RATE_BASIC;
209
210 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
211 i < IEEE80211_RSSADAPT_BKTS;
212 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
213 thridx = i;
214 if (len <= top)
215 break;
216 }
217
218 thrs = &ra->ra_rate_thresh[thridx];
219
220 if (fixed_rate != -1) {
221 if ((rs->rs_rates[fixed_rate] & flags) == flags) {
222 rateidx = fixed_rate;
223 goto out;
224 }
225 flags |= IEEE80211_RATE_BASIC;
226 i = fixed_rate;
227 } else
228 i = rs->rs_nrates;
229
230 while (--i >= 0) {
231 rateidx = i;
232 if ((rs->rs_rates[i] & flags) != flags)
233 continue;
234 if (do_not_adapt)
235 break;
236 if ((*thrs)[i] < ra->ra_avg_rssi)
237 break;
238 }
239
240 out:
241 #ifdef IEEE80211_DEBUG
242 if (ieee80211_rssadapt_debug && dvname != NULL) {
243 printf("%s: dst %s threshold[%d, %d.%d] %d < %d\n",
244 dvname, ether_sprintf(wh->i_addr1), len,
245 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) / 2,
246 (rs->rs_rates[rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
247 (*thrs)[rateidx], ra->ra_avg_rssi);
248 }
249 #endif /* IEEE80211_DEBUG */
250 return rateidx;
251 }
252
253 void
254 ieee80211_rssadapt_updatestats(struct ieee80211_rssadapt *ra)
255 {
256 long interval;
257
258 ra->ra_pktrate =
259 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
260 ra->ra_nfail = ra->ra_nok = 0;
261
262 /* a node is eligible for its rate to be raised every 1/10 to 10
263 * seconds, more eligible in proportion to recent packet rates.
264 */
265 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
266 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
267 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
268 }
269
270 void
271 ieee80211_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
272 struct ieee80211_rssadapt *ra, int rssi)
273 {
274 #ifdef IEEE80211_DEBUG
275 int last_avg_rssi = ra->ra_avg_rssi;
276 #endif
277
278 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
279 ra->ra_avg_rssi, (rssi << 8));
280
281 RSSADAPT_PRINTF(("%s: src %s rssi %d avg %d -> %d\n",
282 ic->ic_ifp->if_xname, ether_sprintf(ni->ni_macaddr),
283 rssi, last_avg_rssi, ra->ra_avg_rssi));
284 }
285
286 /*
287 * Adapt the data rate to suit the conditions. When a transmitted
288 * packet is dropped after IEEE80211_RSSADAPT_RETRY_LIMIT retransmissions,
289 * raise the RSS threshold for transmitting packets of similar length at
290 * the same data rate.
291 */
292 void
293 ieee80211_rssadapt_lower_rate(struct ieee80211com *ic,
294 struct ieee80211_node *ni, struct ieee80211_rssadapt *ra,
295 struct ieee80211_rssdesc *id)
296 {
297 struct ieee80211_rateset *rs = &ni->ni_rates;
298 u_int16_t last_thr;
299 u_int i, thridx, top;
300
301 ra->ra_nfail++;
302
303 if (id->id_rateidx >= rs->rs_nrates) {
304 RSSADAPT_PRINTF(("ieee80211_rssadapt_lower_rate: "
305 "%s rate #%d > #%d out of bounds\n",
306 ether_sprintf(ni->ni_macaddr), id->id_rateidx,
307 rs->rs_nrates - 1));
308 return;
309 }
310
311 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
312 i < IEEE80211_RSSADAPT_BKTS;
313 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
314 thridx = i;
315 if (id->id_len <= top)
316 break;
317 }
318
319 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
320 ra->ra_rate_thresh[thridx][id->id_rateidx] =
321 interpolate(master_expavgctl.rc_thresh, last_thr,
322 (id->id_rssi << 8));
323
324 RSSADAPT_PRINTF(("%s: dst %s rssi %d threshold[%d, %d.%d] %d -> %d\n",
325 ic->ic_ifp->if_xname, ether_sprintf(ni->ni_macaddr),
326 id->id_rssi, id->id_len,
327 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) / 2,
328 (rs->rs_rates[id->id_rateidx] & IEEE80211_RATE_VAL) * 5 % 10,
329 last_thr, ra->ra_rate_thresh[thridx][id->id_rateidx]));
330 }
331
332 void
333 ieee80211_rssadapt_raise_rate(struct ieee80211com *ic,
334 struct ieee80211_rssadapt *ra, struct ieee80211_rssdesc *id)
335 {
336 u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
337 struct ieee80211_node *ni = id->id_node;
338 struct ieee80211_rateset *rs = &ni->ni_rates;
339 int i, rate, top;
340 #ifdef IEEE80211_DEBUG
341 int j;
342 #endif
343
344 ra->ra_nok++;
345
346 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
347 return;
348
349 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
350 i < IEEE80211_RSSADAPT_BKTS;
351 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
352 thrs = &ra->ra_rate_thresh[i];
353 if (id->id_len <= top)
354 break;
355 }
356
357 if (id->id_rateidx + 1 < rs->rs_nrates &&
358 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
359 rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
360
361 RSSADAPT_PRINTF(("%s: threshold[%d, %d.%d] decay %d ",
362 ic->ic_ifp->if_xname,
363 IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER* i),
364 rate / 2, rate * 5 % 10, (*thrs)[id->id_rateidx + 1]));
365 oldthr = (*thrs)[id->id_rateidx + 1];
366 if ((*thrs)[id->id_rateidx] == 0)
367 newthr = ra->ra_avg_rssi;
368 else
369 newthr = (*thrs)[id->id_rateidx];
370 (*thrs)[id->id_rateidx + 1] =
371 interpolate(master_expavgctl.rc_decay, oldthr, newthr);
372
373 RSSADAPT_PRINTF(("-> %d\n", (*thrs)[id->id_rateidx + 1]));
374 }
375
376 #ifdef IEEE80211_DEBUG
377 if (RSSADAPT_DO_PRINT()) {
378 printf("%s: dst %s thresholds\n", ic->ic_ifp->if_xname,
379 ether_sprintf(ni->ni_macaddr));
380 for (i = 0; i < IEEE80211_RSSADAPT_BKTS; i++) {
381 printf("%d-byte", IEEE80211_RSSADAPT_BKT0 << (IEEE80211_RSSADAPT_BKTPOWER * i));
382 for (j = 0; j < rs->rs_nrates; j++) {
383 rate = (rs->rs_rates[j] & IEEE80211_RATE_VAL);
384 printf(", T[%d.%d] = %d", rate / 2,
385 rate * 5 % 10, ra->ra_rate_thresh[i][j]);
386 }
387 printf("\n");
388 }
389 }
390 #endif /* IEEE80211_DEBUG */
391 }
Cache object: 4b3c8ccafe1c01771df9f696206b74fd
|