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