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