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