]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_rssadapt.c
MFV r336851:
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_rssadapt.c
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         int t;
121
122         if (msecs < 100)
123                 msecs = 100;
124         t = msecs_to_ticks(msecs);
125         rs->interval = (t < 1) ? 1 : t;
126 }
127
128 static void
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                 if_printf(vap->iv_ifp, "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
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
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
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 (ni->ni_rctls == NULL) {
181                 ni->ni_rctls = ra = 
182                     IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
183                         M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
184                 if (ra == NULL) {
185                         if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
186                             "structure\n");
187                         return;
188                 }
189         } else
190                 ra = ni->ni_rctls;
191         ra->ra_rs = rsa;
192         ra->ra_rates = *rs;
193         rssadapt_updatestats(ra);
194
195         /* pick initial rate */
196         for (ra->ra_rix = rs->rs_nrates - 1;
197              ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
198              ra->ra_rix--)
199                 ;
200         ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
201         ra->ra_ticks = ticks;
202
203         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
204             "RSSADAPT initial rate %d", ni->ni_txrate);
205 }
206
207 static void
208 rssadapt_node_deinit(struct ieee80211_node *ni)
209 {
210
211         IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
212 }
213
214 static __inline int
215 bucket(int pktlen)
216 {
217         int i, top, thridx;
218
219         for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
220              i < IEEE80211_RSSADAPT_BKTS;
221              i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
222                 thridx = i;
223                 if (pktlen <= top)
224                         break;
225         }
226         return thridx;
227 }
228
229 static int
230 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
231 {
232         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
233         u_int pktlen = iarg;
234         const struct ieee80211_rateset *rs = &ra->ra_rates;
235         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
236         int rix, rssi;
237
238         if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
239                 rssadapt_updatestats(ra);
240                 ra->ra_ticks = ticks;
241         }
242
243         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
244
245         /* XXX this is average rssi, should be using last value */
246         rssi = ni->ni_ic->ic_node_getrssi(ni);
247         for (rix = rs->rs_nrates-1; rix >= 0; rix--)
248                 if ((*thrs)[rix] < (rssi << 8))
249                         break;
250         if (rix != ra->ra_rix) {
251                 /* update public rate */
252                 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
253                 ra->ra_rix = rix;
254
255                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
256                     "RSSADAPT new rate %d (pktlen %d rssi %d)",
257                     ni->ni_txrate, pktlen, rssi);
258         }
259         return rix;
260 }
261
262 /*
263  * Adapt the data rate to suit the conditions.  When a transmitted
264  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
265  * raise the RSS threshold for transmitting packets of similar length at
266  * the same data rate.
267  */
268 static void
269 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
270 {
271         uint16_t last_thr;
272         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
273         u_int rix;
274
275         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
276
277         rix = ra->ra_rix;
278         last_thr = (*thrs)[rix];
279         (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
280             last_thr, (rssi << 8));
281
282         IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
283             "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
284             ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
285             last_thr, (*thrs)[rix], rssi);
286 }
287
288 static void
289 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
290 {
291         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
292         uint16_t newthr, oldthr;
293         int rix;
294
295         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
296
297         rix = ra->ra_rix;
298         if ((*thrs)[rix + 1] > (*thrs)[rix]) {
299                 oldthr = (*thrs)[rix + 1];
300                 if ((*thrs)[rix] == 0)
301                         newthr = (rssi << 8);
302                 else
303                         newthr = (*thrs)[rix];
304                 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
305                     oldthr, newthr);
306
307                 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
308                     "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
309                     ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
310                     oldthr, newthr, rssi);
311
312                 ra->ra_last_raise = ticks;
313         }
314 }
315
316 static void
317 rssadapt_tx_complete(const struct ieee80211_node *ni,
318     const struct ieee80211_ratectl_tx_status *status)
319 {
320         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
321         int pktlen, rssi;
322
323         if ((status->flags &
324             (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
325             (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
326                 return;
327
328         pktlen = status->pktlen;
329         rssi = status->rssi;
330
331         if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
332                 ra->ra_nok++;
333                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
334                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
335                         rssadapt_raise_rate(ra, pktlen, rssi);
336         } else {
337                 ra->ra_nfail++;
338                 rssadapt_lower_rate(ra, pktlen, rssi);
339         }
340 }
341
342 static int
343 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
344 {
345         struct ieee80211vap *vap = arg1;
346         struct ieee80211_rssadapt *rs = vap->iv_rs;
347         int msecs = ticks_to_msecs(rs->interval);
348         int error;
349
350         error = sysctl_handle_int(oidp, &msecs, 0, req);
351         if (error || !req->newptr)
352                 return error;
353         rssadapt_setinterval(vap, msecs);
354         return 0;
355 }
356
357 static void
358 rssadapt_sysctlattach(struct ieee80211vap *vap,
359     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
360 {
361
362         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
363             "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
364             0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
365 }