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