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