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