2 * Copyright (c) 2003, 2004 David Young. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
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
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
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 $
35 #include <sys/param.h>
36 #include <sys/sockio.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.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>
48 #include <netproto/802_11/ieee80211_var.h>
50 #include <dev/netif/ral/if_ralrate.h>
55 #define interpolate(parm, old, new) ((parm##_old * (old) + \
56 (parm##_denom - parm##_old) * (new)) / \
59 static struct ral_rssadapt_expavgctl master_expavgctl = {
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)
72 u_int16_t (*thrs)[IEEE80211_RATE_SIZE];
73 int flags = 0, i, rateidx = 0, thridx, top;
75 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
76 flags |= IEEE80211_RATE_BASIC;
78 for (i = 0, top = RAL_RSSADAPT_BKT0;
79 i < RAL_RSSADAPT_BKTS;
80 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
86 thrs = &ra->ra_rate_thresh[thridx];
91 if ((rs->rs_rates[i] & flags) != flags)
95 if ((*thrs)[i] < ra->ra_avg_rssi)
103 ral_rssadapt_updatestats(struct ral_rssadapt *ra)
108 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
109 ra->ra_nfail = ra->ra_nok = 0;
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.
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);
120 ral_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
121 struct ral_rssadapt *ra, int rssi)
123 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
124 ra->ra_avg_rssi, (rssi << 8));
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.
134 ral_rssadapt_lower_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
135 struct ral_rssadapt *ra, struct ral_rssdesc *id)
137 struct ieee80211_rateset *rs = &ni->ni_rates;
139 u_int i, thridx, top;
143 if (id->id_rateidx >= rs->rs_nrates)
146 for (i = 0, top = RAL_RSSADAPT_BKT0;
147 i < RAL_RSSADAPT_BKTS;
148 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
150 if (id->id_len <= top)
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,
161 ral_rssadapt_raise_rate(struct ieee80211com *ic, struct ral_rssadapt *ra,
162 struct ral_rssdesc *id)
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;
171 if (!ratecheck(&ra->ra_last_raise, &ra->ra_raise_interval))
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)
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);
186 oldthr = (*thrs)[id->id_rateidx + 1];
187 if ((*thrs)[id->id_rateidx] == 0)
188 newthr = ra->ra_avg_rssi;
190 newthr = (*thrs)[id->id_rateidx];
191 (*thrs)[id->id_rateidx + 1] =
192 interpolate(master_expavgctl.rc_decay, oldthr, newthr);