1 /* $FreeBSD$ */
2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
3 /*-
4 * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
5 * Copyright (c) 2003, 2004 David Young. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. The name of David Young may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
24 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 */
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40
41 #include <net/if.h>
42 #include <net/if_media.h>
43
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_rssadapt.h>
46 #include <net80211/ieee80211_ratectl.h>
47
48 struct rssadapt_expavgctl {
49 /* RSS threshold decay. */
50 u_int rc_decay_denom;
51 u_int rc_decay_old;
52 /* RSS threshold update. */
53 u_int rc_thresh_denom;
54 u_int rc_thresh_old;
55 /* RSS average update. */
56 u_int rc_avgrssi_denom;
57 u_int rc_avgrssi_old;
58 };
59
60 static struct rssadapt_expavgctl master_expavgctl = {
61 .rc_decay_denom = 16,
62 .rc_decay_old = 15,
63 .rc_thresh_denom = 8,
64 .rc_thresh_old = 4,
65 .rc_avgrssi_denom = 8,
66 .rc_avgrssi_old = 4
67 };
68
69 #ifdef interpolate
70 #undef interpolate
71 #endif
72 #define interpolate(parm, old, new) ((parm##_old * (old) + \
73 (parm##_denom - parm##_old) * (new)) / \
74 parm##_denom)
75
76 static void rssadapt_setinterval(const struct ieee80211vap *, int);
77 static void rssadapt_init(struct ieee80211vap *);
78 static void rssadapt_deinit(struct ieee80211vap *);
79 static void rssadapt_updatestats(struct ieee80211_rssadapt_node *);
80 static void rssadapt_node_init(struct ieee80211_node *);
81 static void rssadapt_node_deinit(struct ieee80211_node *);
82 static int rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
83 static void rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
84 static void rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
85 int, int);
86 static void rssadapt_tx_complete(const struct ieee80211vap *,
87 const struct ieee80211_node *, int,
88 void *, void *);
89 static void rssadapt_sysctlattach(struct ieee80211vap *,
90 struct sysctl_ctx_list *, struct sysctl_oid *);
91
92 /* number of references from net80211 layer */
93 static int nrefs = 0;
94
95 static const struct ieee80211_ratectl rssadapt = {
96 .ir_name = "rssadapt",
97 .ir_attach = NULL,
98 .ir_detach = NULL,
99 .ir_init = rssadapt_init,
100 .ir_deinit = rssadapt_deinit,
101 .ir_node_init = rssadapt_node_init,
102 .ir_node_deinit = rssadapt_node_deinit,
103 .ir_rate = rssadapt_rate,
104 .ir_tx_complete = rssadapt_tx_complete,
105 .ir_tx_update = NULL,
106 .ir_setinterval = rssadapt_setinterval,
107 };
108 IEEE80211_RATECTL_MODULE(rssadapt, 1);
109 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
110
111 static void
112 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
113 {
114 struct ieee80211_rssadapt *rs = vap->iv_rs;
115 int t;
116
117 if (msecs < 100)
118 msecs = 100;
119 t = msecs_to_ticks(msecs);
120 rs->interval = (t < 1) ? 1 : t;
121 }
122
123 static void
124 rssadapt_init(struct ieee80211vap *vap)
125 {
126 struct ieee80211_rssadapt *rs;
127
128 KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
129 __func__));
130
131 vap->iv_rs = rs = malloc(sizeof(struct ieee80211_rssadapt),
132 M_80211_RATECTL, M_NOWAIT|M_ZERO);
133 if (rs == NULL) {
134 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
135 return;
136 }
137 rs->vap = vap;
138 rssadapt_setinterval(vap, 500 /* msecs */);
139 rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
140 }
141
142 static void
143 rssadapt_deinit(struct ieee80211vap *vap)
144 {
145 free(vap->iv_rs, M_80211_RATECTL);
146 }
147
148 static void
149 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
150 {
151 long interval;
152
153 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
154 ra->ra_nfail = ra->ra_nok = 0;
155
156 /*
157 * A node is eligible for its rate to be raised every 1/10 to 10
158 * seconds, more eligible in proportion to recent packet rates.
159 */
160 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
161 ra->ra_raise_interval = msecs_to_ticks(interval);
162 }
163
164 static void
165 rssadapt_node_init(struct ieee80211_node *ni)
166 {
167 struct ieee80211_rssadapt_node *ra;
168 struct ieee80211vap *vap = ni->ni_vap;
169 struct ieee80211_rssadapt *rsa = vap->iv_rs;
170 const struct ieee80211_rateset *rs = &ni->ni_rates;
171
172 if (ni->ni_rctls == NULL) {
173 ni->ni_rctls = ra =
174 malloc(sizeof(struct ieee80211_rssadapt_node),
175 M_80211_RATECTL, M_NOWAIT|M_ZERO);
176 if (ra == NULL) {
177 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
178 "structure\n");
179 return;
180 }
181 } else
182 ra = ni->ni_rctls;
183 ra->ra_rs = rsa;
184 ra->ra_rates = *rs;
185 rssadapt_updatestats(ra);
186
187 /* pick initial rate */
188 for (ra->ra_rix = rs->rs_nrates - 1;
189 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
190 ra->ra_rix--)
191 ;
192 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
193 ra->ra_ticks = ticks;
194
195 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
196 "RSSADAPT initial rate %d", ni->ni_txrate);
197 }
198
199 static void
200 rssadapt_node_deinit(struct ieee80211_node *ni)
201 {
202
203 free(ni->ni_rctls, M_80211_RATECTL);
204 }
205
206 static __inline int
207 bucket(int pktlen)
208 {
209 int i, top, thridx;
210
211 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
212 i < IEEE80211_RSSADAPT_BKTS;
213 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
214 thridx = i;
215 if (pktlen <= top)
216 break;
217 }
218 return thridx;
219 }
220
221 static int
222 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
223 {
224 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
225 u_int pktlen = iarg;
226 const struct ieee80211_rateset *rs = &ra->ra_rates;
227 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
228 int rix, rssi;
229
230 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
231 rssadapt_updatestats(ra);
232 ra->ra_ticks = ticks;
233 }
234
235 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
236
237 /* XXX this is average rssi, should be using last value */
238 rssi = ni->ni_ic->ic_node_getrssi(ni);
239 for (rix = rs->rs_nrates-1; rix >= 0; rix--)
240 if ((*thrs)[rix] < (rssi << 8))
241 break;
242 if (rix != ra->ra_rix) {
243 /* update public rate */
244 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
245 ra->ra_rix = rix;
246
247 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
248 "RSSADAPT new rate %d (pktlen %d rssi %d)",
249 ni->ni_txrate, pktlen, rssi);
250 }
251 return rix;
252 }
253
254 /*
255 * Adapt the data rate to suit the conditions. When a transmitted
256 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
257 * raise the RSS threshold for transmitting packets of similar length at
258 * the same data rate.
259 */
260 static void
261 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
262 {
263 uint16_t last_thr;
264 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
265 u_int rix;
266
267 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
268
269 rix = ra->ra_rix;
270 last_thr = (*thrs)[rix];
271 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
272 last_thr, (rssi << 8));
273
274 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
275 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
276 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
277 last_thr, (*thrs)[rix], rssi);
278 }
279
280 static void
281 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
282 {
283 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
284 uint16_t newthr, oldthr;
285 int rix;
286
287 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
288
289 rix = ra->ra_rix;
290 if ((*thrs)[rix + 1] > (*thrs)[rix]) {
291 oldthr = (*thrs)[rix + 1];
292 if ((*thrs)[rix] == 0)
293 newthr = (rssi << 8);
294 else
295 newthr = (*thrs)[rix];
296 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
297 oldthr, newthr);
298
299 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
300 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
301 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
302 oldthr, newthr, rssi);
303
304 ra->ra_last_raise = ticks;
305 }
306 }
307
308 static void
309 rssadapt_tx_complete(const struct ieee80211vap *vap,
310 const struct ieee80211_node *ni, int success, void *arg1, void *arg2)
311 {
312 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
313 int pktlen = *(int *)arg1, rssi = *(int *)arg2;
314
315 if (success) {
316 ra->ra_nok++;
317 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
318 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
319 rssadapt_raise_rate(ra, pktlen, rssi);
320 } else {
321 ra->ra_nfail++;
322 rssadapt_lower_rate(ra, pktlen, rssi);
323 }
324 }
325
326 static int
327 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
328 {
329 struct ieee80211vap *vap = arg1;
330 struct ieee80211_rssadapt *rs = vap->iv_rs;
331 int msecs = ticks_to_msecs(rs->interval);
332 int error;
333
334 error = sysctl_handle_int(oidp, &msecs, 0, req);
335 if (error || !req->newptr)
336 return error;
337 rssadapt_setinterval(vap, msecs);
338 return 0;
339 }
340
341 static void
342 rssadapt_sysctlattach(struct ieee80211vap *vap,
343 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
344 {
345
346 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
347 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
348 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
349 }
Cache object: d2cbf7cb9655e66e5f1d2cec71f01b7e
|