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