1 /* $NetBSD: athrate-sample.c,v 1.16 2008/07/09 19:47:24 joerg Exp $ */
4 * Copyright (c) 2005 John Bicket
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
17 * 3. Neither the names of the above-listed copyright holders nor the names
18 * of any contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36 * THE POSSIBILITY OF SUCH DAMAGES.
39 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/sample/sample.c,v 1.9 2005/07/22 16:50:17 sam Exp $");
44 __KERNEL_RCSID(0, "$NetBSD: athrate-sample.c,v 1.16 2008/07/09 19:47:24 joerg Exp $");
49 * John Bicket's SampleRate control algorithm.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
56 #include <sys/kernel.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
62 #include <sys/socket.h>
65 #include <net/if_media.h>
66 #include <net/if_arp.h>
67 #include <net/if_ether.h> /* XXX for ether_sprintf */
69 #include <net80211/ieee80211_var.h>
74 #include <netinet/in.h>
78 #include <dev/ic/athvar.h>
79 #include <dev/ic/athrate-sample.h>
84 ATH_DEBUG_RATE
= 0x00000010 /* rate control */
86 #define DPRINTF(sc, _fmt, ...) do { \
87 if (sc->sc_debug & ATH_DEBUG_RATE) \
88 printf(_fmt, __VA_ARGS__); \
91 #define DPRINTF(sc, _fmt, ...)
95 * This file is an implementation of the SampleRate algorithm
96 * in "Bit-rate Selection in Wireless Networks"
97 * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
99 * SampleRate chooses the bit-rate it predicts will provide the most
100 * throughput based on estimates of the expected per-packet
101 * transmission time for each bit-rate. SampleRate periodically sends
102 * packets at bit-rates other than the current one to estimate when
103 * another bit-rate will provide better performance. SampleRate
104 * switches to another bit-rate when its estimated per-packet
105 * transmission time becomes smaller than the current bit-rate's.
106 * SampleRate reduces the number of bit-rates it must sample by
107 * eliminating those that could not perform better than the one
108 * currently being used. SampleRate also stops probing at a bit-rate
109 * if it experiences several successive losses.
111 * The difference between the algorithm in the thesis and the one in this
112 * file is that the one in this file uses a ewma instead of a window.
114 * Also, this implementation tracks the average transmission time for
115 * a few different packet sizes independently for each link.
118 #define STALE_FAILURE_TIMEOUT_MS 10000
119 #define MIN_SWITCH_MS 1000
121 static void ath_rate_ctl_reset(struct ath_softc
*, struct ieee80211_node
*);
124 size_to_bin(int size
)
127 for (x
= 0; x
< NUM_PACKET_SIZE_BINS
; x
++) {
128 if (size
<= packet_size_bins
[x
]) {
132 return NUM_PACKET_SIZE_BINS
-1;
134 static inline int bin_to_size(int index
) {
135 return packet_size_bins
[index
];
139 rate_to_ndx(struct sample_node
*sn
, int rate
) {
141 for (x
= 0; x
< sn
->num_rates
; x
++) {
142 if (sn
->rates
[x
].rate
== rate
) {
150 ath_rate_node_init(struct ath_softc
*sc
, struct ath_node
*an
)
152 DPRINTF(sc
, "%s:\n", __func__
);
153 /* NB: assumed to be zero'd by caller */
157 ath_rate_node_cleanup(struct ath_softc
*sc
, struct ath_node
*an
)
159 DPRINTF(sc
, "%s:\n", __func__
);
164 * returns the ndx with the lowest average_tx_time,
165 * or -1 if all the average_tx_times are 0.
167 static inline int best_rate_ndx(struct sample_node
*sn
, int size_bin
,
168 int require_acked_before
)
172 int best_rate_tt
= 0;
173 for (x
= 0; x
< sn
->num_rates
; x
++) {
174 int tt
= sn
->stats
[size_bin
][x
].average_tx_time
;
175 if (tt
<= 0 || (require_acked_before
&&
176 !sn
->stats
[size_bin
][x
].packets_acked
)) {
180 /* 9 megabits never works better than 12 */
181 if (sn
->rates
[x
].rate
== 18)
184 /* don't use a bit-rate that has been failing */
185 if (sn
->stats
[size_bin
][x
].successive_failures
> 3)
188 if (!best_rate_tt
|| best_rate_tt
> tt
) {
193 return (best_rate_tt
) ? best_ndx
: -1;
197 * pick a good "random" bit-rate to sample other than the current one
200 pick_sample_ndx(struct sample_node
*sn
, int size_bin
)
204 unsigned current_tt
= 0;
206 current_ndx
= sn
->current_rate
[size_bin
];
207 if (current_ndx
< 0) {
208 /* no successes yet, send at the lowest bit-rate */
212 current_tt
= sn
->stats
[size_bin
][current_ndx
].average_tx_time
;
214 for (x
= 0; x
< sn
->num_rates
; x
++) {
215 int ndx
= (sn
->last_sample_ndx
[size_bin
]+1+x
) % sn
->num_rates
;
217 /* don't sample the current bit-rate */
218 if (ndx
== current_ndx
)
221 /* this bit-rate is always worse than the current one */
222 if (sn
->stats
[size_bin
][ndx
].perfect_tx_time
> current_tt
)
225 /* rarely sample bit-rates that fail a lot */
226 if (ticks
- sn
->stats
[size_bin
][ndx
].last_tx
< ((hz
* STALE_FAILURE_TIMEOUT_MS
)/1000) &&
227 sn
->stats
[size_bin
][ndx
].successive_failures
> 3)
230 /* don't sample more than 2 indexes higher
231 * for rates higher than 11 megabits
233 if (sn
->rates
[ndx
].rate
> 22 && ndx
> current_ndx
+ 2)
236 /* 9 megabits never works better than 12 */
237 if (sn
->rates
[ndx
].rate
== 18)
240 /* if we're using 11 megabits, only sample up to 12 megabits
242 if (sn
->rates
[current_ndx
].rate
== 22 && ndx
> current_ndx
+ 1)
245 sn
->last_sample_ndx
[size_bin
] = ndx
;
252 ath_rate_findrate(struct ath_softc
*sc
, struct ath_node
*an
,
253 int shortPreamble
, size_t frameLen
,
254 u_int8_t
*rix
, int *try0
, u_int8_t
*txrate
)
256 struct sample_node
*sn
= ATH_NODE_SAMPLE(an
);
257 struct sample_softc
*ssc
= ATH_SOFTC_SAMPLE(sc
);
258 struct ieee80211com
*ic
= &sc
->sc_ic
;
259 int ndx
, size_bin
, mrr
, best_ndx
, change_rates
;
260 unsigned average_tx_time
;
262 mrr
= sc
->sc_mrretry
&& !(ic
->ic_flags
& IEEE80211_F_USEPROT
);
263 size_bin
= size_to_bin(frameLen
);
264 best_ndx
= best_rate_ndx(sn
, size_bin
, !mrr
);
267 average_tx_time
= sn
->stats
[size_bin
][best_ndx
].average_tx_time
;
272 if (sn
->static_rate_ndx
!= -1) {
273 ndx
= sn
->static_rate_ndx
;
274 *try0
= ATH_TXMAXTRY
;
276 *try0
= mrr
? 2 : ATH_TXMAXTRY
;
278 if (sn
->sample_tt
[size_bin
] < average_tx_time
* (sn
->packets_since_sample
[size_bin
]*ssc
->ath_sample_rate
/100)) {
280 * we want to limit the time measuring the performance
281 * of other bit-rates to ath_sample_rate% of the
282 * total transmission time.
284 ndx
= pick_sample_ndx(sn
, size_bin
);
285 if (ndx
!= sn
->current_rate
[size_bin
]) {
286 sn
->current_sample_ndx
[size_bin
] = ndx
;
288 sn
->current_sample_ndx
[size_bin
] = -1;
290 sn
->packets_since_sample
[size_bin
] = 0;
294 if (!sn
->packets_sent
[size_bin
] || best_ndx
== -1) {
295 /* no packet has been sent successfully yet */
296 for (ndx
= sn
->num_rates
-1; ndx
> 0; ndx
--) {
298 * pick the highest rate <= 36 Mbps
299 * that hasn't failed.
301 if (sn
->rates
[ndx
].rate
<= 72 &&
302 sn
->stats
[size_bin
][ndx
].successive_failures
== 0) {
308 } else if (sn
->packets_sent
[size_bin
] < 20) {
309 /* let the bit-rate switch quickly during the first few packets */
311 } else if (ticks
- ((hz
*MIN_SWITCH_MS
)/1000) > sn
->ticks_since_switch
[size_bin
]) {
312 /* 2 seconds have gone by */
314 } else if (average_tx_time
* 2 < sn
->stats
[size_bin
][sn
->current_rate
[size_bin
]].average_tx_time
) {
315 /* the current bit-rate is twice as slow as the best one */
319 sn
->packets_since_sample
[size_bin
]++;
322 if (best_ndx
!= sn
->current_rate
[size_bin
]) {
323 DPRINTF(sc
, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n",
325 ether_sprintf(an
->an_node
.ni_macaddr
),
326 packet_size_bins
[size_bin
],
327 sn
->rates
[sn
->current_rate
[size_bin
]].rate
,
328 sn
->stats
[size_bin
][sn
->current_rate
[size_bin
]].average_tx_time
,
329 sn
->stats
[size_bin
][sn
->current_rate
[size_bin
]].perfect_tx_time
,
330 sn
->rates
[best_ndx
].rate
,
331 sn
->stats
[size_bin
][best_ndx
].average_tx_time
,
332 sn
->stats
[size_bin
][best_ndx
].perfect_tx_time
,
333 sn
->packets_since_switch
[size_bin
],
336 sn
->packets_since_switch
[size_bin
] = 0;
337 sn
->current_rate
[size_bin
] = best_ndx
;
338 sn
->ticks_since_switch
[size_bin
] = ticks
;
340 ndx
= sn
->current_rate
[size_bin
];
341 sn
->packets_since_switch
[size_bin
]++;
344 * set the visible txrate for this node
345 * to the rate of small packets
347 an
->an_node
.ni_txrate
= ndx
;
352 KASSERT(ndx
>= 0 && ndx
< sn
->num_rates
, ("ndx is %d", ndx
));
354 *rix
= sn
->rates
[ndx
].rix
;
356 *txrate
= sn
->rates
[ndx
].shortPreambleRateCode
;
358 *txrate
= sn
->rates
[ndx
].rateCode
;
360 sn
->packets_sent
[size_bin
]++;
364 ath_rate_setupxtxdesc(struct ath_softc
*sc
, struct ath_node
*an
,
365 struct ath_desc
*ds
, int shortPreamble
, u_int8_t rix
)
367 struct sample_node
*sn
= ATH_NODE_SAMPLE(an
);
373 size_bin
= size_to_bin(frame_size
); // TODO: it's correct that frame_size alway 0 ?
374 ndx
= sn
->current_rate
[size_bin
]; /* retry at the current bit-rate */
376 if (!sn
->stats
[size_bin
][ndx
].packets_acked
) {
377 ndx
= 0; /* use the lowest bit-rate */
381 rateCode
= sn
->rates
[ndx
].shortPreambleRateCode
;
383 rateCode
= sn
->rates
[ndx
].rateCode
;
385 ath_hal_setupxtxdesc(sc
->sc_ah
, ds
386 , rateCode
, 3 /* series 1 */
387 , sn
->rates
[0].rateCode
, 3 /* series 2 */
388 , 0, 0 /* series 3 */
393 update_stats(struct ath_softc
*sc
, struct ath_node
*an
,
395 int ndx0
, int tries0
,
396 int ndx1
, int tries1
,
397 int ndx2
, int tries2
,
398 int ndx3
, int tries3
,
399 int short_tries
, int tries
, int status
)
401 struct sample_node
*sn
= ATH_NODE_SAMPLE(an
);
402 struct sample_softc
*ssc
= ATH_SOFTC_SAMPLE(sc
);
404 int tries_so_far
= 0;
412 size_bin
= size_to_bin(frame_size
);
413 size
= bin_to_size(size_bin
);
414 rate
= sn
->rates
[ndx0
].rate
;
416 tt
+= calc_usecs_unicast_packet(sc
, size
, sn
->rates
[ndx0
].rix
,
417 short_tries
- 1, MIN(tries0
, tries
) - 1);
418 tries_so_far
+= tries0
;
419 if (tries1
&& tries0
< tries
) {
420 tt
+= calc_usecs_unicast_packet(sc
, size
,
421 ndx1
== -1 ? 0 : sn
->rates
[ndx1
].rix
, short_tries
- 1,
422 MIN(tries1
+ tries_so_far
, tries
) - tries_so_far
- 1);
424 tries_so_far
+= tries1
;
426 if (tries2
&& tries0
+ tries1
< tries
) {
427 tt
+= calc_usecs_unicast_packet(sc
, size
,
428 ndx2
== -1 ? 0 : sn
->rates
[ndx2
].rix
, short_tries
- 1,
429 MIN(tries2
+ tries_so_far
, tries
) - tries_so_far
- 1);
432 tries_so_far
+= tries2
;
434 if (tries3
&& tries0
+ tries1
+ tries2
< tries
) {
435 tt
+= calc_usecs_unicast_packet(sc
, size
,
436 ndx3
== -1 ? 0 : sn
->rates
[ndx3
].rix
, short_tries
- 1,
437 MIN(tries3
+ tries_so_far
, tries
) - tries_so_far
- 1);
440 if (sn
->stats
[size_bin
][ndx0
].total_packets
< (100 / (100 - ssc
->ath_smoothing_rate
))) {
441 /* just average the first few packets */
442 int avg_tx
= sn
->stats
[size_bin
][ndx0
].average_tx_time
;
443 int packets
= sn
->stats
[size_bin
][ndx0
].total_packets
;
444 sn
->stats
[size_bin
][ndx0
].average_tx_time
= (tt
+(avg_tx
*packets
))/(packets
+1);
447 sn
->stats
[size_bin
][ndx0
].average_tx_time
=
448 ((sn
->stats
[size_bin
][ndx0
].average_tx_time
* ssc
->ath_smoothing_rate
) +
449 (tt
* (100 - ssc
->ath_smoothing_rate
))) / 100;
454 sn
->stats
[size_bin
][ndx0
].successive_failures
++;
455 for (y
= size_bin
+1; y
< NUM_PACKET_SIZE_BINS
; y
++) {
456 /* also say larger packets failed since we
457 * assume if a small packet fails at a lower
458 * bit-rate then a larger one will also.
460 sn
->stats
[y
][ndx0
].successive_failures
++;
461 sn
->stats
[y
][ndx0
].last_tx
= ticks
;
462 sn
->stats
[y
][ndx0
].tries
+= tries
;
463 sn
->stats
[y
][ndx0
].total_packets
++;
466 sn
->stats
[size_bin
][ndx0
].packets_acked
++;
467 sn
->stats
[size_bin
][ndx0
].successive_failures
= 0;
469 sn
->stats
[size_bin
][ndx0
].tries
+= tries
;
470 sn
->stats
[size_bin
][ndx0
].last_tx
= ticks
;
471 sn
->stats
[size_bin
][ndx0
].total_packets
++;
474 if (ndx0
== sn
->current_sample_ndx
[size_bin
]) {
475 DPRINTF(sc
, "%s: %s size %d sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d) status %d\n",
476 __func__
, ether_sprintf(an
->an_node
.ni_macaddr
),
477 size
, rate
, short_tries
, tries
, tt
,
478 sn
->stats
[size_bin
][ndx0
].average_tx_time
,
479 sn
->stats
[size_bin
][ndx0
].perfect_tx_time
,
481 sn
->sample_tt
[size_bin
] = tt
;
482 sn
->current_sample_ndx
[size_bin
] = -1;
487 ath_rate_tx_complete(struct ath_softc
*sc
, struct ath_node
*an
,
488 const struct ath_desc
*ds
, const struct ath_desc
*ds0
)
490 struct ieee80211com
*ic
= &sc
->sc_ic
;
491 struct sample_node
*sn
= ATH_NODE_SAMPLE(an
);
492 const struct ar5212_desc
*ads
= (const struct ar5212_desc
*)&ds
->ds_ctl0
;
493 int final_rate
, short_tries
, long_tries
, frame_size
;
497 final_rate
= sc
->sc_hwmap
[ds
->ds_txstat
.ts_rate
&~ HAL_TXSTAT_ALTRATE
].ieeerate
;
498 short_tries
= ds
->ds_txstat
.ts_shortretry
+ 1;
499 long_tries
= ds
->ds_txstat
.ts_longretry
+ 1;
500 frame_size
= ds0
->ds_ctl0
& 0x0fff; /* low-order 12 bits of ds_ctl0 */
501 if (frame_size
== 0) /* NB: should not happen */
504 if (sn
->num_rates
<= 0) {
505 DPRINTF(sc
, "%s: %s size %d status %d rate/try %d/%d "
507 __func__
, ether_sprintf(an
->an_node
.ni_macaddr
),
508 bin_to_size(size_to_bin(frame_size
)),
509 ds
->ds_txstat
.ts_status
,
510 short_tries
, long_tries
);
514 mrr
= sc
->sc_mrretry
&& !(ic
->ic_flags
& IEEE80211_F_USEPROT
);
516 if (sc
->sc_mrretry
&& ds
->ds_txstat
.ts_status
) {
517 /* this packet failed */
518 DPRINTF(sc
, "%s: %s size %d rate/try %d/%d %d/%d %d/%d %d/%d status %s retries (%d/%d)\n",
520 ether_sprintf(an
->an_node
.ni_macaddr
),
521 bin_to_size(size_to_bin(frame_size
)),
522 sc
->sc_hwmap
[ads
->xmit_rate0
].ieeerate
,
524 sc
->sc_hwmap
[ads
->xmit_rate1
].ieeerate
,
526 sc
->sc_hwmap
[ads
->xmit_rate2
].ieeerate
,
528 sc
->sc_hwmap
[ads
->xmit_rate3
].ieeerate
,
530 ds
->ds_txstat
.ts_status
? "FAIL" : "OK",
535 if (!mrr
|| !(ds
->ds_txstat
.ts_rate
& HAL_TXSTAT_ALTRATE
)) {
536 /* only one rate was used */
537 ndx
= rate_to_ndx(sn
, final_rate
);
538 DPRINTF(sc
, "%s: %s size %d status %d rate/try %d/%d/%d\n",
539 __func__
, ether_sprintf(an
->an_node
.ni_macaddr
),
540 bin_to_size(size_to_bin(frame_size
)),
541 ds
->ds_txstat
.ts_status
,
542 ndx
, short_tries
, long_tries
);
543 if (ndx
>= 0 && ndx
< sn
->num_rates
) {
544 update_stats(sc
, an
, frame_size
,
549 short_tries
, long_tries
, ds
->ds_txstat
.ts_status
);
552 int rate0
, tries0
, ndx0
;
553 int rate1
, tries1
, ndx1
;
554 int rate2
, tries2
, ndx2
;
555 int rate3
, tries3
, ndx3
;
556 int finalTSIdx
= ads
->final_ts_index
;
559 * Process intermediate rates that failed.
562 rate0
= sc
->sc_hwmap
[ads
->xmit_rate0
].ieeerate
;
563 tries0
= ads
->xmit_tries0
;
564 ndx0
= rate_to_ndx(sn
, rate0
);
566 rate1
= sc
->sc_hwmap
[ads
->xmit_rate1
].ieeerate
;
567 tries1
= ads
->xmit_tries1
;
568 ndx1
= rate_to_ndx(sn
, rate1
);
570 rate2
= sc
->sc_hwmap
[ads
->xmit_rate2
].ieeerate
;
571 tries2
= ads
->xmit_tries2
;
572 ndx2
= rate_to_ndx(sn
, rate2
);
574 rate3
= sc
->sc_hwmap
[ads
->xmit_rate3
].ieeerate
;
575 tries3
= ads
->xmit_tries3
;
576 ndx3
= rate_to_ndx(sn
, rate3
);
579 DPRINTF(sc
, "%s: %s size %d finaltsidx %d tries %d status %d rate/try %d/%d %d/%d %d/%d %d/%d\n",
580 __func__
, ether_sprintf(an
->an_node
.ni_macaddr
),
581 bin_to_size(size_to_bin(frame_size
)),
584 ds
->ds_txstat
.ts_status
,
592 update_stats(sc
, an
, frame_size
,
597 short_tries
, ds
->ds_txstat
.ts_longretry
+ 1,
598 long_tries
> tries0
);
601 if (tries1
&& finalTSIdx
> 0) {
602 update_stats(sc
, an
, frame_size
,
607 short_tries
, ds
->ds_txstat
.ts_longretry
+ 1 - tries0
,
608 ds
->ds_txstat
.ts_status
);
611 if (tries2
&& finalTSIdx
> 1) {
612 update_stats(sc
, an
, frame_size
,
617 short_tries
, ds
->ds_txstat
.ts_longretry
+ 1 - tries0
- tries1
,
618 ds
->ds_txstat
.ts_status
);
621 if (tries3
&& finalTSIdx
> 2) {
622 update_stats(sc
, an
, frame_size
,
627 short_tries
, ds
->ds_txstat
.ts_longretry
+ 1 - tries0
- tries1
- tries2
,
628 ds
->ds_txstat
.ts_status
);
634 ath_rate_newassoc(struct ath_softc
*sc
, struct ath_node
*an
, int isnew
)
636 DPRINTF(sc
, "%s: %s isnew %d\n", __func__
,
637 ether_sprintf(an
->an_node
.ni_macaddr
), isnew
);
639 ath_rate_ctl_reset(sc
, &an
->an_node
);
643 * Initialize the tables for a node.
646 ath_rate_ctl_reset(struct ath_softc
*sc
, struct ieee80211_node
*ni
)
648 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
649 struct ieee80211com
*ic
= &sc
->sc_ic
;
650 struct ath_node
*an
= ATH_NODE(ni
);
651 struct sample_node
*sn
= ATH_NODE_SAMPLE(an
);
652 const HAL_RATE_TABLE
*rt
= sc
->sc_currates
;
655 KASSERT(rt
!= NULL
, ("no rate table, mode %u", sc
->sc_curmode
));
656 sn
->static_rate_ndx
= -1;
657 if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
) {
659 * A fixed rate is to be used; ic_fixed_rate is an
660 * index into the supported rate set. Convert this
661 * to the index into the negotiated rate set for
662 * the node. We know the rate is there because the
663 * rate set is checked when the station associates.
665 const struct ieee80211_rateset
*rs
=
666 &ic
->ic_sup_rates
[ic
->ic_curmode
];
667 int r
= rs
->rs_rates
[ic
->ic_fixed_rate
] & IEEE80211_RATE_VAL
;
668 /* NB: the rate set is assumed sorted */
669 srate
= ni
->ni_rates
.rs_nrates
- 1;
670 for (; srate
>= 0 && RATE(srate
) != r
; srate
--)
673 ("fixed rate %d not in rate set", ic
->ic_fixed_rate
));
674 sn
->static_rate_ndx
= srate
;
677 DPRINTF(sc
, "%s: %s size 1600 rate/tt", __func__
, ether_sprintf(ni
->ni_macaddr
));
679 sn
->num_rates
= ni
->ni_rates
.rs_nrates
;
680 for (x
= 0; x
< ni
->ni_rates
.rs_nrates
; x
++) {
681 sn
->rates
[x
].rate
= ni
->ni_rates
.rs_rates
[x
] & IEEE80211_RATE_VAL
;
682 sn
->rates
[x
].rix
= sc
->sc_rixmap
[sn
->rates
[x
].rate
];
683 sn
->rates
[x
].rateCode
= rt
->info
[sn
->rates
[x
].rix
].rateCode
;
684 sn
->rates
[x
].shortPreambleRateCode
=
685 rt
->info
[sn
->rates
[x
].rix
].rateCode
|
686 rt
->info
[sn
->rates
[x
].rix
].shortPreamble
;
688 DPRINTF(sc
, " %d/%d", sn
->rates
[x
].rate
,
689 calc_usecs_unicast_packet(sc
, 1600, sn
->rates
[x
].rix
,
692 DPRINTF(sc
, "%s\n", "");
694 /* set the visible bit-rate to the lowest one available */
696 sn
->num_rates
= ni
->ni_rates
.rs_nrates
;
698 for (y
= 0; y
< NUM_PACKET_SIZE_BINS
; y
++) {
699 int size
= bin_to_size(y
);
701 sn
->packets_sent
[y
] = 0;
702 sn
->current_sample_ndx
[y
] = -1;
703 sn
->last_sample_ndx
[y
] = 0;
705 for (x
= 0; x
< ni
->ni_rates
.rs_nrates
; x
++) {
706 sn
->stats
[y
][x
].successive_failures
= 0;
707 sn
->stats
[y
][x
].tries
= 0;
708 sn
->stats
[y
][x
].total_packets
= 0;
709 sn
->stats
[y
][x
].packets_acked
= 0;
710 sn
->stats
[y
][x
].last_tx
= 0;
712 sn
->stats
[y
][x
].perfect_tx_time
=
713 calc_usecs_unicast_packet(sc
, size
,
716 sn
->stats
[y
][x
].average_tx_time
= sn
->stats
[y
][x
].perfect_tx_time
;
719 /* set the initial rate */
720 for (ndx
= sn
->num_rates
-1; ndx
> 0; ndx
--) {
721 if (sn
->rates
[ndx
].rate
<= 72) {
725 sn
->current_rate
[y
] = ndx
;
728 DPRINTF(sc
, "%s: %s %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n",
729 __func__
, ether_sprintf(ni
->ni_macaddr
),
731 sn
->rates
[0].rate
/2, sn
->rates
[0].rate
% 0x1 ? ".5" : "",
732 sn
->stats
[1][0].perfect_tx_time
,
733 sn
->rates
[sn
->num_rates
-1].rate
/2,
734 sn
->rates
[sn
->num_rates
-1].rate
% 0x1 ? ".5" : "",
735 sn
->stats
[1][sn
->num_rates
-1].perfect_tx_time
738 ni
->ni_txrate
= sn
->current_rate
[0];
743 rate_cb(void *arg
, struct ieee80211_node
*ni
)
745 struct ath_softc
*sc
= arg
;
747 ath_rate_newassoc(sc
, ATH_NODE(ni
), 1);
751 * Reset the rate control state for each 802.11 state transition.
754 ath_rate_newstate(struct ath_softc
*sc
, enum ieee80211_state state
)
756 struct ieee80211com
*ic
= &sc
->sc_ic
;
758 if (state
== IEEE80211_S_RUN
) {
759 if (ic
->ic_opmode
!= IEEE80211_M_STA
) {
761 * Sync rates for associated stations and neighbors.
763 ieee80211_iterate_nodes(&ic
->ic_sta
, rate_cb
, sc
);
765 ath_rate_newassoc(sc
, ATH_NODE(ic
->ic_bss
), 1);
770 ath_rate_sysctlattach(struct ath_softc
*sc
, struct sample_softc
*osc
)
773 struct sysctllog
**log
= &sc
->sc_sysctllog
;
774 const struct sysctlnode
*cnode
, *rnode
;
776 if ((rnode
= ath_sysctl_instance(device_xname(sc
->sc_dev
), log
)) == NULL
)
779 /* XXX bounds check [0..100] */
780 if ((rc
= SYSCTL_PFX_INT(osc
->ath_
, CTLFLAG_READWRITE
, smoothing_rate
,
781 "rate control: retry threshold to credit rate raise (%%)")) != 0)
784 /* XXX bounds check [2..100] */
785 if ((rc
= SYSCTL_PFX_INT(osc
->ath_
, CTLFLAG_READWRITE
, sample_rate
,
786 "rate control: # good periods before raising rate")) != 0)
791 printf("%s: sysctl_createv failed, rc = %d\n", __func__
, rc
);
794 struct ath_ratectrl
*
795 ath_rate_attach(struct ath_softc
*sc
)
797 struct sample_softc
*osc
;
799 DPRINTF(sc
, "%s:\n", __func__
);
800 osc
= malloc(sizeof(struct sample_softc
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
803 osc
->arc
.arc_space
= sizeof(struct sample_node
);
804 osc
->ath_smoothing_rate
= 95; /* ewma percentage (out of 100) */
805 osc
->ath_sample_rate
= 10; /* send a different bit-rate 1/X packets */
806 ath_rate_sysctlattach(sc
, osc
);
811 ath_rate_detach(struct ath_ratectrl
*arc
)
813 struct sample_softc
*osc
= (struct sample_softc
*) arc
;