athrate-sample.c revision 1.9 1 /* $NetBSD: athrate-sample.c,v 1.9 2006/04/02 05:52:50 gdamore Exp $ */
2
3 /*-
4 * Copyright (c) 2005 John Bicket
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
20 *
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.
24 *
25 * NO WARRANTY
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.
37 */
38
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/sample/sample.c,v 1.9 2005/07/22 16:50:17 sam Exp $");
42 #endif
43 #ifdef __NetBSD__
44 __KERNEL_RCSID(0, "$NetBSD: athrate-sample.c,v 1.9 2006/04/02 05:52:50 gdamore Exp $");
45 #endif
46
47
48 /*
49 * John Bicket's SampleRate control algorithm.
50 */
51 #include "opt_inet.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/errno.h>
59 #include <sys/device.h>
60
61 #include <machine/bus.h>
62
63 #include <sys/socket.h>
64
65 #include <net/if.h>
66 #include <net/if_media.h>
67 #include <net/if_arp.h>
68 #include <net/if_ether.h> /* XXX for ether_sprintf */
69
70 #include <net80211/ieee80211_var.h>
71
72 #include <net/bpf.h>
73
74 #ifdef INET
75 #include <netinet/in.h>
76 #endif
77
78 #include <dev/ic/athvar.h>
79 #include <dev/ic/athrate-sample.h>
80 #include <contrib/dev/ath/ah_desc.h>
81
82 #define SAMPLE_DEBUG
83 #ifdef SAMPLE_DEBUG
84 enum {
85 ATH_DEBUG_RATE = 0x00000010 /* rate control */
86 };
87 #define DPRINTF(sc, _fmt, ...) do { \
88 if (sc->sc_debug & ATH_DEBUG_RATE) \
89 printf(_fmt, __VA_ARGS__); \
90 } while (0)
91 #else
92 #define DPRINTF(sc, _fmt, ...)
93 #endif
94
95 /*
96 * This file is an implementation of the SampleRate algorithm
97 * in "Bit-rate Selection in Wireless Networks"
98 * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
99 *
100 * SampleRate chooses the bit-rate it predicts will provide the most
101 * throughput based on estimates of the expected per-packet
102 * transmission time for each bit-rate. SampleRate periodically sends
103 * packets at bit-rates other than the current one to estimate when
104 * another bit-rate will provide better performance. SampleRate
105 * switches to another bit-rate when its estimated per-packet
106 * transmission time becomes smaller than the current bit-rate's.
107 * SampleRate reduces the number of bit-rates it must sample by
108 * eliminating those that could not perform better than the one
109 * currently being used. SampleRate also stops probing at a bit-rate
110 * if it experiences several successive losses.
111 *
112 * The difference between the algorithm in the thesis and the one in this
113 * file is that the one in this file uses a ewma instead of a window.
114 *
115 * Also, this implementation tracks the average transmission time for
116 * a few different packet sizes independently for each link.
117 */
118
119 #define STALE_FAILURE_TIMEOUT_MS 10000
120 #define MIN_SWITCH_MS 1000
121
122 static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
123
124 static inline int
125 size_to_bin(int size)
126 {
127 int x = 0;
128 for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) {
129 if (size <= packet_size_bins[x]) {
130 return x;
131 }
132 }
133 return NUM_PACKET_SIZE_BINS-1;
134 }
135 static inline int bin_to_size(int index) {
136 return packet_size_bins[index];
137 }
138
139 static inline int
140 rate_to_ndx(struct sample_node *sn, int rate) {
141 int x = 0;
142 for (x = 0; x < sn->num_rates; x++) {
143 if (sn->rates[x].rate == rate) {
144 return x;
145 }
146 }
147 return -1;
148 }
149
150 void
151 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
152 {
153 DPRINTF(sc, "%s:\n", __func__);
154 /* NB: assumed to be zero'd by caller */
155 }
156
157 void
158 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
159 {
160 DPRINTF(sc, "%s:\n", __func__);
161 }
162
163
164 /*
165 * returns the ndx with the lowest average_tx_time,
166 * or -1 if all the average_tx_times are 0.
167 */
168 static inline int best_rate_ndx(struct sample_node *sn, int size_bin,
169 int require_acked_before)
170 {
171 int x = 0;
172 int best_ndx = 0;
173 int best_rate_tt = 0;
174 for (x = 0; x < sn->num_rates; x++) {
175 int tt = sn->stats[size_bin][x].average_tx_time;
176 if (tt <= 0 || (require_acked_before &&
177 !sn->stats[size_bin][x].packets_acked)) {
178 continue;
179 }
180
181 /* 9 megabits never works better than 12 */
182 if (sn->rates[x].rate == 18)
183 continue;
184
185 /* don't use a bit-rate that has been failing */
186 if (sn->stats[size_bin][x].successive_failures > 3)
187 continue;
188
189 if (!best_rate_tt || best_rate_tt > tt) {
190 best_rate_tt = tt;
191 best_ndx = x;
192 }
193 }
194 return (best_rate_tt) ? best_ndx : -1;
195 }
196
197 /*
198 * pick a good "random" bit-rate to sample other than the current one
199 */
200 static inline int
201 pick_sample_ndx(struct sample_node *sn, int size_bin)
202 {
203 int x = 0;
204 int current_ndx = 0;
205 unsigned current_tt = 0;
206
207 current_ndx = sn->current_rate[size_bin];
208 if (current_ndx < 0) {
209 /* no successes yet, send at the lowest bit-rate */
210 return 0;
211 }
212
213 current_tt = sn->stats[size_bin][current_ndx].average_tx_time;
214
215 for (x = 0; x < sn->num_rates; x++) {
216 int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates;
217
218 /* don't sample the current bit-rate */
219 if (ndx == current_ndx)
220 continue;
221
222 /* this bit-rate is always worse than the current one */
223 if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt)
224 continue;
225
226 /* rarely sample bit-rates that fail a lot */
227 if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) &&
228 sn->stats[size_bin][ndx].successive_failures > 3)
229 continue;
230
231 /* don't sample more than 2 indexes higher
232 * for rates higher than 11 megabits
233 */
234 if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2)
235 continue;
236
237 /* 9 megabits never works better than 12 */
238 if (sn->rates[ndx].rate == 18)
239 continue;
240
241 /* if we're using 11 megabits, only sample up to 12 megabits
242 */
243 if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1)
244 continue;
245
246 sn->last_sample_ndx[size_bin] = ndx;
247 return ndx;
248 }
249 return current_ndx;
250 }
251
252 void
253 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
254 int shortPreamble, size_t frameLen,
255 u_int8_t *rix, int *try0, u_int8_t *txrate)
256 {
257 struct sample_node *sn = ATH_NODE_SAMPLE(an);
258 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
259 struct ieee80211com *ic = &sc->sc_ic;
260 int ndx, size_bin, mrr, best_ndx, change_rates;
261 unsigned average_tx_time;
262
263 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
264 size_bin = size_to_bin(frameLen);
265 best_ndx = best_rate_ndx(sn, size_bin, !mrr);
266
267 if (best_ndx >= 0) {
268 average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time;
269 } else {
270 average_tx_time = 0;
271 }
272
273 if (sn->static_rate_ndx != -1) {
274 ndx = sn->static_rate_ndx;
275 *try0 = ATH_TXMAXTRY;
276 } else {
277 *try0 = mrr ? 2 : ATH_TXMAXTRY;
278
279 if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) {
280 /*
281 * we want to limit the time measuring the performance
282 * of other bit-rates to ath_sample_rate% of the
283 * total transmission time.
284 */
285 ndx = pick_sample_ndx(sn, size_bin);
286 if (ndx != sn->current_rate[size_bin]) {
287 sn->current_sample_ndx[size_bin] = ndx;
288 } else {
289 sn->current_sample_ndx[size_bin] = -1;
290 }
291 sn->packets_since_sample[size_bin] = 0;
292
293 } else {
294 change_rates = 0;
295 if (!sn->packets_sent[size_bin] || best_ndx == -1) {
296 /* no packet has been sent successfully yet */
297 for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
298 /*
299 * pick the highest rate <= 36 Mbps
300 * that hasn't failed.
301 */
302 if (sn->rates[ndx].rate <= 72 &&
303 sn->stats[size_bin][ndx].successive_failures == 0) {
304 break;
305 }
306 }
307 change_rates = 1;
308 best_ndx = ndx;
309 } else if (sn->packets_sent[size_bin] < 20) {
310 /* let the bit-rate switch quickly during the first few packets */
311 change_rates = 1;
312 } else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) {
313 /* 2 seconds have gone by */
314 change_rates = 1;
315 } else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) {
316 /* the current bit-rate is twice as slow as the best one */
317 change_rates = 1;
318 }
319
320 sn->packets_since_sample[size_bin]++;
321
322 if (change_rates) {
323 if (best_ndx != sn->current_rate[size_bin]) {
324 DPRINTF(sc, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n",
325 __func__,
326 ether_sprintf(an->an_node.ni_macaddr),
327 packet_size_bins[size_bin],
328 sn->rates[sn->current_rate[size_bin]].rate,
329 sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time,
330 sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time,
331 sn->rates[best_ndx].rate,
332 sn->stats[size_bin][best_ndx].average_tx_time,
333 sn->stats[size_bin][best_ndx].perfect_tx_time,
334 sn->packets_since_switch[size_bin],
335 mrr);
336 }
337 sn->packets_since_switch[size_bin] = 0;
338 sn->current_rate[size_bin] = best_ndx;
339 sn->ticks_since_switch[size_bin] = ticks;
340 }
341 ndx = sn->current_rate[size_bin];
342 sn->packets_since_switch[size_bin]++;
343 if (size_bin == 0) {
344 /*
345 * set the visible txrate for this node
346 * to the rate of small packets
347 */
348 an->an_node.ni_txrate = ndx;
349 }
350 }
351 }
352
353 KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx));
354
355 *rix = sn->rates[ndx].rix;
356 if (shortPreamble) {
357 *txrate = sn->rates[ndx].shortPreambleRateCode;
358 } else {
359 *txrate = sn->rates[ndx].rateCode;
360 }
361 sn->packets_sent[size_bin]++;
362 }
363
364 void
365 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
366 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
367 {
368 struct sample_node *sn = ATH_NODE_SAMPLE(an);
369 int rateCode = -1;
370 int frame_size = 0;
371 int size_bin = 0;
372 int ndx = 0;
373
374 size_bin = size_to_bin(frame_size); // TODO: it's correct that frame_size alway 0 ?
375 ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */
376
377 if (!sn->stats[size_bin][ndx].packets_acked) {
378 ndx = 0; /* use the lowest bit-rate */
379 }
380
381 if (shortPreamble) {
382 rateCode = sn->rates[ndx].shortPreambleRateCode;
383 } else {
384 rateCode = sn->rates[ndx].rateCode;
385 }
386 ath_hal_setupxtxdesc(sc->sc_ah, ds
387 , rateCode, 3 /* series 1 */
388 , sn->rates[0].rateCode, 3 /* series 2 */
389 , 0, 0 /* series 3 */
390 );
391 }
392
393 static void
394 update_stats(struct ath_softc *sc, struct ath_node *an,
395 int frame_size,
396 int ndx0, int tries0,
397 int ndx1, int tries1,
398 int ndx2, int tries2,
399 int ndx3, int tries3,
400 int short_tries, int tries, int status)
401 {
402 struct sample_node *sn = ATH_NODE_SAMPLE(an);
403 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
404 int tt = 0;
405 int tries_so_far = 0;
406 int size_bin = 0;
407 int size = 0;
408 int rate = 0;
409
410 size_bin = size_to_bin(frame_size);
411 size = bin_to_size(size_bin);
412 rate = sn->rates[ndx0].rate;
413
414 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix,
415 short_tries-1,
416 MIN(tries0, tries) - 1);
417 tries_so_far += tries0;
418 if (tries1 && tries0 < tries) {
419 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix,
420 short_tries-1,
421 MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
422 }
423 tries_so_far += tries1;
424
425 if (tries2 && tries0 + tries1 < tries) {
426 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix,
427 short_tries-1,
428 MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
429 }
430
431 tries_so_far += tries2;
432
433 if (tries3 && tries0 + tries1 + tries2 < tries) {
434 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix,
435 short_tries-1,
436 MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
437 }
438 if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) {
439 /* just average the first few packets */
440 int avg_tx = sn->stats[size_bin][ndx0].average_tx_time;
441 int packets = sn->stats[size_bin][ndx0].total_packets;
442 sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
443 } else {
444 /* use a ewma */
445 sn->stats[size_bin][ndx0].average_tx_time =
446 ((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) +
447 (tt * (100 - ssc->ath_smoothing_rate))) / 100;
448 }
449
450 if (status) {
451 int y;
452 sn->stats[size_bin][ndx0].successive_failures++;
453 for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
454 /* also say larger packets failed since we
455 * assume if a small packet fails at a lower
456 * bit-rate then a larger one will also.
457 */
458 sn->stats[y][ndx0].successive_failures++;
459 sn->stats[y][ndx0].last_tx = ticks;
460 sn->stats[y][ndx0].tries += tries;
461 sn->stats[y][ndx0].total_packets++;
462 }
463 } else {
464 sn->stats[size_bin][ndx0].packets_acked++;
465 sn->stats[size_bin][ndx0].successive_failures = 0;
466 }
467 sn->stats[size_bin][ndx0].tries += tries;
468 sn->stats[size_bin][ndx0].last_tx = ticks;
469 sn->stats[size_bin][ndx0].total_packets++;
470
471
472 if (ndx0 == sn->current_sample_ndx[size_bin]) {
473 DPRINTF(sc, "%s: %s size %d sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d) status %d\n",
474 __func__, ether_sprintf(an->an_node.ni_macaddr),
475 size, rate, short_tries, tries, tt,
476 sn->stats[size_bin][ndx0].average_tx_time,
477 sn->stats[size_bin][ndx0].perfect_tx_time,
478 status);
479 sn->sample_tt[size_bin] = tt;
480 sn->current_sample_ndx[size_bin] = -1;
481 }
482 }
483
484 void
485 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
486 const struct ath_desc *ds, const struct ath_desc *ds0)
487 {
488 struct ieee80211com *ic = &sc->sc_ic;
489 struct sample_node *sn = ATH_NODE_SAMPLE(an);
490 const struct ar5212_desc *ads = (const struct ar5212_desc *)&ds->ds_ctl0;
491 int final_rate, short_tries, long_tries, frame_size;
492 int ndx = -1;
493 int mrr;
494
495 final_rate = sc->sc_hwmap[ds->ds_txstat.ts_rate &~ HAL_TXSTAT_ALTRATE].ieeerate;
496 short_tries = ds->ds_txstat.ts_shortretry + 1;
497 long_tries = ds->ds_txstat.ts_longretry + 1;
498 frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
499 if (frame_size == 0) /* NB: should not happen */
500 frame_size = 1500;
501
502 if (sn->num_rates <= 0) {
503 DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d "
504 "no rates yet\n",
505 __func__, ether_sprintf(an->an_node.ni_macaddr),
506 bin_to_size(size_to_bin(frame_size)),
507 ds->ds_txstat.ts_status,
508 short_tries, long_tries);
509 return;
510 }
511
512 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
513
514 if (sc->sc_mrretry && ds->ds_txstat.ts_status) {
515 /* this packet failed */
516 DPRINTF(sc, "%s: %s size %d rate/try %d/%d %d/%d %d/%d %d/%d status %s retries (%d/%d)\n",
517 __func__,
518 ether_sprintf(an->an_node.ni_macaddr),
519 bin_to_size(size_to_bin(frame_size)),
520 sc->sc_hwmap[ads->xmit_rate0].ieeerate,
521 ads->xmit_tries0,
522 sc->sc_hwmap[ads->xmit_rate1].ieeerate,
523 ads->xmit_tries1,
524 sc->sc_hwmap[ads->xmit_rate2].ieeerate,
525 ads->xmit_tries2,
526 sc->sc_hwmap[ads->xmit_rate3].ieeerate,
527 ads->xmit_tries3,
528 ds->ds_txstat.ts_status ? "FAIL" : "OK",
529 short_tries,
530 long_tries);
531 }
532
533 if (!mrr || !(ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)) {
534 /* only one rate was used */
535 ndx = rate_to_ndx(sn, final_rate);
536 DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d/%d\n",
537 __func__, ether_sprintf(an->an_node.ni_macaddr),
538 bin_to_size(size_to_bin(frame_size)),
539 ds->ds_txstat.ts_status,
540 ndx, short_tries, long_tries);
541 if (ndx >= 0 && ndx < sn->num_rates) {
542 update_stats(sc, an, frame_size,
543 ndx, long_tries,
544 0, 0,
545 0, 0,
546 0, 0,
547 short_tries, long_tries, ds->ds_txstat.ts_status);
548 }
549 } else {
550 int rate0, tries0, ndx0;
551 int rate1, tries1, ndx1;
552 int rate2, tries2, ndx2;
553 int rate3, tries3, ndx3;
554 int finalTSIdx = ads->final_ts_index;
555
556 /*
557 * Process intermediate rates that failed.
558 */
559
560 rate0 = sc->sc_hwmap[ads->xmit_rate0].ieeerate;
561 tries0 = ads->xmit_tries0;
562 ndx0 = rate_to_ndx(sn, rate0);
563
564 rate1 = sc->sc_hwmap[ads->xmit_rate1].ieeerate;
565 tries1 = ads->xmit_tries1;
566 ndx1 = rate_to_ndx(sn, rate1);
567
568 rate2 = sc->sc_hwmap[ads->xmit_rate2].ieeerate;
569 tries2 = ads->xmit_tries2;
570 ndx2 = rate_to_ndx(sn, rate2);
571
572 rate3 = sc->sc_hwmap[ads->xmit_rate3].ieeerate;
573 tries3 = ads->xmit_tries3;
574 ndx3 = rate_to_ndx(sn, rate3);
575
576 #if 1
577 DPRINTF(sc, "%s: %s size %d finaltsidx %d tries %d status %d rate/try %d/%d %d/%d %d/%d %d/%d\n",
578 __func__, ether_sprintf(an->an_node.ni_macaddr),
579 bin_to_size(size_to_bin(frame_size)),
580 finalTSIdx,
581 long_tries,
582 ds->ds_txstat.ts_status,
583 rate0, tries0,
584 rate1, tries1,
585 rate2, tries2,
586 rate3, tries3);
587 #endif
588
589 if (tries0) {
590 update_stats(sc, an, frame_size,
591 ndx0, tries0,
592 ndx1, tries1,
593 ndx2, tries2,
594 ndx3, tries3,
595 short_tries, ds->ds_txstat.ts_longretry + 1,
596 long_tries > tries0);
597 }
598
599 if (tries1 && finalTSIdx > 0) {
600 update_stats(sc, an, frame_size,
601 ndx1, tries1,
602 ndx2, tries2,
603 ndx3, tries3,
604 0, 0,
605 short_tries, ds->ds_txstat.ts_longretry + 1 - tries0,
606 ds->ds_txstat.ts_status);
607 }
608
609 if (tries2 && finalTSIdx > 1) {
610 update_stats(sc, an, frame_size,
611 ndx2, tries2,
612 ndx3, tries3,
613 0, 0,
614 0, 0,
615 short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1,
616 ds->ds_txstat.ts_status);
617 }
618
619 if (tries3 && finalTSIdx > 2) {
620 update_stats(sc, an, frame_size,
621 ndx3, tries3,
622 0, 0,
623 0, 0,
624 0, 0,
625 short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1 - tries2,
626 ds->ds_txstat.ts_status);
627 }
628 }
629 }
630
631 void
632 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
633 {
634 DPRINTF(sc, "%s: %s isnew %d\n", __func__,
635 ether_sprintf(an->an_node.ni_macaddr), isnew);
636 if (isnew)
637 ath_rate_ctl_reset(sc, &an->an_node);
638 }
639
640 /*
641 * Initialize the tables for a node.
642 */
643 static void
644 ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
645 {
646 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
647 struct ieee80211com *ic = &sc->sc_ic;
648 struct ath_node *an = ATH_NODE(ni);
649 struct sample_node *sn = ATH_NODE_SAMPLE(an);
650 const HAL_RATE_TABLE *rt = sc->sc_currates;
651 int x, y, srate;
652
653 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
654 sn->static_rate_ndx = -1;
655 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
656 /*
657 * A fixed rate is to be used; ic_fixed_rate is an
658 * index into the supported rate set. Convert this
659 * to the index into the negotiated rate set for
660 * the node. We know the rate is there because the
661 * rate set is checked when the station associates.
662 */
663 const struct ieee80211_rateset *rs =
664 &ic->ic_sup_rates[ic->ic_curmode];
665 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
666 /* NB: the rate set is assumed sorted */
667 srate = ni->ni_rates.rs_nrates - 1;
668 for (; srate >= 0 && RATE(srate) != r; srate--)
669 ;
670 KASSERT(srate >= 0,
671 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
672 sn->static_rate_ndx = srate;
673 }
674
675 DPRINTF(sc, "%s: %s size 1600 rate/tt", __func__, ether_sprintf(ni->ni_macaddr));
676
677 sn->num_rates = ni->ni_rates.rs_nrates;
678 for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
679 sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL;
680 sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate];
681 sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode;
682 sn->rates[x].shortPreambleRateCode =
683 rt->info[sn->rates[x].rix].rateCode |
684 rt->info[sn->rates[x].rix].shortPreamble;
685
686 DPRINTF(sc, " %d/%d", sn->rates[x].rate,
687 calc_usecs_unicast_packet(sc, 1600, sn->rates[x].rix,
688 0,0));
689 }
690 DPRINTF(sc, "%s\n", "");
691
692 /* set the visible bit-rate to the lowest one available */
693 ni->ni_txrate = 0;
694 sn->num_rates = ni->ni_rates.rs_nrates;
695
696 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
697 int size = bin_to_size(y);
698 int ndx = 0;
699 sn->packets_sent[y] = 0;
700 sn->current_sample_ndx[y] = -1;
701 sn->last_sample_ndx[y] = 0;
702
703 for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
704 sn->stats[y][x].successive_failures = 0;
705 sn->stats[y][x].tries = 0;
706 sn->stats[y][x].total_packets = 0;
707 sn->stats[y][x].packets_acked = 0;
708 sn->stats[y][x].last_tx = 0;
709
710 sn->stats[y][x].perfect_tx_time =
711 calc_usecs_unicast_packet(sc, size,
712 sn->rates[x].rix,
713 0, 0);
714 sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time;
715 }
716
717 /* set the initial rate */
718 for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
719 if (sn->rates[ndx].rate <= 72) {
720 break;
721 }
722 }
723 sn->current_rate[y] = ndx;
724 }
725
726 DPRINTF(sc, "%s: %s %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n",
727 __func__, ether_sprintf(ni->ni_macaddr),
728 sn->num_rates,
729 sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "",
730 sn->stats[1][0].perfect_tx_time,
731 sn->rates[sn->num_rates-1].rate/2,
732 sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "",
733 sn->stats[1][sn->num_rates-1].perfect_tx_time
734 );
735
736 ni->ni_txrate = sn->current_rate[0];
737 #undef RATE
738 }
739
740 static void
741 rate_cb(void *arg, struct ieee80211_node *ni)
742 {
743 struct ath_softc *sc = arg;
744
745 ath_rate_newassoc(sc, ATH_NODE(ni), 1);
746 }
747
748 /*
749 * Reset the rate control state for each 802.11 state transition.
750 */
751 void
752 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
753 {
754 struct ieee80211com *ic = &sc->sc_ic;
755
756 if (state == IEEE80211_S_RUN) {
757 if (ic->ic_opmode != IEEE80211_M_STA) {
758 /*
759 * Sync rates for associated stations and neighbors.
760 */
761 ieee80211_iterate_nodes(&ic->ic_sta, rate_cb, sc);
762 }
763 ath_rate_newassoc(sc, ATH_NODE(ic->ic_bss), 1);
764 }
765 }
766
767 static void
768 ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc)
769 {
770 int rc;
771 struct sysctllog **log = &sc->sc_sysctllog;
772 const struct sysctlnode *cnode, *rnode;
773
774 if ((rnode = ath_sysctl_instance(sc->sc_dev.dv_xname, log)) == NULL)
775 return;
776
777 /* XXX bounds check [0..100] */
778 if ((rc = SYSCTL_PFX_INT(osc->ath_, CTLFLAG_READWRITE, smoothing_rate,
779 "rate control: retry threshold to credit rate raise (%%)")) != 0)
780 goto err;
781
782 /* XXX bounds check [2..100] */
783 if ((rc = SYSCTL_PFX_INT(osc->ath_, CTLFLAG_READWRITE, sample_rate,
784 "rate control: # good periods before raising rate")) != 0)
785 goto err;
786
787 return;
788 err:
789 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
790 }
791
792 struct ath_ratectrl *
793 ath_rate_attach(struct ath_softc *sc)
794 {
795 struct sample_softc *osc;
796
797 DPRINTF(sc, "%s:\n", __func__);
798 osc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
799 if (osc == NULL)
800 return NULL;
801 osc->arc.arc_space = sizeof(struct sample_node);
802 osc->ath_smoothing_rate = 95; /* ewma percentage (out of 100) */
803 osc->ath_sample_rate = 10; /* send a different bit-rate 1/X packets */
804 ath_rate_sysctlattach(sc, osc);
805 return &osc->arc;
806 }
807
808 void
809 ath_rate_detach(struct ath_ratectrl *arc)
810 {
811 struct sample_softc *osc = (struct sample_softc *) arc;
812
813 free(osc, M_DEVBUF);
814 }
815