]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_rssadapt.c
net80211: fix channel list construction for non-auto operating mode.
[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 (!rs)
123                 return;
124
125         if (msecs < 100)
126                 msecs = 100;
127         t = msecs_to_ticks(msecs);
128         rs->interval = (t < 1) ? 1 : t;
129 }
130
131 static void
132 rssadapt_init(struct ieee80211vap *vap)
133 {
134         struct ieee80211_rssadapt *rs;
135
136         KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
137             __func__));
138
139         nrefs++;                /* XXX locking */
140         vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
141             M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
142         if (rs == NULL) {
143                 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
144                 return;
145         }
146         rs->vap = vap;
147         rssadapt_setinterval(vap, 500 /* msecs */);
148         rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
149 }
150
151 static void
152 rssadapt_deinit(struct ieee80211vap *vap)
153 {
154         IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
155         KASSERT(nrefs > 0, ("imbalanced attach/detach"));
156         nrefs--;                /* XXX locking */
157 }
158
159 static void
160 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
161 {
162         long interval;
163
164         ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
165         ra->ra_nfail = ra->ra_nok = 0;
166
167         /*
168          * A node is eligible for its rate to be raised every 1/10 to 10
169          * seconds, more eligible in proportion to recent packet rates.
170          */
171         interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
172         ra->ra_raise_interval = msecs_to_ticks(interval);
173 }
174
175 static void
176 rssadapt_node_init(struct ieee80211_node *ni)
177 {
178         struct ieee80211_rssadapt_node *ra;
179         struct ieee80211vap *vap = ni->ni_vap;
180         struct ieee80211_rssadapt *rsa = vap->iv_rs;
181         const struct ieee80211_rateset *rs = &ni->ni_rates;
182
183         if (!rsa) {
184                 if_printf(vap->iv_ifp, "ratectl structure was not allocated, "
185                     "per-node structure allocation skipped\n");
186                 return;
187         }
188
189         if (ni->ni_rctls == NULL) {
190                 ni->ni_rctls = ra = 
191                     IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
192                         M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
193                 if (ra == NULL) {
194                         if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
195                             "structure\n");
196                         return;
197                 }
198         } else
199                 ra = ni->ni_rctls;
200         ra->ra_rs = rsa;
201         ra->ra_rates = *rs;
202         rssadapt_updatestats(ra);
203
204         /* pick initial rate */
205         for (ra->ra_rix = rs->rs_nrates - 1;
206              ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
207              ra->ra_rix--)
208                 ;
209         ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
210         ra->ra_ticks = ticks;
211
212         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
213             "RSSADAPT initial rate %d", ni->ni_txrate);
214 }
215
216 static void
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
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
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                 ni->ni_txrate = ni->ni_rates.rs_rates[0];
251                 return 0;
252         }
253
254         rs = &ra->ra_rates;
255         if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
256                 rssadapt_updatestats(ra);
257                 ra->ra_ticks = ticks;
258         }
259
260         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
261
262         /* XXX this is average rssi, should be using last value */
263         rssi = ni->ni_ic->ic_node_getrssi(ni);
264         for (rix = rs->rs_nrates-1; rix >= 0; rix--)
265                 if ((*thrs)[rix] < (rssi << 8))
266                         break;
267         if (rix != ra->ra_rix) {
268                 /* update public rate */
269                 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
270                 ra->ra_rix = rix;
271
272                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
273                     "RSSADAPT new rate %d (pktlen %d rssi %d)",
274                     ni->ni_txrate, pktlen, rssi);
275         }
276         return rix;
277 }
278
279 /*
280  * Adapt the data rate to suit the conditions.  When a transmitted
281  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
282  * raise the RSS threshold for transmitting packets of similar length at
283  * the same data rate.
284  */
285 static void
286 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
287 {
288         uint16_t last_thr;
289         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
290         u_int rix;
291
292         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
293
294         rix = ra->ra_rix;
295         last_thr = (*thrs)[rix];
296         (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
297             last_thr, (rssi << 8));
298
299         IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
300             "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
301             ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
302             last_thr, (*thrs)[rix], rssi);
303 }
304
305 static void
306 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
307 {
308         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
309         uint16_t newthr, oldthr;
310         int rix;
311
312         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
313
314         rix = ra->ra_rix;
315         if ((*thrs)[rix + 1] > (*thrs)[rix]) {
316                 oldthr = (*thrs)[rix + 1];
317                 if ((*thrs)[rix] == 0)
318                         newthr = (rssi << 8);
319                 else
320                         newthr = (*thrs)[rix];
321                 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
322                     oldthr, newthr);
323
324                 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
325                     "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
326                     ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
327                     oldthr, newthr, rssi);
328
329                 ra->ra_last_raise = ticks;
330         }
331 }
332
333 static void
334 rssadapt_tx_complete(const struct ieee80211_node *ni,
335     const struct ieee80211_ratectl_tx_status *status)
336 {
337         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
338         int pktlen, rssi;
339
340         if (!ra)
341                 return;
342
343         if ((status->flags &
344             (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI)) !=
345             (IEEE80211_RATECTL_STATUS_PKTLEN|IEEE80211_RATECTL_STATUS_RSSI))
346                 return;
347
348         pktlen = status->pktlen;
349         rssi = status->rssi;
350
351         if (status->status == IEEE80211_RATECTL_TX_SUCCESS) {
352                 ra->ra_nok++;
353                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
354                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
355                         rssadapt_raise_rate(ra, pktlen, rssi);
356         } else {
357                 ra->ra_nfail++;
358                 rssadapt_lower_rate(ra, pktlen, rssi);
359         }
360 }
361
362 static int
363 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
364 {
365         struct ieee80211vap *vap = arg1;
366         struct ieee80211_rssadapt *rs = vap->iv_rs;
367         int msecs, error;
368
369         if (!rs)
370                 return ENOMEM;
371
372         msecs = ticks_to_msecs(rs->interval);
373         error = sysctl_handle_int(oidp, &msecs, 0, req);
374         if (error || !req->newptr)
375                 return error;
376         rssadapt_setinterval(vap, msecs);
377         return 0;
378 }
379
380 static void
381 rssadapt_sysctlattach(struct ieee80211vap *vap,
382     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
383 {
384
385         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
386             "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
387             0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
388 }