sample.c revision 232170
1/*- 2 * Copyright (c) 2005 John Bicket 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 3. Neither the names of the above-listed copyright holders nor the names 16 * of any contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * Alternatively, this software may be distributed under the terms of the 20 * GNU General Public License ("GPL") version 2 as published by the Free 21 * Software Foundation. 22 * 23 * NO WARRANTY 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34 * THE POSSIBILITY OF SUCH DAMAGES. 35 * 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/sample/sample.c 232170 2012-02-26 06:04:44Z adrian $"); 40 41/* 42 * John Bicket's SampleRate control algorithm. 43 */ 44#include "opt_inet.h" 45#include "opt_wlan.h" 46#include "opt_ah.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/sysctl.h> 51#include <sys/kernel.h> 52#include <sys/lock.h> 53#include <sys/mutex.h> 54#include <sys/errno.h> 55 56#include <machine/bus.h> 57#include <machine/resource.h> 58#include <sys/bus.h> 59 60#include <sys/socket.h> 61 62#include <net/if.h> 63#include <net/if_media.h> 64#include <net/if_arp.h> 65#include <net/ethernet.h> /* XXX for ether_sprintf */ 66 67#include <net80211/ieee80211_var.h> 68 69#include <net/bpf.h> 70 71#ifdef INET 72#include <netinet/in.h> 73#include <netinet/if_ether.h> 74#endif 75 76#include <dev/ath/if_athvar.h> 77#include <dev/ath/ath_rate/sample/sample.h> 78#include <dev/ath/ath_hal/ah_desc.h> 79#include <dev/ath/ath_rate/sample/tx_schedules.h> 80 81/* 82 * This file is an implementation of the SampleRate algorithm 83 * in "Bit-rate Selection in Wireless Networks" 84 * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps) 85 * 86 * SampleRate chooses the bit-rate it predicts will provide the most 87 * throughput based on estimates of the expected per-packet 88 * transmission time for each bit-rate. SampleRate periodically sends 89 * packets at bit-rates other than the current one to estimate when 90 * another bit-rate will provide better performance. SampleRate 91 * switches to another bit-rate when its estimated per-packet 92 * transmission time becomes smaller than the current bit-rate's. 93 * SampleRate reduces the number of bit-rates it must sample by 94 * eliminating those that could not perform better than the one 95 * currently being used. SampleRate also stops probing at a bit-rate 96 * if it experiences several successive losses. 97 * 98 * The difference between the algorithm in the thesis and the one in this 99 * file is that the one in this file uses a ewma instead of a window. 100 * 101 * Also, this implementation tracks the average transmission time for 102 * a few different packet sizes independently for each link. 103 */ 104 105static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *); 106 107static const int packet_size_bins[NUM_PACKET_SIZE_BINS] = { 250, 1600 }; 108 109static __inline int 110size_to_bin(int size) 111{ 112#if NUM_PACKET_SIZE_BINS > 1 113 if (size <= packet_size_bins[0]) 114 return 0; 115#endif 116#if NUM_PACKET_SIZE_BINS > 2 117 if (size <= packet_size_bins[1]) 118 return 1; 119#endif 120#if NUM_PACKET_SIZE_BINS > 3 121 if (size <= packet_size_bins[2]) 122 return 2; 123#endif 124#if NUM_PACKET_SIZE_BINS > 4 125#error "add support for more packet sizes" 126#endif 127 return NUM_PACKET_SIZE_BINS-1; 128} 129 130static __inline int 131bin_to_size(int index) 132{ 133 return packet_size_bins[index]; 134} 135 136void 137ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 138{ 139 /* NB: assumed to be zero'd by caller */ 140} 141 142void 143ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 144{ 145} 146 147static int 148dot11rate(const HAL_RATE_TABLE *rt, int rix) 149{ 150 if (rix < 0) 151 return -1; 152 return rt->info[rix].phy == IEEE80211_T_HT ? 153 rt->info[rix].dot11Rate : (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL) / 2; 154} 155 156static const char * 157dot11rate_label(const HAL_RATE_TABLE *rt, int rix) 158{ 159 if (rix < 0) 160 return ""; 161 return rt->info[rix].phy == IEEE80211_T_HT ? "MCS" : "Mb "; 162} 163 164/* 165 * Return the rix with the lowest average_tx_time, 166 * or -1 if all the average_tx_times are 0. 167 */ 168static __inline int 169pick_best_rate(struct ath_node *an, const HAL_RATE_TABLE *rt, 170 int size_bin, int require_acked_before) 171{ 172 struct sample_node *sn = ATH_NODE_SAMPLE(an); 173 int best_rate_rix, best_rate_tt, best_rate_pct; 174 uint32_t mask; 175 int rix, tt, pct; 176 177 best_rate_rix = 0; 178 best_rate_tt = 0; 179 best_rate_pct = 0; 180 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 181 if ((mask & 1) == 0) /* not a supported rate */ 182 continue; 183 184 /* Don't pick a non-HT rate for a HT node */ 185 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) && 186 (rt->info[rix].phy != IEEE80211_T_HT)) { 187 continue; 188 } 189 190 tt = sn->stats[size_bin][rix].average_tx_time; 191 if (tt <= 0 || 192 (require_acked_before && 193 !sn->stats[size_bin][rix].packets_acked)) 194 continue; 195 196 /* Calculate percentage if possible */ 197 if (sn->stats[size_bin][rix].total_packets > 0) { 198 pct = sn->stats[size_bin][rix].ewma_pct; 199 } else { 200 /* XXX for now, assume 95% ok */ 201 pct = 95; 202 } 203 204 /* don't use a bit-rate that has been failing */ 205 if (sn->stats[size_bin][rix].successive_failures > 3) 206 continue; 207 208 /* 209 * For HT, Don't use a bit rate that is much more 210 * lossy than the best. 211 * 212 * XXX this isn't optimal; it's just designed to 213 * eliminate rates that are going to be obviously 214 * worse. 215 */ 216 if (an->an_node.ni_flags & IEEE80211_NODE_HT) { 217 if (best_rate_pct > (pct + 50)) 218 continue; 219 } 220 221 /* 222 * For non-MCS rates, use the current average txtime for 223 * comparison. 224 */ 225 if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) { 226 if (best_rate_tt == 0 || tt <= best_rate_tt) { 227 best_rate_tt = tt; 228 best_rate_rix = rix; 229 best_rate_pct = pct; 230 } 231 } 232 233 /* 234 * Since 2 stream rates have slightly higher TX times, 235 * allow a little bit of leeway. This should later 236 * be abstracted out and properly handled. 237 */ 238 if (an->an_node.ni_flags & IEEE80211_NODE_HT) { 239 if (best_rate_tt == 0 || (tt * 8 <= best_rate_tt * 10)) { 240 best_rate_tt = tt; 241 best_rate_rix = rix; 242 best_rate_pct = pct; 243 } 244 } 245 } 246 return (best_rate_tt ? best_rate_rix : -1); 247} 248 249/* 250 * Pick a good "random" bit-rate to sample other than the current one. 251 */ 252static __inline int 253pick_sample_rate(struct sample_softc *ssc , struct ath_node *an, 254 const HAL_RATE_TABLE *rt, int size_bin) 255{ 256#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 257#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 258 struct sample_node *sn = ATH_NODE_SAMPLE(an); 259 int current_rix, rix; 260 unsigned current_tt; 261 uint32_t mask; 262 263 current_rix = sn->current_rix[size_bin]; 264 if (current_rix < 0) { 265 /* no successes yet, send at the lowest bit-rate */ 266 /* XXX should return MCS0 if HT */ 267 return 0; 268 } 269 270 current_tt = sn->stats[size_bin][current_rix].average_tx_time; 271 272 rix = sn->last_sample_rix[size_bin]+1; /* next sample rate */ 273 mask = sn->ratemask &~ (1<<current_rix);/* don't sample current rate */ 274 while (mask != 0) { 275 if ((mask & (1<<rix)) == 0) { /* not a supported rate */ 276 nextrate: 277 if (++rix >= rt->rateCount) 278 rix = 0; 279 continue; 280 } 281 282 /* if the node is HT and the rate isn't HT, don't bother sample */ 283 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) && 284 (rt->info[rix].phy != IEEE80211_T_HT)) { 285 mask &= ~(1<<rix); 286 goto nextrate; 287 } 288 289 /* this bit-rate is always worse than the current one */ 290 if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) { 291 mask &= ~(1<<rix); 292 goto nextrate; 293 } 294 295 /* rarely sample bit-rates that fail a lot */ 296 if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures && 297 ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) { 298 mask &= ~(1<<rix); 299 goto nextrate; 300 } 301 302 /* 303 * When doing aggregation, successive failures don't happen 304 * as often, as sometimes some of the sub-frames get through. 305 * 306 * If the sample rix average tx time is greater than the 307 * average tx time of the current rix, don't immediately use 308 * the rate for sampling. 309 */ 310 if (an->an_node.ni_flags & IEEE80211_NODE_HT) { 311 if ((sn->stats[size_bin][rix].average_tx_time * 10 > 312 sn->stats[size_bin][current_rix].average_tx_time * 9) && 313 (ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout)) { 314 mask &= ~(1<<rix); 315 goto nextrate; 316 } 317 } 318 319 /* 320 * XXX TODO 321 * For HT, limit sample somehow? 322 */ 323 324 /* Don't sample more than 2 rates higher for rates > 11M for non-HT rates */ 325 if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) { 326 if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) { 327 mask &= ~(1<<rix); 328 goto nextrate; 329 } 330 } 331 332 sn->last_sample_rix[size_bin] = rix; 333 return rix; 334 } 335 return current_rix; 336#undef DOT11RATE 337#undef MCS 338} 339 340static int 341ath_rate_get_static_rix(struct ath_softc *sc, const struct ieee80211_node *ni) 342{ 343#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 344#define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL) 345#define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS) 346 const struct ieee80211_txparam *tp = ni->ni_txparms; 347 int srate; 348 349 /* Check MCS rates */ 350 for (srate = ni->ni_htrates.rs_nrates - 1; srate >= 0; srate--) { 351 if (MCS(srate) == tp->ucastrate) 352 return sc->sc_rixmap[tp->ucastrate]; 353 } 354 355 /* Check legacy rates */ 356 for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--) { 357 if (RATE(srate) == tp->ucastrate) 358 return sc->sc_rixmap[tp->ucastrate]; 359 } 360 return -1; 361#undef RATE 362#undef DOT11RATE 363#undef MCS 364} 365 366static void 367ath_rate_update_static_rix(struct ath_softc *sc, struct ieee80211_node *ni) 368{ 369 struct ath_node *an = ATH_NODE(ni); 370 const struct ieee80211_txparam *tp = ni->ni_txparms; 371 struct sample_node *sn = ATH_NODE_SAMPLE(an); 372 373 if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 374 /* 375 * A fixed rate is to be used; ucastrate is the IEEE code 376 * for this rate (sans basic bit). Check this against the 377 * negotiated rate set for the node. Note the fixed rate 378 * may not be available for various reasons so we only 379 * setup the static rate index if the lookup is successful. 380 */ 381 sn->static_rix = ath_rate_get_static_rix(sc, ni); 382 } else { 383 sn->static_rix = -1; 384 } 385} 386 387/* 388 * Pick a non-HT rate to begin using. 389 */ 390static int 391ath_rate_pick_seed_rate_legacy(struct ath_softc *sc, struct ath_node *an, 392 int frameLen) 393{ 394#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 395#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 396#define RATE(ix) (DOT11RATE(ix) / 2) 397 int rix = -1; 398 const HAL_RATE_TABLE *rt = sc->sc_currates; 399 struct sample_node *sn = ATH_NODE_SAMPLE(an); 400 const int size_bin = size_to_bin(frameLen); 401 402 /* no packet has been sent successfully yet */ 403 for (rix = rt->rateCount-1; rix > 0; rix--) { 404 if ((sn->ratemask & (1<<rix)) == 0) 405 continue; 406 407 /* Skip HT rates */ 408 if (rt->info[rix].phy == IEEE80211_T_HT) 409 continue; 410 411 /* 412 * Pick the highest rate <= 36 Mbps 413 * that hasn't failed. 414 */ 415 if (DOT11RATE(rix) <= 72 && 416 sn->stats[size_bin][rix].successive_failures == 0) { 417 break; 418 } 419 } 420 return rix; 421#undef RATE 422#undef MCS 423#undef DOT11RATE 424} 425 426/* 427 * Pick a HT rate to begin using. 428 * 429 * Don't use any non-HT rates; only consider HT rates. 430 */ 431static int 432ath_rate_pick_seed_rate_ht(struct ath_softc *sc, struct ath_node *an, 433 int frameLen) 434{ 435#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 436#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 437#define RATE(ix) (DOT11RATE(ix) / 2) 438 int rix = -1, ht_rix = -1; 439 const HAL_RATE_TABLE *rt = sc->sc_currates; 440 struct sample_node *sn = ATH_NODE_SAMPLE(an); 441 const int size_bin = size_to_bin(frameLen); 442 443 /* no packet has been sent successfully yet */ 444 for (rix = rt->rateCount-1; rix > 0; rix--) { 445 /* Skip rates we can't use */ 446 if ((sn->ratemask & (1<<rix)) == 0) 447 continue; 448 449 /* Keep a copy of the last seen HT rate index */ 450 if (rt->info[rix].phy == IEEE80211_T_HT) 451 ht_rix = rix; 452 453 /* Skip non-HT rates */ 454 if (rt->info[rix].phy != IEEE80211_T_HT) 455 continue; 456 457 /* 458 * Pick a medium-speed rate regardless of stream count 459 * which has not seen any failures. Higher rates may fail; 460 * we'll try them later. 461 */ 462 if (((MCS(rix) & 0x7) <= 4) && 463 sn->stats[size_bin][rix].successive_failures == 0) { 464 break; 465 } 466 } 467 468 /* 469 * If all the MCS rates have successive failures, rix should be 470 * > 0; otherwise use the lowest MCS rix (hopefully MCS 0.) 471 */ 472 return MAX(rix, ht_rix); 473#undef RATE 474#undef MCS 475#undef DOT11RATE 476} 477 478 479void 480ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 481 int shortPreamble, size_t frameLen, 482 u_int8_t *rix0, int *try0, u_int8_t *txrate) 483{ 484#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 485#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 486#define RATE(ix) (DOT11RATE(ix) / 2) 487 struct sample_node *sn = ATH_NODE_SAMPLE(an); 488 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 489 struct ifnet *ifp = sc->sc_ifp; 490 struct ieee80211com *ic = ifp->if_l2com; 491 const HAL_RATE_TABLE *rt = sc->sc_currates; 492 const int size_bin = size_to_bin(frameLen); 493 int rix, mrr, best_rix, change_rates; 494 unsigned average_tx_time; 495 496 ath_rate_update_static_rix(sc, &an->an_node); 497 498 if (sn->currates != sc->sc_currates) { 499 device_printf(sc->sc_dev, "%s: currates != sc_currates!\n", 500 __func__); 501 rix = 0; 502 *try0 = ATH_TXMAXTRY; 503 goto done; 504 } 505 506 if (sn->static_rix != -1) { 507 rix = sn->static_rix; 508 *try0 = ATH_TXMAXTRY; 509 goto done; 510 } 511 512 /* XXX TODO: this doesn't know about 11gn vs 11g protection; teach it */ 513 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 514 515 best_rix = pick_best_rate(an, rt, size_bin, !mrr); 516 if (best_rix >= 0) { 517 average_tx_time = sn->stats[size_bin][best_rix].average_tx_time; 518 } else { 519 average_tx_time = 0; 520 } 521 /* 522 * Limit the time measuring the performance of other tx 523 * rates to sample_rate% of the total transmission time. 524 */ 525 if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) { 526 rix = pick_sample_rate(ssc, an, rt, size_bin); 527 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 528 &an->an_node, "att %d sample_tt %d size %u sample rate %d %s current rate %d %s", 529 average_tx_time, 530 sn->sample_tt[size_bin], 531 bin_to_size(size_bin), 532 dot11rate(rt, rix), 533 dot11rate_label(rt, rix), 534 dot11rate(rt, sn->current_rix[size_bin]), 535 dot11rate_label(rt, sn->current_rix[size_bin])); 536 if (rix != sn->current_rix[size_bin]) { 537 sn->current_sample_rix[size_bin] = rix; 538 } else { 539 sn->current_sample_rix[size_bin] = -1; 540 } 541 sn->packets_since_sample[size_bin] = 0; 542 } else { 543 change_rates = 0; 544 if (!sn->packets_sent[size_bin] || best_rix == -1) { 545 /* no packet has been sent successfully yet */ 546 change_rates = 1; 547 if (an->an_node.ni_flags & IEEE80211_NODE_HT) 548 best_rix = 549 ath_rate_pick_seed_rate_ht(sc, an, frameLen); 550 else 551 best_rix = 552 ath_rate_pick_seed_rate_legacy(sc, an, frameLen); 553 } else if (sn->packets_sent[size_bin] < 20) { 554 /* let the bit-rate switch quickly during the first few packets */ 555 IEEE80211_NOTE(an->an_node.ni_vap, 556 IEEE80211_MSG_RATECTL, &an->an_node, 557 "%s: switching quickly..", __func__); 558 change_rates = 1; 559 } else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) { 560 /* min_switch seconds have gone by */ 561 IEEE80211_NOTE(an->an_node.ni_vap, 562 IEEE80211_MSG_RATECTL, &an->an_node, 563 "%s: min_switch %d > ticks_since_switch %d..", 564 __func__, ticks - ssc->min_switch, sn->ticks_since_switch[size_bin]); 565 change_rates = 1; 566 } else if ((! (an->an_node.ni_flags & IEEE80211_NODE_HT)) && 567 (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time)) { 568 /* the current bit-rate is twice as slow as the best one */ 569 IEEE80211_NOTE(an->an_node.ni_vap, 570 IEEE80211_MSG_RATECTL, &an->an_node, 571 "%s: 2x att (= %d) < cur_rix att %d", 572 __func__, 573 2 * average_tx_time, sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time); 574 change_rates = 1; 575 } else if ((an->an_node.ni_flags & IEEE80211_NODE_HT)) { 576 int cur_rix = sn->current_rix[size_bin]; 577 int cur_att = sn->stats[size_bin][cur_rix].average_tx_time; 578 /* 579 * If the node is HT, upgrade it if the MCS rate is 580 * higher and the average tx time is within 20% of 581 * the current rate. It can fail a little. 582 * 583 * This is likely not optimal! 584 */ 585#if 0 586 printf("cur rix/att %x/%d, best rix/att %x/%d\n", 587 MCS(cur_rix), cur_att, MCS(best_rix), average_tx_time); 588#endif 589 if ((MCS(best_rix) > MCS(cur_rix)) && 590 (average_tx_time * 8) <= (cur_att * 10)) { 591 IEEE80211_NOTE(an->an_node.ni_vap, 592 IEEE80211_MSG_RATECTL, &an->an_node, 593 "%s: HT: best_rix 0x%d > cur_rix 0x%x, average_tx_time %d, cur_att %d", 594 __func__, 595 MCS(best_rix), MCS(cur_rix), average_tx_time, cur_att); 596 change_rates = 1; 597 } 598 } 599 600 sn->packets_since_sample[size_bin]++; 601 602 if (change_rates) { 603 if (best_rix != sn->current_rix[size_bin]) { 604 IEEE80211_NOTE(an->an_node.ni_vap, 605 IEEE80211_MSG_RATECTL, 606 &an->an_node, 607"%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d", 608 __func__, 609 bin_to_size(size_bin), 610 RATE(sn->current_rix[size_bin]), 611 sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time, 612 sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time, 613 RATE(best_rix), 614 sn->stats[size_bin][best_rix].average_tx_time, 615 sn->stats[size_bin][best_rix].perfect_tx_time, 616 sn->packets_since_switch[size_bin], 617 mrr); 618 } 619 sn->packets_since_switch[size_bin] = 0; 620 sn->current_rix[size_bin] = best_rix; 621 sn->ticks_since_switch[size_bin] = ticks; 622 /* 623 * Set the visible txrate for this node. 624 */ 625 an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ? MCS(best_rix) : DOT11RATE(best_rix); 626 } 627 rix = sn->current_rix[size_bin]; 628 sn->packets_since_switch[size_bin]++; 629 } 630 *try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY; 631done: 632 633 /* 634 * This bug totally sucks and should be fixed. 635 * 636 * For now though, let's not panic, so we can start to figure 637 * out how to better reproduce it. 638 */ 639 if (rix < 0 || rix >= rt->rateCount) { 640 printf("%s: ERROR: rix %d out of bounds (rateCount=%d)\n", 641 __func__, 642 rix, 643 rt->rateCount); 644 rix = 0; /* XXX just default for now */ 645 } 646 KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix)); 647 648 *rix0 = rix; 649 *txrate = rt->info[rix].rateCode 650 | (shortPreamble ? rt->info[rix].shortPreamble : 0); 651 sn->packets_sent[size_bin]++; 652#undef DOT11RATE 653#undef MCS 654#undef RATE 655} 656 657/* 658 * Get the TX rates. Don't fiddle with short preamble flags for them; 659 * the caller can do that. 660 */ 661void 662ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an, 663 uint8_t rix0, struct ath_rc_series *rc) 664{ 665 struct sample_node *sn = ATH_NODE_SAMPLE(an); 666 const struct txschedule *sched = &sn->sched[rix0]; 667 668 KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n", rix0, sched->r0)); 669 670 rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0; 671 672 rc[0].rix = sched->r0; 673 rc[1].rix = sched->r1; 674 rc[2].rix = sched->r2; 675 rc[3].rix = sched->r3; 676 677 rc[0].tries = sched->t0; 678 rc[1].tries = sched->t1; 679 rc[2].tries = sched->t2; 680 rc[3].tries = sched->t3; 681} 682 683void 684ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 685 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 686{ 687 struct sample_node *sn = ATH_NODE_SAMPLE(an); 688 const struct txschedule *sched = &sn->sched[rix]; 689 const HAL_RATE_TABLE *rt = sc->sc_currates; 690 uint8_t rix1, s1code, rix2, s2code, rix3, s3code; 691 692 /* XXX precalculate short preamble tables */ 693 rix1 = sched->r1; 694 s1code = rt->info[rix1].rateCode 695 | (shortPreamble ? rt->info[rix1].shortPreamble : 0); 696 rix2 = sched->r2; 697 s2code = rt->info[rix2].rateCode 698 | (shortPreamble ? rt->info[rix2].shortPreamble : 0); 699 rix3 = sched->r3; 700 s3code = rt->info[rix3].rateCode 701 | (shortPreamble ? rt->info[rix3].shortPreamble : 0); 702 ath_hal_setupxtxdesc(sc->sc_ah, ds, 703 s1code, sched->t1, /* series 1 */ 704 s2code, sched->t2, /* series 2 */ 705 s3code, sched->t3); /* series 3 */ 706} 707 708/* 709 * Update the EWMA percentage. 710 * 711 * This is a simple hack to track an EWMA based on the current 712 * rate scenario. For the rate codes which failed, this will 713 * record a 0% against it. For the rate code which succeeded, 714 * EWMA will record the nbad*100/nframes percentage against it. 715 */ 716static void 717update_ewma_stats(struct ath_softc *sc, struct ath_node *an, 718 int frame_size, 719 int rix0, int tries0, 720 int rix1, int tries1, 721 int rix2, int tries2, 722 int rix3, int tries3, 723 int short_tries, int tries, int status, 724 int nframes, int nbad) 725{ 726 struct sample_node *sn = ATH_NODE_SAMPLE(an); 727 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 728 const int size_bin = size_to_bin(frame_size); 729 int tries_so_far; 730 int pct; 731 int rix = rix0; 732 733 /* Calculate percentage based on current rate */ 734 if (nframes == 0) 735 nframes = nbad = 1; 736 pct = ((nframes - nbad) * 1000) / nframes; 737 738 /* Figure out which rate index succeeded */ 739 tries_so_far = tries0; 740 741 if (tries1 && tries_so_far < tries) { 742 tries_so_far += tries1; 743 rix = rix1; 744 /* XXX bump ewma pct */ 745 } 746 747 if (tries2 && tries_so_far < tries) { 748 tries_so_far += tries2; 749 rix = rix2; 750 /* XXX bump ewma pct */ 751 } 752 753 if (tries3 && tries_so_far < tries) { 754 rix = rix3; 755 /* XXX bump ewma pct */ 756 } 757 758 /* rix is the successful rate, update EWMA for final rix */ 759 if (sn->stats[size_bin][rix].total_packets < 760 ssc->smoothing_minpackets) { 761 /* just average the first few packets */ 762 int a_pct = (sn->stats[size_bin][rix].packets_acked * 1000) / 763 (sn->stats[size_bin][rix].total_packets); 764 sn->stats[size_bin][rix].ewma_pct = a_pct; 765 } else { 766 /* use a ewma */ 767 sn->stats[size_bin][rix].ewma_pct = 768 ((sn->stats[size_bin][rix].ewma_pct * ssc->smoothing_rate) + 769 (pct * (100 - ssc->smoothing_rate))) / 100; 770 } 771} 772 773static void 774update_stats(struct ath_softc *sc, struct ath_node *an, 775 int frame_size, 776 int rix0, int tries0, 777 int rix1, int tries1, 778 int rix2, int tries2, 779 int rix3, int tries3, 780 int short_tries, int tries, int status, 781 int nframes, int nbad) 782{ 783 struct sample_node *sn = ATH_NODE_SAMPLE(an); 784 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 785#ifdef IEEE80211_DEBUG 786 const HAL_RATE_TABLE *rt = sc->sc_currates; 787#endif 788 const int size_bin = size_to_bin(frame_size); 789 const int size = bin_to_size(size_bin); 790 int tt, tries_so_far; 791 int is_ht40 = (an->an_node.ni_chw == 40); 792 793 if (!IS_RATE_DEFINED(sn, rix0)) 794 return; 795 tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries, 796 MIN(tries0, tries) - 1, is_ht40); 797 tries_so_far = tries0; 798 799 if (tries1 && tries_so_far < tries) { 800 if (!IS_RATE_DEFINED(sn, rix1)) 801 return; 802 tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries, 803 MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 804 tries_so_far += tries1; 805 } 806 807 if (tries2 && tries_so_far < tries) { 808 if (!IS_RATE_DEFINED(sn, rix2)) 809 return; 810 tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries, 811 MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 812 tries_so_far += tries2; 813 } 814 815 if (tries3 && tries_so_far < tries) { 816 if (!IS_RATE_DEFINED(sn, rix3)) 817 return; 818 tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries, 819 MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 820 } 821 822 if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) { 823 /* just average the first few packets */ 824 int avg_tx = sn->stats[size_bin][rix0].average_tx_time; 825 int packets = sn->stats[size_bin][rix0].total_packets; 826 sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+nframes); 827 } else { 828 /* use a ewma */ 829 sn->stats[size_bin][rix0].average_tx_time = 830 ((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) + 831 (tt * (100 - ssc->smoothing_rate))) / 100; 832 } 833 834 /* 835 * XXX Don't mark the higher bit rates as also having failed; as this 836 * unfortunately stops those rates from being tasted when trying to 837 * TX. This happens with 11n aggregation. 838 */ 839 if (nframes == nbad) { 840#if 0 841 int y; 842#endif 843 sn->stats[size_bin][rix0].successive_failures += nbad; 844#if 0 845 for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) { 846 /* 847 * Also say larger packets failed since we 848 * assume if a small packet fails at a 849 * bit-rate then a larger one will also. 850 */ 851 sn->stats[y][rix0].successive_failures += nbad; 852 sn->stats[y][rix0].last_tx = ticks; 853 sn->stats[y][rix0].tries += tries; 854 sn->stats[y][rix0].total_packets += nframes; 855 } 856#endif 857 } else { 858 sn->stats[size_bin][rix0].packets_acked += (nframes - nbad); 859 sn->stats[size_bin][rix0].successive_failures = 0; 860 } 861 sn->stats[size_bin][rix0].tries += tries; 862 sn->stats[size_bin][rix0].last_tx = ticks; 863 sn->stats[size_bin][rix0].total_packets += nframes; 864 865 if (rix0 == sn->current_sample_rix[size_bin]) { 866 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 867 &an->an_node, 868"%s: size %d %s sample rate %d %s tries (%d/%d) tt %d avg_tt (%d/%d) nfrm %d nbad %d", 869 __func__, 870 size, 871 status ? "FAIL" : "OK", 872 dot11rate(rt, rix0), 873 dot11rate_label(rt, rix0), 874 short_tries, tries, tt, 875 sn->stats[size_bin][rix0].average_tx_time, 876 sn->stats[size_bin][rix0].perfect_tx_time, 877 nframes, nbad); 878 sn->sample_tt[size_bin] = tt; 879 sn->current_sample_rix[size_bin] = -1; 880 } 881} 882 883static void 884badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status) 885{ 886 if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n", 887 series, hwrate, tries, status); 888} 889 890void 891ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 892 const struct ath_rc_series *rc, const struct ath_tx_status *ts, 893 int frame_size, int nframes, int nbad) 894{ 895 struct ifnet *ifp = sc->sc_ifp; 896 struct ieee80211com *ic = ifp->if_l2com; 897 struct sample_node *sn = ATH_NODE_SAMPLE(an); 898 int final_rix, short_tries, long_tries; 899 const HAL_RATE_TABLE *rt = sc->sc_currates; 900 int status = ts->ts_status; 901 int mrr; 902 903 final_rix = rt->rateCodeToIndex[ts->ts_rate]; 904 short_tries = ts->ts_shortretry; 905 long_tries = ts->ts_longretry + 1; 906 907 if (frame_size == 0) /* NB: should not happen */ 908 frame_size = 1500; 909 910 if (sn->ratemask == 0) { 911 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 912 &an->an_node, 913 "%s: size %d %s rate/try %d/%d no rates yet", 914 __func__, 915 bin_to_size(size_to_bin(frame_size)), 916 status ? "FAIL" : "OK", 917 short_tries, long_tries); 918 return; 919 } 920 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 921 if (!mrr || ts->ts_finaltsi == 0) { 922 if (!IS_RATE_DEFINED(sn, final_rix)) { 923 badrate(ifp, 0, ts->ts_rate, long_tries, status); 924 return; 925 } 926 /* 927 * Only one rate was used; optimize work. 928 */ 929 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 930 &an->an_node, "%s: size %d (%d bytes) %s rate/try %d %s/%d/%d nframes/nbad [%d/%d]", 931 __func__, 932 bin_to_size(size_to_bin(frame_size)), 933 frame_size, 934 status ? "FAIL" : "OK", 935 dot11rate(rt, final_rix), dot11rate_label(rt, final_rix), 936 short_tries, long_tries, nframes, nbad); 937 update_stats(sc, an, frame_size, 938 final_rix, long_tries, 939 0, 0, 940 0, 0, 941 0, 0, 942 short_tries, long_tries, status, 943 nframes, nbad); 944 update_ewma_stats(sc, an, frame_size, 945 final_rix, long_tries, 946 0, 0, 947 0, 0, 948 0, 0, 949 short_tries, long_tries, status, 950 nframes, nbad); 951 952 } else { 953 int finalTSIdx = ts->ts_finaltsi; 954 int i; 955 956 /* 957 * Process intermediate rates that failed. 958 */ 959 960 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 961 &an->an_node, 962"%s: size %d (%d bytes) finaltsidx %d tries %d %s rate/try [%d %s/%d %d %s/%d %d %s/%d %d %s/%d] nframes/nbad [%d/%d]", 963 __func__, 964 bin_to_size(size_to_bin(frame_size)), 965 frame_size, 966 finalTSIdx, 967 long_tries, 968 status ? "FAIL" : "OK", 969 dot11rate(rt, rc[0].rix), 970 dot11rate_label(rt, rc[0].rix), rc[0].tries, 971 dot11rate(rt, rc[1].rix), 972 dot11rate_label(rt, rc[1].rix), rc[1].tries, 973 dot11rate(rt, rc[2].rix), 974 dot11rate_label(rt, rc[2].rix), rc[2].tries, 975 dot11rate(rt, rc[3].rix), 976 dot11rate_label(rt, rc[3].rix), rc[3].tries, 977 nframes, nbad); 978 979 for (i = 0; i < 4; i++) { 980 if (rc[i].tries && !IS_RATE_DEFINED(sn, rc[i].rix)) 981 badrate(ifp, 0, rc[i].ratecode, rc[i].tries, 982 status); 983 } 984 985 /* 986 * NB: series > 0 are not penalized for failure 987 * based on the try counts under the assumption 988 * that losses are often bursty and since we 989 * sample higher rates 1 try at a time doing so 990 * may unfairly penalize them. 991 */ 992 if (rc[0].tries) { 993 update_stats(sc, an, frame_size, 994 rc[0].rix, rc[0].tries, 995 rc[1].rix, rc[1].tries, 996 rc[2].rix, rc[2].tries, 997 rc[3].rix, rc[3].tries, 998 short_tries, long_tries, 999 long_tries > rc[0].tries, 1000 nframes, nbad); 1001 long_tries -= rc[0].tries; 1002 } 1003 1004 if (rc[1].tries && finalTSIdx > 0) { 1005 update_stats(sc, an, frame_size, 1006 rc[1].rix, rc[1].tries, 1007 rc[2].rix, rc[2].tries, 1008 rc[3].rix, rc[3].tries, 1009 0, 0, 1010 short_tries, long_tries, 1011 status, 1012 nframes, nbad); 1013 long_tries -= rc[1].tries; 1014 } 1015 1016 if (rc[2].tries && finalTSIdx > 1) { 1017 update_stats(sc, an, frame_size, 1018 rc[2].rix, rc[2].tries, 1019 rc[3].rix, rc[3].tries, 1020 0, 0, 1021 0, 0, 1022 short_tries, long_tries, 1023 status, 1024 nframes, nbad); 1025 long_tries -= rc[2].tries; 1026 } 1027 1028 if (rc[3].tries && finalTSIdx > 2) { 1029 update_stats(sc, an, frame_size, 1030 rc[3].rix, rc[3].tries, 1031 0, 0, 1032 0, 0, 1033 0, 0, 1034 short_tries, long_tries, 1035 status, 1036 nframes, nbad); 1037 } 1038 1039 update_ewma_stats(sc, an, frame_size, 1040 rc[0].rix, rc[0].tries, 1041 rc[1].rix, rc[1].tries, 1042 rc[2].rix, rc[2].tries, 1043 rc[3].rix, rc[3].tries, 1044 short_tries, long_tries, 1045 long_tries > rc[0].tries, 1046 nframes, nbad); 1047 1048 } 1049} 1050 1051void 1052ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 1053{ 1054 if (isnew) 1055 ath_rate_ctl_reset(sc, &an->an_node); 1056} 1057 1058static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = { 1059 NULL, /* IEEE80211_MODE_AUTO */ 1060 series_11a, /* IEEE80211_MODE_11A */ 1061 series_11g, /* IEEE80211_MODE_11B */ 1062 series_11g, /* IEEE80211_MODE_11G */ 1063 NULL, /* IEEE80211_MODE_FH */ 1064 series_11a, /* IEEE80211_MODE_TURBO_A */ 1065 series_11g, /* IEEE80211_MODE_TURBO_G */ 1066 series_11a, /* IEEE80211_MODE_STURBO_A */ 1067 series_11na, /* IEEE80211_MODE_11NA */ 1068 series_11ng, /* IEEE80211_MODE_11NG */ 1069 series_half, /* IEEE80211_MODE_HALF */ 1070 series_quarter, /* IEEE80211_MODE_QUARTER */ 1071}; 1072 1073/* 1074 * Initialize the tables for a node. 1075 */ 1076static void 1077ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni) 1078{ 1079#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 1080#define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL) 1081#define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS) 1082 struct ath_node *an = ATH_NODE(ni); 1083 struct sample_node *sn = ATH_NODE_SAMPLE(an); 1084 const HAL_RATE_TABLE *rt = sc->sc_currates; 1085 int x, y, rix; 1086 1087 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 1088 1089 KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2, 1090 ("curmode %u", sc->sc_curmode)); 1091 sn->sched = mrr_schedules[sc->sc_curmode]; 1092 KASSERT(sn->sched != NULL, 1093 ("no mrr schedule for mode %u", sc->sc_curmode)); 1094 1095 sn->static_rix = -1; 1096 ath_rate_update_static_rix(sc, ni); 1097 1098 sn->currates = sc->sc_currates; 1099 1100 /* 1101 * Construct a bitmask of usable rates. This has all 1102 * negotiated rates minus those marked by the hal as 1103 * to be ignored for doing rate control. 1104 */ 1105 sn->ratemask = 0; 1106 /* MCS rates */ 1107 if (ni->ni_flags & IEEE80211_NODE_HT) { 1108 for (x = 0; x < ni->ni_htrates.rs_nrates; x++) { 1109 rix = sc->sc_rixmap[MCS(x)]; 1110 if (rix == 0xff) 1111 continue; 1112 /* skip rates marked broken by hal */ 1113 if (!rt->info[rix].valid) 1114 continue; 1115 KASSERT(rix < SAMPLE_MAXRATES, 1116 ("mcs %u has rix %d", MCS(x), rix)); 1117 sn->ratemask |= 1<<rix; 1118 } 1119 } 1120 1121 /* Legacy rates */ 1122 for (x = 0; x < ni->ni_rates.rs_nrates; x++) { 1123 rix = sc->sc_rixmap[RATE(x)]; 1124 if (rix == 0xff) 1125 continue; 1126 /* skip rates marked broken by hal */ 1127 if (!rt->info[rix].valid) 1128 continue; 1129 KASSERT(rix < SAMPLE_MAXRATES, 1130 ("rate %u has rix %d", RATE(x), rix)); 1131 sn->ratemask |= 1<<rix; 1132 } 1133#ifdef IEEE80211_DEBUG 1134 if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) { 1135 uint32_t mask; 1136 1137 ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt", 1138 ni->ni_macaddr, ":", __func__); 1139 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 1140 if ((mask & 1) == 0) 1141 continue; 1142 printf(" %d %s/%d", dot11rate(rt, rix), dot11rate_label(rt, rix), 1143 calc_usecs_unicast_packet(sc, 1600, rix, 0,0, 1144 (ni->ni_chw == 40))); 1145 } 1146 printf("\n"); 1147 } 1148#endif 1149 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 1150 int size = bin_to_size(y); 1151 uint32_t mask; 1152 1153 sn->packets_sent[y] = 0; 1154 sn->current_sample_rix[y] = -1; 1155 sn->last_sample_rix[y] = 0; 1156 /* XXX start with first valid rate */ 1157 sn->current_rix[y] = ffs(sn->ratemask)-1; 1158 1159 /* 1160 * Initialize the statistics buckets; these are 1161 * indexed by the rate code index. 1162 */ 1163 for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) { 1164 if ((mask & 1) == 0) /* not a valid rate */ 1165 continue; 1166 sn->stats[y][rix].successive_failures = 0; 1167 sn->stats[y][rix].tries = 0; 1168 sn->stats[y][rix].total_packets = 0; 1169 sn->stats[y][rix].packets_acked = 0; 1170 sn->stats[y][rix].last_tx = 0; 1171 sn->stats[y][rix].ewma_pct = 0; 1172 1173 sn->stats[y][rix].perfect_tx_time = 1174 calc_usecs_unicast_packet(sc, size, rix, 0, 0, 1175 (ni->ni_chw == 40)); 1176 sn->stats[y][rix].average_tx_time = 1177 sn->stats[y][rix].perfect_tx_time; 1178 } 1179 } 1180#if 0 1181 /* XXX 0, num_rates-1 are wrong */ 1182 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 1183 "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__, 1184 sn->num_rates, 1185 DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "", 1186 sn->stats[1][0].perfect_tx_time, 1187 DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "", 1188 sn->stats[1][sn->num_rates-1].perfect_tx_time 1189 ); 1190#endif 1191 /* set the visible bit-rate */ 1192 if (sn->static_rix != -1) 1193 ni->ni_txrate = DOT11RATE(sn->static_rix); 1194 else 1195 ni->ni_txrate = RATE(0); 1196#undef RATE 1197#undef DOT11RATE 1198} 1199 1200static void 1201sample_stats(void *arg, struct ieee80211_node *ni) 1202{ 1203 struct ath_softc *sc = arg; 1204 const HAL_RATE_TABLE *rt = sc->sc_currates; 1205 struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni)); 1206 uint32_t mask; 1207 int rix, y; 1208 1209 printf("\n[%s] refcnt %d static_rix (%d %s) ratemask 0x%x\n", 1210 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni), 1211 dot11rate(rt, sn->static_rix), 1212 dot11rate_label(rt, sn->static_rix), 1213 sn->ratemask); 1214 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 1215 printf("[%4u] cur rix %d (%d %s) since switch: packets %d ticks %u\n", 1216 bin_to_size(y), sn->current_rix[y], 1217 dot11rate(rt, sn->current_rix[y]), 1218 dot11rate_label(rt, sn->current_rix[y]), 1219 sn->packets_since_switch[y], sn->ticks_since_switch[y]); 1220 printf("[%4u] last sample (%d %s) cur sample (%d %s) packets sent %d\n", 1221 bin_to_size(y), 1222 dot11rate(rt, sn->last_sample_rix[y]), 1223 dot11rate_label(rt, sn->last_sample_rix[y]), 1224 dot11rate(rt, sn->current_sample_rix[y]), 1225 dot11rate_label(rt, sn->current_sample_rix[y]), 1226 sn->packets_sent[y]); 1227 printf("[%4u] packets since sample %d sample tt %u\n", 1228 bin_to_size(y), sn->packets_since_sample[y], 1229 sn->sample_tt[y]); 1230 } 1231 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 1232 if ((mask & 1) == 0) 1233 continue; 1234 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 1235 if (sn->stats[y][rix].total_packets == 0) 1236 continue; 1237 printf("[%2u %s:%4u] %8ju:%-8ju (%3d%%) (EWMA %3d.%1d%%) T %8ju F %4d avg %5u last %u\n", 1238 dot11rate(rt, rix), dot11rate_label(rt, rix), 1239 bin_to_size(y), 1240 (uintmax_t) sn->stats[y][rix].total_packets, 1241 (uintmax_t) sn->stats[y][rix].packets_acked, 1242 (int) ((sn->stats[y][rix].packets_acked * 100ULL) / 1243 sn->stats[y][rix].total_packets), 1244 sn->stats[y][rix].ewma_pct / 10, 1245 sn->stats[y][rix].ewma_pct % 10, 1246 (uintmax_t) sn->stats[y][rix].tries, 1247 sn->stats[y][rix].successive_failures, 1248 sn->stats[y][rix].average_tx_time, 1249 ticks - sn->stats[y][rix].last_tx); 1250 } 1251 } 1252} 1253 1254static int 1255ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS) 1256{ 1257 struct ath_softc *sc = arg1; 1258 struct ifnet *ifp = sc->sc_ifp; 1259 struct ieee80211com *ic = ifp->if_l2com; 1260 int error, v; 1261 1262 v = 0; 1263 error = sysctl_handle_int(oidp, &v, 0, req); 1264 if (error || !req->newptr) 1265 return error; 1266 ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc); 1267 return 0; 1268} 1269 1270static int 1271ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS) 1272{ 1273 struct sample_softc *ssc = arg1; 1274 int rate, error; 1275 1276 rate = ssc->smoothing_rate; 1277 error = sysctl_handle_int(oidp, &rate, 0, req); 1278 if (error || !req->newptr) 1279 return error; 1280 if (!(0 <= rate && rate < 100)) 1281 return EINVAL; 1282 ssc->smoothing_rate = rate; 1283 ssc->smoothing_minpackets = 100 / (100 - rate); 1284 return 0; 1285} 1286 1287static int 1288ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS) 1289{ 1290 struct sample_softc *ssc = arg1; 1291 int rate, error; 1292 1293 rate = ssc->sample_rate; 1294 error = sysctl_handle_int(oidp, &rate, 0, req); 1295 if (error || !req->newptr) 1296 return error; 1297 if (!(2 <= rate && rate <= 100)) 1298 return EINVAL; 1299 ssc->sample_rate = rate; 1300 return 0; 1301} 1302 1303static void 1304ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc) 1305{ 1306 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 1307 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 1308 1309 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 1310 "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0, 1311 ath_rate_sysctl_smoothing_rate, "I", 1312 "sample: smoothing rate for avg tx time (%%)"); 1313 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 1314 "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0, 1315 ath_rate_sysctl_sample_rate, "I", 1316 "sample: percent air time devoted to sampling new rates (%%)"); 1317 /* XXX max_successive_failures, stale_failure_timeout, min_switch */ 1318 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 1319 "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 1320 ath_rate_sysctl_stats, "I", "sample: print statistics"); 1321} 1322 1323struct ath_ratectrl * 1324ath_rate_attach(struct ath_softc *sc) 1325{ 1326 struct sample_softc *ssc; 1327 1328 ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 1329 if (ssc == NULL) 1330 return NULL; 1331 ssc->arc.arc_space = sizeof(struct sample_node); 1332 ssc->smoothing_rate = 95; /* ewma percentage ([0..99]) */ 1333 ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate); 1334 ssc->sample_rate = 10; /* %time to try diff tx rates */ 1335 ssc->max_successive_failures = 3; /* threshold for rate sampling*/ 1336 ssc->stale_failure_timeout = 10 * hz; /* 10 seconds */ 1337 ssc->min_switch = hz; /* 1 second */ 1338 ath_rate_sysctlattach(sc, ssc); 1339 return &ssc->arc; 1340} 1341 1342void 1343ath_rate_detach(struct ath_ratectrl *arc) 1344{ 1345 struct sample_softc *ssc = (struct sample_softc *) arc; 1346 1347 free(ssc, M_DEVBUF); 1348} 1349