1 /* $FreeBSD: releng/6.0/sys/dev/ral/if_ralrate.c 145247 2005-04-18 18:47:38Z damien $ */
2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
3 /*-
4 * Copyright (c) 2003, 2004 David Young. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * 3. The name of David Young may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
23 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/sockio.h>
35 #include <sys/mbuf.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_types.h>
45
46 #include <net80211/ieee80211_var.h>
47
48 #include <dev/ral/if_ralrate.h>
49
50 #ifdef interpolate
51 #undef interpolate
52 #endif
53 #define interpolate(parm, old, new) ((parm##_old * (old) + \
54 (parm##_denom - parm##_old) * (new)) / \
55 parm##_denom)
56
57 static struct ral_rssadapt_expavgctl master_expavgctl = {
58 rc_decay_denom : 16,
59 rc_decay_old : 15,
60 rc_thresh_denom : 8,
61 rc_thresh_old : 4,
62 rc_avgrssi_denom : 8,
63 rc_avgrssi_old : 4
64 };
65
66 int
67 ral_rssadapt_choose(struct ral_rssadapt *ra, struct ieee80211_rateset *rs,
68 struct ieee80211_frame *wh, u_int len, const char *dvname, int do_not_adapt)
69 {
70 u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
71 int flags = 0, i, rateidx = 0, thridx, top;
72
73 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
74 flags |= IEEE80211_RATE_BASIC;
75
76 for (i = 0, top = RAL_RSSADAPT_BKT0;
77 i < RAL_RSSADAPT_BKTS;
78 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
79 thridx = i;
80 if (len <= top)
81 break;
82 }
83
84 thrs = &ra->ra_rate_thresh[thridx];
85
86 i = rs->rs_nrates;
87 while (--i >= 0) {
88 rateidx = i;
89 if ((rs->rs_rates[i] & flags) != flags)
90 continue;
91 if (do_not_adapt)
92 break;
93 if ((*thrs)[i] < ra->ra_avg_rssi)
94 break;
95 }
96
97 return rateidx;
98 }
99
100 void
101 ral_rssadapt_updatestats(struct ral_rssadapt *ra)
102 {
103 long interval;
104
105 ra->ra_pktrate =
106 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
107 ra->ra_nfail = ra->ra_nok = 0;
108
109 /* a node is eligible for its rate to be raised every 1/10 to 10
110 * seconds, more eligible in proportion to recent packet rates.
111 */
112 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
113 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
114 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
115 }
116
117 void
118 ral_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
119 struct ral_rssadapt *ra, int rssi)
120 {
121 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
122 ra->ra_avg_rssi, (rssi << 8));
123 }
124
125 /*
126 * Adapt the data rate to suit the conditions. When a transmitted
127 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
128 * raise the RSS threshold for transmitting packets of similar length at
129 * the same data rate.
130 */
131 void
132 ral_rssadapt_lower_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
133 struct ral_rssadapt *ra, struct ral_rssdesc *id)
134 {
135 struct ieee80211_rateset *rs = &ni->ni_rates;
136 u_int16_t last_thr;
137 u_int i, thridx, top;
138
139 ra->ra_nfail++;
140
141 if (id->id_rateidx >= rs->rs_nrates)
142 return;
143
144 for (i = 0, top = RAL_RSSADAPT_BKT0;
145 i < RAL_RSSADAPT_BKTS;
146 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
147 thridx = i;
148 if (id->id_len <= top)
149 break;
150 }
151
152 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
153 ra->ra_rate_thresh[thridx][id->id_rateidx] =
154 interpolate(master_expavgctl.rc_thresh, last_thr,
155 (id->id_rssi << 8));
156 }
157
158 void
159 ral_rssadapt_raise_rate(struct ieee80211com *ic, struct ral_rssadapt *ra,
160 struct ral_rssdesc *id)
161 {
162 u_int16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
163 struct ieee80211_node *ni = id->id_node;
164 struct ieee80211_rateset *rs = &ni->ni_rates;
165 int i, rate, top;
166
167 ra->ra_nok++;
168
169 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
170 return;
171
172 for (i = 0, top = RAL_RSSADAPT_BKT0;
173 i < RAL_RSSADAPT_BKTS;
174 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
175 thrs = &ra->ra_rate_thresh[i];
176 if (id->id_len <= top)
177 break;
178 }
179
180 if (id->id_rateidx + 1 < rs->rs_nrates &&
181 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
182 rate = (rs->rs_rates[id->id_rateidx + 1] & IEEE80211_RATE_VAL);
183
184 oldthr = (*thrs)[id->id_rateidx + 1];
185 if ((*thrs)[id->id_rateidx] == 0)
186 newthr = ra->ra_avg_rssi;
187 else
188 newthr = (*thrs)[id->id_rateidx];
189 (*thrs)[id->id_rateidx + 1] =
190 interpolate(master_expavgctl.rc_decay, oldthr, newthr);
191 }
192 }
Cache object: 4d77b971769fe1d644e91348156589f0
|