1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* $FreeBSD$ */
/* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
/*-
* Copyright (c) 2003, 2004 David Young. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of David Young may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
* Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_rssadapt.h>
struct rssadapt_expavgctl {
/* RSS threshold decay. */
u_int rc_decay_denom;
u_int rc_decay_old;
/* RSS threshold update. */
u_int rc_thresh_denom;
u_int rc_thresh_old;
/* RSS average update. */
u_int rc_avgrssi_denom;
u_int rc_avgrssi_old;
};
static struct rssadapt_expavgctl master_expavgctl = {
rc_decay_denom : 16,
rc_decay_old : 15,
rc_thresh_denom : 8,
rc_thresh_old : 4,
rc_avgrssi_denom : 8,
rc_avgrssi_old : 4
};
#ifdef interpolate
#undef interpolate
#endif
#define interpolate(parm, old, new) ((parm##_old * (old) + \
(parm##_denom - parm##_old) * (new)) / \
parm##_denom)
static void rssadapt_sysctlattach(struct ieee80211_rssadapt *rs,
struct sysctl_ctx_list *ctx, struct sysctl_oid *tree);
/* number of references from net80211 layer */
static int nrefs = 0;
void
ieee80211_rssadapt_setinterval(struct ieee80211_rssadapt *rs, int msecs)
{
int t;
if (msecs < 100)
msecs = 100;
t = msecs_to_ticks(msecs);
rs->interval = (t < 1) ? 1 : t;
}
void
ieee80211_rssadapt_init(struct ieee80211_rssadapt *rs, struct ieee80211vap *vap, int interval)
{
rs->vap = vap;
ieee80211_rssadapt_setinterval(rs, interval);
rssadapt_sysctlattach(rs, vap->iv_sysctl, vap->iv_oid);
}
void
ieee80211_rssadapt_cleanup(struct ieee80211_rssadapt *rs)
{
}
static void
rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
{
long interval;
ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
ra->ra_nfail = ra->ra_nok = 0;
/*
* A node is eligible for its rate to be raised every 1/10 to 10
* seconds, more eligible in proportion to recent packet rates.
*/
interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
ra->ra_raise_interval = msecs_to_ticks(interval);
}
void
ieee80211_rssadapt_node_init(struct ieee80211_rssadapt *rsa,
struct ieee80211_rssadapt_node *ra, struct ieee80211_node *ni)
{
const struct ieee80211_rateset *rs = &ni->ni_rates;
ra->ra_rs = rsa;
ra->ra_rates = *rs;
rssadapt_updatestats(ra);
/* pick initial rate */
for (ra->ra_rix = rs->rs_nrates - 1;
ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
ra->ra_rix--)
;
ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
ra->ra_ticks = ticks;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
"RSSADAPT initial rate %d", ni->ni_txrate);
}
static __inline int
bucket(int pktlen)
{
int i, top, thridx;
for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
i < IEEE80211_RSSADAPT_BKTS;
i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
thridx = i;
if (pktlen <= top)
break;
}
return thridx;
}
int
ieee80211_rssadapt_choose(struct ieee80211_node *ni,
struct ieee80211_rssadapt_node *ra, u_int pktlen)
{
const struct ieee80211_rateset *rs = &ra->ra_rates;
uint16_t (*thrs)[IEEE80211_RATE_SIZE];
int rix, rssi;
if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
rssadapt_updatestats(ra);
ra->ra_ticks = ticks;
}
thrs = &ra->ra_rate_thresh[bucket(pktlen)];
/* XXX this is average rssi, should be using last value */
rssi = ni->ni_ic->ic_node_getrssi(ni);
for (rix = rs->rs_nrates-1; rix >= 0; rix--)
if ((*thrs)[rix] < (rssi << 8))
break;
if (rix != ra->ra_rix) {
/* update public rate */
ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
ra->ra_rix = rix;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
"RSSADAPT new rate %d (pktlen %d rssi %d)",
ni->ni_txrate, pktlen, rssi);
}
return rix;
}
/*
* Adapt the data rate to suit the conditions. When a transmitted
* packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
* raise the RSS threshold for transmitting packets of similar length at
* the same data rate.
*/
void
ieee80211_rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra,
int pktlen, int rssi)
{
uint16_t last_thr;
uint16_t (*thrs)[IEEE80211_RATE_SIZE];
u_int rix;
thrs = &ra->ra_rate_thresh[bucket(pktlen)];
rix = ra->ra_rix;
last_thr = (*thrs)[rix];
(*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
last_thr, (rssi << 8));
IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
"RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
last_thr, (*thrs)[rix], rssi);
}
void
ieee80211_rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra,
int pktlen, int rssi)
{
uint16_t (*thrs)[IEEE80211_RATE_SIZE];
uint16_t newthr, oldthr;
int rix;
thrs = &ra->ra_rate_thresh[bucket(pktlen)];
rix = ra->ra_rix;
if ((*thrs)[rix + 1] > (*thrs)[rix]) {
oldthr = (*thrs)[rix + 1];
if ((*thrs)[rix] == 0)
newthr = (rssi << 8);
else
newthr = (*thrs)[rix];
(*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
oldthr, newthr);
IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
"RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
oldthr, newthr, rssi);
ra->ra_last_raise = ticks;
}
}
static int
rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
{
struct ieee80211_rssadapt *rs = arg1;
int msecs = ticks_to_msecs(rs->interval);
int error;
error = sysctl_handle_int(oidp, &msecs, 0, req);
if (error || !req->newptr)
return error;
ieee80211_rssadapt_setinterval(rs, msecs);
return 0;
}
static void
rssadapt_sysctlattach(struct ieee80211_rssadapt *rs,
struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
{
SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
"rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, rs,
0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
}
/*
* Module glue.
*/
IEEE80211_RATE_MODULE(rssadapt, 1);
|