sync with latest code in madwifi

Obtained from:	madwifi
MFC after:	2 weeks
This commit is contained in:
Sam Leffler 2006-02-09 20:40:28 +00:00
parent 1e02285171
commit b91bf513f3
2 changed files with 188 additions and 149 deletions

View File

@ -78,7 +78,7 @@ __FBSDID("$FreeBSD$");
#define SAMPLE_DEBUG
#ifdef SAMPLE_DEBUG
enum {
ATH_DEBUG_RATE = 0x00000010, /* rate control */
ATH_DEBUG_RATE = 0x00000010 /* rate control */
};
#define DPRINTF(sc, _fmt, ...) do { \
if (sc->sc_debug & ATH_DEBUG_RATE) \
@ -108,13 +108,17 @@ enum {
* The difference between the algorithm in the thesis and the one in this
* file is that the one in this file uses a ewma instead of a window.
*
* Also, this implementation tracks the average transmission time for
* a few different packet sizes independently for each link.
*/
#define STALE_FAILURE_TIMEOUT_MS 10000
#define MIN_SWITCH_MS 1000
static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
static __inline int size_to_bin(int size)
static __inline int
size_to_bin(int size)
{
int x = 0;
for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) {
@ -124,11 +128,13 @@ static __inline int size_to_bin(int size)
}
return NUM_PACKET_SIZE_BINS-1;
}
static __inline int bin_to_size(int index) {
static __inline int
bin_to_size(int index) {
return packet_size_bins[index];
}
static __inline int rate_to_ndx(struct sample_node *sn, int rate) {
static __inline int
rate_to_ndx(struct sample_node *sn, int rate) {
int x = 0;
for (x = 0; x < sn->num_rates; x++) {
if (sn->rates[x].rate == rate) {
@ -185,6 +191,15 @@ static __inline int best_rate_ndx(struct sample_node *sn, int size_bin,
!sn->stats[size_bin][x].packets_acked)) {
continue;
}
/* 9 megabits never works better than 12 */
if (sn->rates[x].rate == 18)
continue;
/* don't use a bit-rate that has been failing */
if (sn->stats[size_bin][x].successive_failures > 3)
continue;
if (!best_rate_tt || best_rate_tt > tt) {
best_rate_tt = tt;
best_rate_ndx = x;
@ -194,11 +209,10 @@ static __inline int best_rate_ndx(struct sample_node *sn, int size_bin,
}
/*
* pick a ndx s.t. the perfect_tx_time
* is less than the best bit-rate's average_tx_time
* and the ndx has not had four successive failures.
* pick a good "random" bit-rate to sample other than the current one
*/
static __inline int pick_sample_ndx(struct sample_node *sn, int size_bin)
static __inline int
pick_sample_ndx(struct sample_node *sn, int size_bin)
{
int x = 0;
int current_ndx = 0;
@ -213,24 +227,38 @@ static __inline int pick_sample_ndx(struct sample_node *sn, int size_bin)
current_tt = sn->stats[size_bin][current_ndx].average_tx_time;
for (x = 0; x < sn->num_rates; x++) {
int ndx = (sn->last_sample_ndx[size_bin] + 1 + x) % sn->num_rates;
/*
* clear any stale stuff out.
*/
if (ticks - sn->stats[size_bin][ndx].last_tx > ((hz * STALE_FAILURE_TIMEOUT_MS)/1000)) {
sn->stats[size_bin][ndx].average_tx_time = sn->stats[size_bin][ndx].perfect_tx_time;
sn->stats[size_bin][ndx].successive_failures = 0;
sn->stats[size_bin][ndx].tries = 0;
sn->stats[size_bin][ndx].total_packets = 0;
sn->stats[size_bin][ndx].packets_acked = 0;
}
int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates;
if (ndx != current_ndx &&
sn->stats[size_bin][ndx].perfect_tx_time < current_tt &&
sn->stats[size_bin][ndx].successive_failures < 4) {
sn->last_sample_ndx[size_bin] = ndx;
return ndx;
}
/* don't sample the current bit-rate */
if (ndx == current_ndx)
continue;
/* this bit-rate is always worse than the current one */
if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt)
continue;
/* rarely sample bit-rates that fail a lot */
if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) &&
sn->stats[size_bin][ndx].successive_failures > 3)
continue;
/* don't sample more than 2 indexes higher
* for rates higher than 11 megabits
*/
if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2)
continue;
/* 9 megabits never works better than 12 */
if (sn->rates[ndx].rate == 18)
continue;
/* if we're using 11 megabits, only sample up to 12 megabits
*/
if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1)
continue;
sn->last_sample_ndx[size_bin] = ndx;
return ndx;
}
return current_ndx;
}
@ -243,11 +271,10 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
struct sample_node *sn = ATH_NODE_SAMPLE(an);
struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
struct ieee80211com *ic = &sc->sc_ic;
int ndx, size_bin, mrr, best_ndx;
int ndx, size_bin, mrr, best_ndx, change_rates;
unsigned average_tx_time;
mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT) &&
!(frameLen > ic->ic_rtsthreshold);
mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
size_bin = size_to_bin(frameLen);
best_ndx = best_rate_ndx(sn, size_bin, !mrr);
@ -256,57 +283,14 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
} else {
average_tx_time = 0;
}
if (sn->static_rate_ndx != -1) {
ndx = sn->static_rate_ndx;
*try0 = ATH_TXMAXTRY;
} else {
ndx = 0;
*try0 = mrr ? 2 : ATH_TXMAXTRY;
DPRINTF(sc, "%s: %s size %d mrr %d packets_sent %d best_ndx %d "
"sample tt %d packets since %d\n"
, __func__, ether_sprintf(an->an_node.ni_macaddr)
, packet_size_bins[size_bin]
, mrr
, sn->packets_sent[size_bin]
, best_ndx
, sn->sample_tt[size_bin]
, sn->packets_since_sample[size_bin]
);
if (!sn->packets_sent[size_bin]) {
/* no packets sent */
if (best_ndx == -1) {
ndx = sn->num_rates - 1;
if (sc->sc_curmode != IEEE80211_MODE_11B) {
for (; ndx >= 0 && sn->rates[ndx].rate > 72; ndx--)
;
}
} else {
ndx = best_ndx;
}
} else if (best_ndx == -1) {
/* no packet has succeeded yet */
if (mrr) {
/*
* no packet has succeeded, try the
* highest bitrate that hasn't failed
*/
for (ndx = sn->num_rates-1; ndx >= 0; ndx--) {
if (sn->stats[size_bin][ndx].successive_failures == 0) {
break;
}
}
} else {
ndx = sn->num_rates - 1;
if (sc->sc_curmode != IEEE80211_MODE_11B) {
for (; ndx >= 0 && sn->rates[ndx].rate > 72; ndx--)
;
}
}
} else if (sn->sample_tt[size_bin] < (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100) * average_tx_time &&
sn->packets_since_sample[size_bin] > 15) {
if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) {
/*
* we want to limit the time measuring the performance
* of other bit-rates to ath_sample_rate% of the
@ -314,13 +298,6 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
*/
ndx = pick_sample_ndx(sn, size_bin);
if (ndx != sn->current_rate[size_bin]) {
DPRINTF(sc, "%s: %s size %d last sample tt %d sampling %d packets since %d\n",
__func__,
ether_sprintf(an->an_node.ni_macaddr),
packet_size_bins[size_bin],
sn->sample_tt[size_bin],
sn->rates[ndx].rate,
sn->packets_since_sample[size_bin]);
sn->current_sample_ndx[size_bin] = ndx;
} else {
sn->current_sample_ndx[size_bin] = -1;
@ -328,19 +305,37 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
sn->packets_since_sample[size_bin] = 0;
} else {
change_rates = 0;
if (!sn->packets_sent[size_bin] || best_ndx == -1) {
/* no packet has been sent successfully yet */
for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
/*
* pick the highest rate <= 36 Mbps
* that hasn't failed.
*/
if (sn->rates[ndx].rate <= 72 &&
sn->stats[size_bin][ndx].successive_failures == 0) {
break;
}
}
change_rates = 1;
best_ndx = ndx;
} else if (sn->packets_sent[size_bin] < 20) {
/* let the bit-rate switch quickly during the first few packets */
change_rates = 1;
} else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) {
/* 2 seconds have gone by */
change_rates = 1;
} else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) {
/* the current bit-rate is twice as slow as the best one */
change_rates = 1;
}
sn->packets_since_sample[size_bin]++;
/*
* don't switch bit-rates every packet. only
* switch during the first few packets we send
* or after 100 packets, or if the current
* bit-rate begins to perform twice as bad as
* another one.
*/
if (sn->packets_sent[size_bin] < 20 ||
ticks - ((hz*2000)/1000) > sn->jiffies_since_switch[size_bin] ||
average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time ) {
if (sn->packets_sent[size_bin] > 20) {
DPRINTF(sc, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mmr %d\n",
if (change_rates) {
if (best_ndx != sn->current_rate[size_bin]) {
DPRINTF(sc, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n",
__func__,
ether_sprintf(an->an_node.ni_macaddr),
packet_size_bins[size_bin],
@ -355,17 +350,22 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
}
sn->packets_since_switch[size_bin] = 0;
sn->current_rate[size_bin] = best_ndx;
sn->jiffies_since_switch[size_bin] = ticks;
sn->ticks_since_switch[size_bin] = ticks;
}
ndx = sn->current_rate[size_bin];
sn->packets_since_switch[size_bin]++;
if (size_bin == 0) {
/*
* set the visible txrate for this node
* to the rate of small packets
*/
an->an_node.ni_txrate = ndx;
}
}
}
if (ndx < 0) {
ndx = 0;
}
KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx));
*rix = sn->rates[ndx].rix;
if (shortPreamble) {
*txrate = sn->rates[ndx].shortPreambleRateCode;
@ -373,7 +373,6 @@ ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
*txrate = sn->rates[ndx].rateCode;
}
sn->packets_sent[size_bin]++;
an->an_node.ni_txrate = ndx;
}
void
@ -382,28 +381,17 @@ ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
{
struct sample_node *sn = ATH_NODE_SAMPLE(an);
int rateCode = -1;
int frame_size, size_bin, best_ndx, ndx;
int frame_size = 0;
int size_bin = 0;
int ndx = 0;
frame_size = ds->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
KASSERT(frame_size != 0, ("no frame size"));
size_bin = size_to_bin(frame_size);
best_ndx = best_rate_ndx(sn, size_bin, 0);
if (best_ndx == -1 || !sn->stats[size_bin][best_ndx].packets_acked) {
/*
* no packet has succeeded, so also try at the
* lowest bitate.
*/
ndx = 0;
} else {
/*
* we're trying a different bit-rate, and it could be lossy,
* so if it fails try at the best bit-rate.
*/
ndx = best_ndx;
size_bin = size_to_bin(frame_size); // TODO: it's correct that frame_size alway 0 ?
ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */
if (!sn->stats[size_bin][ndx].packets_acked) {
ndx = 0; /* use the lowest bit-rate */
}
KASSERT(0 <= ndx && ndx < IEEE80211_RATE_MAXSIZE,
("invalid ndx %d", ndx));
if (shortPreamble) {
rateCode = sn->rates[ndx].shortPreambleRateCode;
} else {
@ -461,17 +449,6 @@ update_stats(struct ath_softc *sc, struct ath_node *an,
short_tries-1,
MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
}
#ifdef SAMPLE_DEBUG
if (short_tries + tries > 3 || status) {
DPRINTF(sc, "%s: %s size %d rate %d ndx %d tries (%d/%d) tries0 %d tt %d avg_tt %d perfect_tt %d status %d\n",
__func__, ether_sprintf(an->an_node.ni_macaddr),
size,
rate, ndx0, short_tries, tries, tries0, tt,
sn->stats[size_bin][ndx0].average_tx_time,
sn->stats[size_bin][ndx0].perfect_tx_time,
status);
}
#endif /* SAMPLE_DEBUG */
if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) {
/* just average the first few packets */
int avg_tx = sn->stats[size_bin][ndx0].average_tx_time;
@ -485,16 +462,17 @@ update_stats(struct ath_softc *sc, struct ath_node *an,
}
if (status) {
/*
* this packet failed - count this as a failure
* for larger packets also, since we assume
* if a small packet fails at a lower bit-rate
* then a larger one will also.
*/
int y;
for (y = size_bin; y < NUM_PACKET_SIZE_BINS; y++) {
sn->stats[size_bin][ndx0].successive_failures++;
for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
/* also say larger packets failed since we
* assume if a small packet fails at a lower
* bit-rate then a larger one will also.
*/
sn->stats[y][ndx0].successive_failures++;
sn->stats[y][ndx0].last_tx = ticks;
sn->stats[y][ndx0].tries += tries;
sn->stats[y][ndx0].total_packets++;
}
} else {
sn->stats[size_bin][ndx0].packets_acked++;
@ -521,10 +499,12 @@ void
ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
const struct ath_desc *ds, const struct ath_desc *ds0)
{
struct ieee80211com *ic = &sc->sc_ic;
struct sample_node *sn = ATH_NODE_SAMPLE(an);
const struct ar5212_desc *ads = (const struct ar5212_desc *)&ds->ds_ctl0;
int final_rate, short_tries, long_tries, frame_size;
int ndx = -1;
int mrr;
final_rate = sc->sc_hwmap[ds->ds_txstat.ts_rate &~ HAL_TXSTAT_ALTRATE].ieeerate;
short_tries = ds->ds_txstat.ts_shortretry + 1;
@ -543,6 +523,8 @@ ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
return;
}
mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
if (sc->sc_mrretry && ds->ds_txstat.ts_status) {
/* this packet failed */
DPRINTF(sc, "%s: %s size %d rate/try %d/%d %d/%d %d/%d %d/%d status %s retries (%d/%d)\n",
@ -562,7 +544,7 @@ ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
long_tries);
}
if (!(ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)) {
if (!mrr || !(ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)) {
/* only one rate was used */
ndx = rate_to_ndx(sn, final_rate);
DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d/%d\n",
@ -625,7 +607,7 @@ ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
ndx2, tries2,
ndx3, tries3,
short_tries, ds->ds_txstat.ts_longretry + 1,
ds->ds_txstat.ts_status);
long_tries > tries0);
}
if (tries1 && finalTSIdx > 0) {
@ -732,6 +714,7 @@ ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
int size = bin_to_size(y);
int ndx = 0;
sn->packets_sent[y] = 0;
sn->current_sample_ndx[y] = -1;
sn->last_sample_ndx[y] = 0;
@ -749,7 +732,27 @@ ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
0, 0);
sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time;
}
/* set the initial rate */
for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
if (sn->rates[ndx].rate <= 72) {
break;
}
}
sn->current_rate[y] = ndx;
}
DPRINTF(sc, "%s: %s %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n",
__func__, ether_sprintf(ni->ni_macaddr),
sn->num_rates,
sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "",
sn->stats[1][0].perfect_tx_time,
sn->rates[sn->num_rates-1].rate/2,
sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "",
sn->stats[1][sn->num_rates-1].perfect_tx_time
);
ni->ni_txrate = sn->current_rate[0];
#undef RATE
}

View File

@ -90,7 +90,7 @@ struct sample_node {
int current_rate[NUM_PACKET_SIZE_BINS];
int packets_since_switch[NUM_PACKET_SIZE_BINS];
unsigned jiffies_since_switch[NUM_PACKET_SIZE_BINS];
unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
int packets_since_sample[NUM_PACKET_SIZE_BINS];
unsigned sample_tt[NUM_PACKET_SIZE_BINS];
@ -186,8 +186,8 @@ static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
int length,
int rix, int short_retries, int long_retries) {
const HAL_RATE_TABLE *rt = sc->sc_currates;
int rts, cts;
/* pg 205 ieee.802.11.pdf */
unsigned t_slot = 20;
unsigned t_difs = 50;
unsigned t_sifs = 10;
@ -196,16 +196,43 @@ static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
int x = 0;
int cw = WIFI_CW_MIN;
int cix = rt->info[rix].controlRate;
int rts = 0;
int cts = 0;
KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
if (rt->info[rix].phy == IEEE80211_T_OFDM) {
t_slot = 9;
t_sifs = 9;
t_difs = 28;
if (!rt->info[rix].rateKbps) {
printf("rix %d (%d) bad ratekbps %d mode %u",
rix, rt->info[rix].dot11Rate,
rt->info[rix].rateKbps,
sc->sc_curmode);
return 0;
}
/*
* XXX getting mac/phy level timings should be fixed for turbo
* rates, and there is probably a way to get this from the
* hal...
*/
switch (rt->info[rix].phy) {
case IEEE80211_T_OFDM:
t_slot = 9;
t_sifs = 16;
t_difs = 28;
/* fall through */
case IEEE80211_T_TURBO:
t_slot = 9;
t_sifs = 8;
t_difs = 28;
break;
case IEEE80211_T_DS:
/* fall through to default */
default:
/* pg 205 ieee.802.11.pdf */
t_slot = 20;
t_difs = 50;
t_sifs = 10;
}
rts = cts = 0;
if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
rt->info[rix].phy == IEEE80211_T_OFDM) {
@ -218,13 +245,22 @@ static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
}
if (length > ic->ic_rtsthreshold) {
if (0 /*length > ic->ic_rtsthreshold */) {
rts = 1;
}
if (rts || cts) {
int ctsrate = rt->info[cix].rateCode;
int ctsduration = 0;
if (!rt->info[cix].rateKbps) {
printf("cix %d (%d) bad ratekbps %d mode %u",
cix, rt->info[cix].dot11Rate,
rt->info[cix].rateKbps,
sc->sc_curmode);
return 0;
}
ctsrate |= rt->info[cix].shortPreamble;
if (rts) /* SIFS + CTS */
ctsduration += rt->info[cix].spAckDuration;
@ -238,7 +274,7 @@ static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
tt += (short_retries + 1) * ctsduration;
}
tt += t_difs;
tt += (long_retries+1)*(t_sifs + rt->info[cix].spAckDuration);
tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
tt += (long_retries+1)*ath_hal_computetxtime(sc->sc_ah, rt, length,
rix, AH_TRUE);
for (x = 0; x <= short_retries + long_retries; x++) {