ieee80211_rssadapt.c revision 1.21.16.5 1 /* $NetBSD: ieee80211_rssadapt.c,v 1.21.16.5 2019/06/10 22:09:46 christos 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 <sys/cdefs.h>
36 #ifdef __NetBSD__
37 __KERNEL_RCSID(0, "$NetBSD: ieee80211_rssadapt.c,v 1.21.16.5 2019/06/10 22:09:46 christos Exp $");
38 #endif
39
40 #ifdef _KERNEL_OPT
41 #include "opt_wlan.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #ifdef __NetBSD__
50 /* Why doesn't FreeBSD have this? */
51 #include <sys/sbuf.h>
52 #endif
53 #include <sys/socket.h>
54 #include <sys/sysctl.h>
55
56 #include <net/if.h>
57 #ifdef __FreeBSD__
58 #include <net/if_var.h>
59 #endif
60 #include <net/if_media.h>
61 #ifdef __FreeBSD__
62 #include <net/ethernet.h>
63 #endif
64 #ifdef __NetBSD__
65 #include <net/route.h>
66 #endif
67
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_rssadapt.h>
70 #include <net80211/ieee80211_ratectl.h>
71
72 #ifdef __NetBSD__
73 #undef KASSERT
74 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
75 #endif
76
77 struct rssadapt_expavgctl {
78 /* RSS threshold decay. */
79 u_int rc_decay_denom;
80 u_int rc_decay_old;
81 /* RSS threshold update. */
82 u_int rc_thresh_denom;
83 u_int rc_thresh_old;
84 /* RSS average update. */
85 u_int rc_avgrssi_denom;
86 u_int rc_avgrssi_old;
87 };
88
89 static struct rssadapt_expavgctl master_expavgctl = {
90 .rc_decay_denom = 16,
91 .rc_decay_old = 15,
92 .rc_thresh_denom = 8,
93 .rc_thresh_old = 4,
94 .rc_avgrssi_denom = 8,
95 .rc_avgrssi_old = 4
96 };
97
98 #ifdef interpolate
99 #undef interpolate
100 #endif
101 #define interpolate(parm, old, new) ((parm##_old * (old) + \
102 (parm##_denom - parm##_old) * (new)) / \
103 parm##_denom)
104
105 static void rssadapt_setinterval(const struct ieee80211vap *, int);
106 static void rssadapt_init(struct ieee80211vap *);
107 static void rssadapt_deinit(struct ieee80211vap *);
108 static void rssadapt_updatestats(struct ieee80211_rssadapt_node *);
109 static void rssadapt_node_init(struct ieee80211_node *);
110 static void rssadapt_node_deinit(struct ieee80211_node *);
111 static int rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
112 static void rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
113 static void rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
114 int, int);
115 static void rssadapt_tx_complete(const struct ieee80211_node *,
116 const struct ieee80211_ratectl_tx_status *);
117 #ifdef notyet
118 static void rssadapt_sysctlattach(struct ieee80211vap *,
119 struct sysctl_ctx_list *, struct sysctl_oid *);
120 #endif
121
122 /* number of references from net80211 layer */
123 static int nrefs = 0;
124
125 #if __FreeBSD__
126 static const struct ieee80211_ratectl rssadapt = {
127 #elif __NetBSD__
128 const struct ieee80211_ratectl ratectl_rssadapt = {
129 #endif
130 .ir_name = "rssadapt",
131 .ir_attach = NULL,
132 .ir_detach = NULL,
133 .ir_init = rssadapt_init,
134 .ir_deinit = rssadapt_deinit,
135 .ir_node_init = rssadapt_node_init,
136 .ir_node_deinit = rssadapt_node_deinit,
137 .ir_rate = rssadapt_rate,
138 .ir_tx_complete = rssadapt_tx_complete,
139 .ir_tx_update = NULL,
140 .ir_setinterval = rssadapt_setinterval,
141 };
142 #if __FreeBSD__
143 IEEE80211_RATECTL_MODULE(rssadapt, 1);
144 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
145 #endif
146
147 static void
148 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
149 {
150 struct ieee80211_rssadapt *rs = vap->iv_rs;
151 int t;
152
153 if (msecs < 100)
154 msecs = 100;
155 t = msecs_to_ticks(msecs);
156 rs->interval = (t < 1) ? 1 : t;
157 }
158
159 static void
160 rssadapt_init(struct ieee80211vap *vap)
161 {
162 struct ieee80211_rssadapt *rs;
163
164 KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
165 __func__));
166
167 nrefs++; /* XXX locking */
168 vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
169 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
170 if (rs == NULL) {
171 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
172 return;
173 }
174 rs->vap = vap;
175 rssadapt_setinterval(vap, 500 /* msecs */);
176 #ifdef notyet
177 rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
178 #endif
179 }
180
181 static void
182 rssadapt_deinit(struct ieee80211vap *vap)
183 {
184 IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
185 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
186 nrefs--; /* XXX locking */
187 }
188
189 static void
190 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
191 {
192 long interval;
193
194 ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
195 ra->ra_nfail = ra->ra_nok = 0;
196
197 /*
198 * A node is eligible for its rate to be raised every 1/10 to 10
199 * seconds, more eligible in proportion to recent packet rates.
200 */
201 interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
202 ra->ra_raise_interval = msecs_to_ticks(interval);
203 }
204
205 static void
206 rssadapt_node_init(struct ieee80211_node *ni)
207 {
208 struct ieee80211_rssadapt_node *ra;
209 struct ieee80211vap *vap = ni->ni_vap;
210 struct ieee80211_rssadapt *rsa = vap->iv_rs;
211 const struct ieee80211_rateset *rs = &ni->ni_rates;
212
213 if (ni->ni_rctls == NULL) {
214 ni->ni_rctls = ra =
215 IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
216 M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
217 if (ra == NULL) {
218 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
219 "structure\n");
220 return;
221 }
222 } else
223 ra = ni->ni_rctls;
224 ra->ra_rs = rsa;
225 ra->ra_rates = *rs;
226 rssadapt_updatestats(ra);
227
228 /* pick initial rate */
229 for (ra->ra_rix = rs->rs_nrates - 1;
230 ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
231 ra->ra_rix--)
232 ;
233 ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
234 ra->ra_ticks = ticks;
235
236 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
237 "RSSADAPT initial rate %d", ni->ni_txrate);
238 }
239
240 static void
241 rssadapt_node_deinit(struct ieee80211_node *ni)
242 {
243
244 IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
245 }
246
247 static __inline int
248 bucket(int pktlen)
249 {
250 int i, top, thridx;
251
252 for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
253 i < IEEE80211_RSSADAPT_BKTS;
254 i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
255 thridx = i;
256 if (pktlen <= top)
257 break;
258 }
259 return thridx;
260 }
261
262 static int
263 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
264 {
265 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
266 u_int pktlen = iarg;
267 const struct ieee80211_rateset *rs = &ra->ra_rates;
268 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
269 int rix, rssi;
270
271 if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
272 rssadapt_updatestats(ra);
273 ra->ra_ticks = ticks;
274 }
275
276 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
277
278 /* XXX this is average rssi, should be using last value */
279 rssi = ni->ni_ic->ic_node_getrssi(ni);
280 for (rix = rs->rs_nrates-1; rix >= 0; rix--)
281 if ((*thrs)[rix] < (rssi << 8))
282 break;
283 if (rix != ra->ra_rix) {
284 /* update public rate */
285 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
286 ra->ra_rix = rix;
287
288 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
289 "RSSADAPT new rate %d (pktlen %d rssi %d)",
290 ni->ni_txrate, pktlen, rssi);
291 }
292 return rix;
293 }
294
295 /*
296 * Adapt the data rate to suit the conditions. When a transmitted
297 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
298 * raise the RSS threshold for transmitting packets of similar length at
299 * the same data rate.
300 */
301 static void
302 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
303 {
304 uint16_t last_thr;
305 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
306 u_int rix;
307
308 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
309
310 rix = ra->ra_rix;
311 last_thr = (*thrs)[rix];
312 (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
313 last_thr, (rssi << 8));
314
315 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
316 "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
317 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
318 last_thr, (*thrs)[rix], rssi);
319 }
320
321 static void
322 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
323 {
324 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
325 uint16_t newthr, oldthr;
326 int rix;
327
328 thrs = &ra->ra_rate_thresh[bucket(pktlen)];
329
330 rix = ra->ra_rix;
331 if ((*thrs)[rix + 1] > (*thrs)[rix]) {
332 oldthr = (*thrs)[rix + 1];
333 if ((*thrs)[rix] == 0)
334 newthr = (rssi << 8);
335 else
336 newthr = (*thrs)[rix];
337 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
338 oldthr, newthr);
339
340 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
341 "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
342 ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
343 oldthr, newthr, rssi);
344
345 ra->ra_last_raise = ticks;
346 }
347 }
348
349 static void
350 rssadapt_tx_complete(const struct ieee80211_node *ni,
351 const struct ieee80211_ratectl_tx_status *status)
352 {
353 struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
354 int pktlen, rssi;
355
356 if ((status->flags &
357 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
358 (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
359 return;
360
361 pktlen = status->pktlen;
362 rssi = status->rssi;
363
364 if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
365 ra->ra_nok++;
366 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
367 (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
368 rssadapt_raise_rate(ra, pktlen, rssi);
369 } else {
370 ra->ra_nfail++;
371 rssadapt_lower_rate(ra, pktlen, rssi);
372 }
373 }
374
375 #ifdef notyet
376 static int
377 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
378 {
379 struct ieee80211vap *vap = arg1;
380 struct ieee80211_rssadapt *rs = vap->iv_rs;
381 int msecs = ticks_to_msecs(rs->interval);
382 int error;
383
384 error = sysctl_handle_int(oidp, &msecs, 0, req);
385 if (error || !req->newptr)
386 return error;
387 rssadapt_setinterval(vap, msecs);
388 return 0;
389 }
390
391 static void
392 rssadapt_sysctlattach(struct ieee80211vap *vap,
393 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
394 {
395 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
396 "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
397 0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
398 }
399 #endif
400