sample.c revision 227340
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 227340 2011-11-08 14:46:03Z 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; 174 uint32_t mask; 175 int rix, tt; 176 177 best_rate_rix = 0; 178 best_rate_tt = 0; 179 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 180 if ((mask & 1) == 0) /* not a supported rate */ 181 continue; 182 183 /* Don't pick a non-HT rate for a HT node */ 184 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) && 185 (rt->info[rix].phy != IEEE80211_T_HT)) { 186 continue; 187 } 188 189 tt = sn->stats[size_bin][rix].average_tx_time; 190 if (tt <= 0 || 191 (require_acked_before && 192 !sn->stats[size_bin][rix].packets_acked)) 193 continue; 194 195 /* don't use a bit-rate that has been failing */ 196 if (sn->stats[size_bin][rix].successive_failures > 3) 197 continue; 198 199 if (best_rate_tt == 0 || tt < best_rate_tt) { 200 best_rate_tt = tt; 201 best_rate_rix = rix; 202 } 203 } 204 return (best_rate_tt ? best_rate_rix : -1); 205} 206 207/* 208 * Pick a good "random" bit-rate to sample other than the current one. 209 */ 210static __inline int 211pick_sample_rate(struct sample_softc *ssc , struct ath_node *an, 212 const HAL_RATE_TABLE *rt, int size_bin) 213{ 214#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 215#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 216 struct sample_node *sn = ATH_NODE_SAMPLE(an); 217 int current_rix, rix; 218 unsigned current_tt; 219 uint32_t mask; 220 221 current_rix = sn->current_rix[size_bin]; 222 if (current_rix < 0) { 223 /* no successes yet, send at the lowest bit-rate */ 224 /* XXX should return MCS0 if HT */ 225 return 0; 226 } 227 228 current_tt = sn->stats[size_bin][current_rix].average_tx_time; 229 230 rix = sn->last_sample_rix[size_bin]+1; /* next sample rate */ 231 mask = sn->ratemask &~ (1<<current_rix);/* don't sample current rate */ 232 while (mask != 0) { 233 if ((mask & (1<<rix)) == 0) { /* not a supported rate */ 234 nextrate: 235 if (++rix >= rt->rateCount) 236 rix = 0; 237 continue; 238 } 239 240 /* if the node is HT and the rate isn't HT, don't bother sample */ 241 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) && 242 (rt->info[rix].phy != IEEE80211_T_HT)) { 243 mask &= ~(1<<rix); 244 goto nextrate; 245 } 246 247 /* this bit-rate is always worse than the current one */ 248 if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) { 249 mask &= ~(1<<rix); 250 goto nextrate; 251 } 252 253 /* rarely sample bit-rates that fail a lot */ 254 if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures && 255 ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) { 256 mask &= ~(1<<rix); 257 goto nextrate; 258 } 259 260 /* Don't sample more than 2 rates higher for rates > 11M for non-HT rates */ 261 if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) { 262 if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) { 263 mask &= ~(1<<rix); 264 goto nextrate; 265 } 266 } 267 268 sn->last_sample_rix[size_bin] = rix; 269 return rix; 270 } 271 return current_rix; 272#undef DOT11RATE 273#undef MCS 274} 275 276static int 277ath_rate_get_static_rix(struct ath_softc *sc, const struct ieee80211_node *ni) 278{ 279#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 280#define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL) 281#define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS) 282 const struct ieee80211_txparam *tp = ni->ni_txparms; 283 int srate; 284 285 /* Check MCS rates */ 286 for (srate = ni->ni_htrates.rs_nrates - 1; srate >= 0; srate--) { 287 if (MCS(srate) == tp->ucastrate) 288 return sc->sc_rixmap[tp->ucastrate]; 289 } 290 291 /* Check legacy rates */ 292 for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--) { 293 if (RATE(srate) == tp->ucastrate) 294 return sc->sc_rixmap[tp->ucastrate]; 295 } 296 return -1; 297#undef RATE 298#undef DOT11RATE 299#undef MCS 300} 301 302static void 303ath_rate_update_static_rix(struct ath_softc *sc, struct ieee80211_node *ni) 304{ 305 struct ath_node *an = ATH_NODE(ni); 306 const struct ieee80211_txparam *tp = ni->ni_txparms; 307 struct sample_node *sn = ATH_NODE_SAMPLE(an); 308 309 if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 310 /* 311 * A fixed rate is to be used; ucastrate is the IEEE code 312 * for this rate (sans basic bit). Check this against the 313 * negotiated rate set for the node. Note the fixed rate 314 * may not be available for various reasons so we only 315 * setup the static rate index if the lookup is successful. 316 */ 317 sn->static_rix = ath_rate_get_static_rix(sc, ni); 318 } else { 319 sn->static_rix = -1; 320 } 321} 322 323 324 325void 326ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 327 int shortPreamble, size_t frameLen, 328 u_int8_t *rix0, int *try0, u_int8_t *txrate) 329{ 330#define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL) 331#define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS) 332#define RATE(ix) (DOT11RATE(ix) / 2) 333 struct sample_node *sn = ATH_NODE_SAMPLE(an); 334 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 335 struct ifnet *ifp = sc->sc_ifp; 336 struct ieee80211com *ic = ifp->if_l2com; 337 const HAL_RATE_TABLE *rt = sc->sc_currates; 338 const int size_bin = size_to_bin(frameLen); 339 int rix, mrr, best_rix, change_rates; 340 unsigned average_tx_time; 341 342 ath_rate_update_static_rix(sc, &an->an_node); 343 344 if (sn->static_rix != -1) { 345 rix = sn->static_rix; 346 *try0 = ATH_TXMAXTRY; 347 goto done; 348 } 349 350 /* XXX TODO: this doesn't know about 11gn vs 11g protection; teach it */ 351 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 352 353 best_rix = pick_best_rate(an, rt, size_bin, !mrr); 354 if (best_rix >= 0) { 355 average_tx_time = sn->stats[size_bin][best_rix].average_tx_time; 356 } else { 357 average_tx_time = 0; 358 } 359 /* 360 * Limit the time measuring the performance of other tx 361 * rates to sample_rate% of the total transmission time. 362 */ 363 if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) { 364 rix = pick_sample_rate(ssc, an, rt, size_bin); 365 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 366 &an->an_node, "size %u sample rate %d current rate %d", 367 bin_to_size(size_bin), RATE(rix), 368 RATE(sn->current_rix[size_bin])); 369 if (rix != sn->current_rix[size_bin]) { 370 sn->current_sample_rix[size_bin] = rix; 371 } else { 372 sn->current_sample_rix[size_bin] = -1; 373 } 374 sn->packets_since_sample[size_bin] = 0; 375 } else { 376 change_rates = 0; 377 if (!sn->packets_sent[size_bin] || best_rix == -1) { 378 /* no packet has been sent successfully yet */ 379 for (rix = rt->rateCount-1; rix > 0; rix--) { 380 if ((sn->ratemask & (1<<rix)) == 0) 381 continue; 382 /* 383 * Pick the highest rate <= 36 Mbps 384 * that hasn't failed. 385 */ 386 if (DOT11RATE(rix) <= 72 && 387 sn->stats[size_bin][rix].successive_failures == 0) { 388 break; 389 } 390 } 391 change_rates = 1; 392 best_rix = rix; 393 } else if (sn->packets_sent[size_bin] < 20) { 394 /* let the bit-rate switch quickly during the first few packets */ 395 change_rates = 1; 396 } else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) { 397 /* min_switch seconds have gone by */ 398 change_rates = 1; 399 } else if (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time) { 400 /* the current bit-rate is twice as slow as the best one */ 401 change_rates = 1; 402 } 403 404 sn->packets_since_sample[size_bin]++; 405 406 if (change_rates) { 407 if (best_rix != sn->current_rix[size_bin]) { 408 IEEE80211_NOTE(an->an_node.ni_vap, 409 IEEE80211_MSG_RATECTL, 410 &an->an_node, 411"%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d", 412 __func__, 413 bin_to_size(size_bin), 414 RATE(sn->current_rix[size_bin]), 415 sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time, 416 sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time, 417 RATE(best_rix), 418 sn->stats[size_bin][best_rix].average_tx_time, 419 sn->stats[size_bin][best_rix].perfect_tx_time, 420 sn->packets_since_switch[size_bin], 421 mrr); 422 } 423 sn->packets_since_switch[size_bin] = 0; 424 sn->current_rix[size_bin] = best_rix; 425 sn->ticks_since_switch[size_bin] = ticks; 426 /* 427 * Set the visible txrate for this node. 428 */ 429 an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ? MCS(best_rix) : DOT11RATE(best_rix); 430 } 431 rix = sn->current_rix[size_bin]; 432 sn->packets_since_switch[size_bin]++; 433 } 434 *try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY; 435done: 436 KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix)); 437 438 *rix0 = rix; 439 *txrate = rt->info[rix].rateCode 440 | (shortPreamble ? rt->info[rix].shortPreamble : 0); 441 sn->packets_sent[size_bin]++; 442#undef DOT11RATE 443#undef MCS 444#undef RATE 445} 446 447/* 448 * Get the TX rates. Don't fiddle with short preamble flags for them; 449 * the caller can do that. 450 */ 451void 452ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an, 453 uint8_t rix0, uint8_t *rix, uint8_t *try) 454{ 455 struct sample_node *sn = ATH_NODE_SAMPLE(an); 456 const struct txschedule *sched = &sn->sched[rix0]; 457 458 KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n", rix0, sched->r0)); 459 460/* rix[0] = sched->r0; */ 461 rix[1] = sched->r1; 462 rix[2] = sched->r2; 463 rix[3] = sched->r3; 464 465 try[0] = sched->t0; 466 try[1] = sched->t1; 467 try[2] = sched->t2; 468 try[3] = sched->t3; 469} 470 471void 472ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 473 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 474{ 475 struct sample_node *sn = ATH_NODE_SAMPLE(an); 476 const struct txschedule *sched = &sn->sched[rix]; 477 const HAL_RATE_TABLE *rt = sc->sc_currates; 478 uint8_t rix1, s1code, rix2, s2code, rix3, s3code; 479 480 /* XXX precalculate short preamble tables */ 481 rix1 = sched->r1; 482 s1code = rt->info[rix1].rateCode 483 | (shortPreamble ? rt->info[rix1].shortPreamble : 0); 484 rix2 = sched->r2; 485 s2code = rt->info[rix2].rateCode 486 | (shortPreamble ? rt->info[rix2].shortPreamble : 0); 487 rix3 = sched->r3; 488 s3code = rt->info[rix3].rateCode 489 | (shortPreamble ? rt->info[rix3].shortPreamble : 0); 490 ath_hal_setupxtxdesc(sc->sc_ah, ds, 491 s1code, sched->t1, /* series 1 */ 492 s2code, sched->t2, /* series 2 */ 493 s3code, sched->t3); /* series 3 */ 494} 495 496static void 497update_stats(struct ath_softc *sc, struct ath_node *an, 498 int frame_size, 499 int rix0, int tries0, 500 int rix1, int tries1, 501 int rix2, int tries2, 502 int rix3, int tries3, 503 int short_tries, int tries, int status) 504{ 505 struct sample_node *sn = ATH_NODE_SAMPLE(an); 506 struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 507 const int size_bin = size_to_bin(frame_size); 508 const int size = bin_to_size(size_bin); 509 int tt, tries_so_far; 510 int is_ht40 = (an->an_node.ni_chw == 40); 511 512 if (!IS_RATE_DEFINED(sn, rix0)) 513 return; 514 tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries, 515 MIN(tries0, tries) - 1, is_ht40); 516 tries_so_far = tries0; 517 518 if (tries1 && tries_so_far < tries) { 519 if (!IS_RATE_DEFINED(sn, rix1)) 520 return; 521 tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries, 522 MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 523 tries_so_far += tries1; 524 } 525 526 if (tries2 && tries_so_far < tries) { 527 if (!IS_RATE_DEFINED(sn, rix2)) 528 return; 529 tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries, 530 MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 531 tries_so_far += tries2; 532 } 533 534 if (tries3 && tries_so_far < tries) { 535 if (!IS_RATE_DEFINED(sn, rix3)) 536 return; 537 tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries, 538 MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40); 539 } 540 541 if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) { 542 /* just average the first few packets */ 543 int avg_tx = sn->stats[size_bin][rix0].average_tx_time; 544 int packets = sn->stats[size_bin][rix0].total_packets; 545 sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+1); 546 } else { 547 /* use a ewma */ 548 sn->stats[size_bin][rix0].average_tx_time = 549 ((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) + 550 (tt * (100 - ssc->smoothing_rate))) / 100; 551 } 552 553 if (status != 0) { 554 int y; 555 sn->stats[size_bin][rix0].successive_failures++; 556 for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) { 557 /* 558 * Also say larger packets failed since we 559 * assume if a small packet fails at a 560 * bit-rate then a larger one will also. 561 */ 562 sn->stats[y][rix0].successive_failures++; 563 sn->stats[y][rix0].last_tx = ticks; 564 sn->stats[y][rix0].tries += tries; 565 sn->stats[y][rix0].total_packets++; 566 } 567 } else { 568 sn->stats[size_bin][rix0].packets_acked++; 569 sn->stats[size_bin][rix0].successive_failures = 0; 570 } 571 sn->stats[size_bin][rix0].tries += tries; 572 sn->stats[size_bin][rix0].last_tx = ticks; 573 sn->stats[size_bin][rix0].total_packets++; 574 575 if (rix0 == sn->current_sample_rix[size_bin]) { 576 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 577 &an->an_node, 578"%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)", 579 __func__, 580 size, 581 status ? "FAIL" : "OK", 582 rix0, short_tries, tries, tt, 583 sn->stats[size_bin][rix0].average_tx_time, 584 sn->stats[size_bin][rix0].perfect_tx_time); 585 sn->sample_tt[size_bin] = tt; 586 sn->current_sample_rix[size_bin] = -1; 587 } 588} 589 590static void 591badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status) 592{ 593 if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n", 594 series, hwrate, tries, status); 595} 596 597void 598ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 599 const struct ath_buf *bf) 600{ 601 struct ifnet *ifp = sc->sc_ifp; 602 struct ieee80211com *ic = ifp->if_l2com; 603 struct sample_node *sn = ATH_NODE_SAMPLE(an); 604 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 605 const struct ath_desc *ds0 = &bf->bf_desc[0]; 606 int final_rix, short_tries, long_tries, frame_size; 607 const HAL_RATE_TABLE *rt = sc->sc_currates; 608 int mrr; 609 610 final_rix = rt->rateCodeToIndex[ts->ts_rate]; 611 short_tries = ts->ts_shortretry; 612 long_tries = ts->ts_longretry + 1; 613 frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */ 614 if (frame_size == 0) /* NB: should not happen */ 615 frame_size = 1500; 616 617 if (sn->ratemask == 0) { 618 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 619 &an->an_node, 620 "%s: size %d %s rate/try %d/%d no rates yet", 621 __func__, 622 bin_to_size(size_to_bin(frame_size)), 623 ts->ts_status ? "FAIL" : "OK", 624 short_tries, long_tries); 625 return; 626 } 627 mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 628 if (!mrr || ts->ts_finaltsi == 0) { 629 if (!IS_RATE_DEFINED(sn, final_rix)) { 630 badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status); 631 return; 632 } 633 /* 634 * Only one rate was used; optimize work. 635 */ 636 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 637 &an->an_node, "%s: size %d (%d bytes) %s rate/try %d %s/%d/%d", 638 __func__, 639 bin_to_size(size_to_bin(frame_size)), 640 frame_size, 641 ts->ts_status ? "FAIL" : "OK", 642 dot11rate(rt, final_rix), dot11rate_label(rt, final_rix), short_tries, long_tries); 643 update_stats(sc, an, frame_size, 644 final_rix, long_tries, 645 0, 0, 646 0, 0, 647 0, 0, 648 short_tries, long_tries, ts->ts_status); 649 } else { 650 int hwrates[4], tries[4], rix[4]; 651 int finalTSIdx = ts->ts_finaltsi; 652 int i; 653 654 /* 655 * Process intermediate rates that failed. 656 */ 657 ath_hal_gettxcompletionrates(sc->sc_ah, ds0, hwrates, tries); 658 659 for (i = 0; i < 4; i++) { 660 rix[i] = rt->rateCodeToIndex[hwrates[i]]; 661 } 662 663 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 664 &an->an_node, 665"%s: size %d (%d bytes) finaltsidx %d tries %d %s rate/try [%d %s/%d %d %s/%d %d %s/%d %d %s/%d]", 666 __func__, 667 bin_to_size(size_to_bin(frame_size)), 668 frame_size, 669 finalTSIdx, 670 long_tries, 671 ts->ts_status ? "FAIL" : "OK", 672 dot11rate(rt, rix[0]), dot11rate_label(rt, rix[0]), tries[0], 673 dot11rate(rt, rix[1]), dot11rate_label(rt, rix[1]), tries[1], 674 dot11rate(rt, rix[2]), dot11rate_label(rt, rix[2]), tries[2], 675 dot11rate(rt, rix[3]), dot11rate_label(rt, rix[3]), tries[3]); 676 677 for (i = 0; i < 4; i++) { 678 if (tries[i] && !IS_RATE_DEFINED(sn, rix[i])) 679 badrate(ifp, 0, hwrates[i], tries[i], ts->ts_status); 680 } 681 682 /* 683 * NB: series > 0 are not penalized for failure 684 * based on the try counts under the assumption 685 * that losses are often bursty and since we 686 * sample higher rates 1 try at a time doing so 687 * may unfairly penalize them. 688 */ 689 if (tries[0]) { 690 update_stats(sc, an, frame_size, 691 rix[0], tries[0], 692 rix[1], tries[1], 693 rix[2], tries[2], 694 rix[3], tries[3], 695 short_tries, long_tries, 696 long_tries > tries[0]); 697 long_tries -= tries[0]; 698 } 699 700 if (tries[1] && finalTSIdx > 0) { 701 update_stats(sc, an, frame_size, 702 rix[1], tries[1], 703 rix[2], tries[2], 704 rix[3], tries[3], 705 0, 0, 706 short_tries, long_tries, 707 ts->ts_status); 708 long_tries -= tries[1]; 709 } 710 711 if (tries[2] && finalTSIdx > 1) { 712 update_stats(sc, an, frame_size, 713 rix[2], tries[2], 714 rix[3], tries[3], 715 0, 0, 716 0, 0, 717 short_tries, long_tries, 718 ts->ts_status); 719 long_tries -= tries[2]; 720 } 721 722 if (tries[3] && finalTSIdx > 2) { 723 update_stats(sc, an, frame_size, 724 rix[3], tries[3], 725 0, 0, 726 0, 0, 727 0, 0, 728 short_tries, long_tries, 729 ts->ts_status); 730 } 731 } 732} 733 734void 735ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 736{ 737 if (isnew) 738 ath_rate_ctl_reset(sc, &an->an_node); 739} 740 741static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = { 742 NULL, /* IEEE80211_MODE_AUTO */ 743 series_11a, /* IEEE80211_MODE_11A */ 744 series_11g, /* IEEE80211_MODE_11B */ 745 series_11g, /* IEEE80211_MODE_11G */ 746 NULL, /* IEEE80211_MODE_FH */ 747 series_11a, /* IEEE80211_MODE_TURBO_A */ 748 series_11g, /* IEEE80211_MODE_TURBO_G */ 749 series_11a, /* IEEE80211_MODE_STURBO_A */ 750 series_11na, /* IEEE80211_MODE_11NA */ 751 series_11ng, /* IEEE80211_MODE_11NG */ 752 series_half, /* IEEE80211_MODE_HALF */ 753 series_quarter, /* IEEE80211_MODE_QUARTER */ 754}; 755 756/* 757 * Initialize the tables for a node. 758 */ 759static void 760ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni) 761{ 762#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 763#define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL) 764#define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS) 765 struct ath_node *an = ATH_NODE(ni); 766 struct sample_node *sn = ATH_NODE_SAMPLE(an); 767 const HAL_RATE_TABLE *rt = sc->sc_currates; 768 int x, y, rix; 769 770 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 771 772 KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2, 773 ("curmode %u", sc->sc_curmode)); 774 sn->sched = mrr_schedules[sc->sc_curmode]; 775 KASSERT(sn->sched != NULL, 776 ("no mrr schedule for mode %u", sc->sc_curmode)); 777 778 sn->static_rix = -1; 779 ath_rate_update_static_rix(sc, ni); 780 781 /* 782 * Construct a bitmask of usable rates. This has all 783 * negotiated rates minus those marked by the hal as 784 * to be ignored for doing rate control. 785 */ 786 sn->ratemask = 0; 787 /* MCS rates */ 788 if (ni->ni_flags & IEEE80211_NODE_HT) { 789 for (x = 0; x < ni->ni_htrates.rs_nrates; x++) { 790 rix = sc->sc_rixmap[MCS(x)]; 791 if (rix == 0xff) 792 continue; 793 /* skip rates marked broken by hal */ 794 if (!rt->info[rix].valid) 795 continue; 796 KASSERT(rix < SAMPLE_MAXRATES, 797 ("mcs %u has rix %d", MCS(x), rix)); 798 sn->ratemask |= 1<<rix; 799 } 800 } 801 802 /* Legacy rates */ 803 for (x = 0; x < ni->ni_rates.rs_nrates; x++) { 804 rix = sc->sc_rixmap[RATE(x)]; 805 if (rix == 0xff) 806 continue; 807 /* skip rates marked broken by hal */ 808 if (!rt->info[rix].valid) 809 continue; 810 KASSERT(rix < SAMPLE_MAXRATES, 811 ("rate %u has rix %d", RATE(x), rix)); 812 sn->ratemask |= 1<<rix; 813 } 814#ifdef IEEE80211_DEBUG 815 if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) { 816 uint32_t mask; 817 818 ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt", 819 ni->ni_macaddr, ":", __func__); 820 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 821 if ((mask & 1) == 0) 822 continue; 823 printf(" %d %s/%d", dot11rate(rt, rix), dot11rate_label(rt, rix), 824 calc_usecs_unicast_packet(sc, 1600, rix, 0,0, 825 (ni->ni_chw == 40))); 826 } 827 printf("\n"); 828 } 829#endif 830 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 831 int size = bin_to_size(y); 832 uint32_t mask; 833 834 sn->packets_sent[y] = 0; 835 sn->current_sample_rix[y] = -1; 836 sn->last_sample_rix[y] = 0; 837 /* XXX start with first valid rate */ 838 sn->current_rix[y] = ffs(sn->ratemask)-1; 839 840 /* 841 * Initialize the statistics buckets; these are 842 * indexed by the rate code index. 843 */ 844 for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) { 845 if ((mask & 1) == 0) /* not a valid rate */ 846 continue; 847 sn->stats[y][rix].successive_failures = 0; 848 sn->stats[y][rix].tries = 0; 849 sn->stats[y][rix].total_packets = 0; 850 sn->stats[y][rix].packets_acked = 0; 851 sn->stats[y][rix].last_tx = 0; 852 853 sn->stats[y][rix].perfect_tx_time = 854 calc_usecs_unicast_packet(sc, size, rix, 0, 0, 855 (ni->ni_chw == 40)); 856 sn->stats[y][rix].average_tx_time = 857 sn->stats[y][rix].perfect_tx_time; 858 } 859 } 860#if 0 861 /* XXX 0, num_rates-1 are wrong */ 862 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 863 "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__, 864 sn->num_rates, 865 DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "", 866 sn->stats[1][0].perfect_tx_time, 867 DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "", 868 sn->stats[1][sn->num_rates-1].perfect_tx_time 869 ); 870#endif 871 /* set the visible bit-rate */ 872 if (sn->static_rix != -1) 873 ni->ni_txrate = DOT11RATE(sn->static_rix); 874 else 875 ni->ni_txrate = RATE(0); 876#undef RATE 877#undef DOT11RATE 878} 879 880static void 881sample_stats(void *arg, struct ieee80211_node *ni) 882{ 883 struct ath_softc *sc = arg; 884 const HAL_RATE_TABLE *rt = sc->sc_currates; 885 struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni)); 886 uint32_t mask; 887 int rix, y; 888 889 printf("\n[%s] refcnt %d static_rix %d ratemask 0x%x\n", 890 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni), 891 sn->static_rix, sn->ratemask); 892 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 893 printf("[%4u] cur rix %d (%d %s) since switch: packets %d ticks %u\n", 894 bin_to_size(y), sn->current_rix[y], 895 dot11rate(rt, sn->current_rix[y]), 896 dot11rate_label(rt, sn->current_rix[y]), 897 sn->packets_since_switch[y], sn->ticks_since_switch[y]); 898 printf("[%4u] last sample %d cur sample %d packets sent %d\n", 899 bin_to_size(y), sn->last_sample_rix[y], 900 sn->current_sample_rix[y], sn->packets_sent[y]); 901 printf("[%4u] packets since sample %d sample tt %u\n", 902 bin_to_size(y), sn->packets_since_sample[y], 903 sn->sample_tt[y]); 904 } 905 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) { 906 if ((mask & 1) == 0) 907 continue; 908 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 909 if (sn->stats[y][rix].total_packets == 0) 910 continue; 911 printf("[%2u %s:%4u] %8ju:%-8ju (%3d%%) T %8ju F %4d avg %5u last %u\n", 912 dot11rate(rt, rix), dot11rate_label(rt, rix), 913 bin_to_size(y), 914 (uintmax_t) sn->stats[y][rix].total_packets, 915 (uintmax_t) sn->stats[y][rix].packets_acked, 916 (int) ((sn->stats[y][rix].packets_acked * 100ULL) / 917 sn->stats[y][rix].total_packets), 918 (uintmax_t) sn->stats[y][rix].tries, 919 sn->stats[y][rix].successive_failures, 920 sn->stats[y][rix].average_tx_time, 921 ticks - sn->stats[y][rix].last_tx); 922 } 923 } 924} 925 926static int 927ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS) 928{ 929 struct ath_softc *sc = arg1; 930 struct ifnet *ifp = sc->sc_ifp; 931 struct ieee80211com *ic = ifp->if_l2com; 932 int error, v; 933 934 v = 0; 935 error = sysctl_handle_int(oidp, &v, 0, req); 936 if (error || !req->newptr) 937 return error; 938 ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc); 939 return 0; 940} 941 942static int 943ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS) 944{ 945 struct sample_softc *ssc = arg1; 946 int rate, error; 947 948 rate = ssc->smoothing_rate; 949 error = sysctl_handle_int(oidp, &rate, 0, req); 950 if (error || !req->newptr) 951 return error; 952 if (!(0 <= rate && rate < 100)) 953 return EINVAL; 954 ssc->smoothing_rate = rate; 955 ssc->smoothing_minpackets = 100 / (100 - rate); 956 return 0; 957} 958 959static int 960ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS) 961{ 962 struct sample_softc *ssc = arg1; 963 int rate, error; 964 965 rate = ssc->sample_rate; 966 error = sysctl_handle_int(oidp, &rate, 0, req); 967 if (error || !req->newptr) 968 return error; 969 if (!(2 <= rate && rate <= 100)) 970 return EINVAL; 971 ssc->sample_rate = rate; 972 return 0; 973} 974 975static void 976ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc) 977{ 978 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 979 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 980 981 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 982 "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0, 983 ath_rate_sysctl_smoothing_rate, "I", 984 "sample: smoothing rate for avg tx time (%%)"); 985 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 986 "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0, 987 ath_rate_sysctl_sample_rate, "I", 988 "sample: percent air time devoted to sampling new rates (%%)"); 989 /* XXX max_successive_failures, stale_failure_timeout, min_switch */ 990 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 991 "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 992 ath_rate_sysctl_stats, "I", "sample: print statistics"); 993} 994 995struct ath_ratectrl * 996ath_rate_attach(struct ath_softc *sc) 997{ 998 struct sample_softc *ssc; 999 1000 ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 1001 if (ssc == NULL) 1002 return NULL; 1003 ssc->arc.arc_space = sizeof(struct sample_node); 1004 ssc->smoothing_rate = 95; /* ewma percentage ([0..99]) */ 1005 ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate); 1006 ssc->sample_rate = 10; /* %time to try diff tx rates */ 1007 ssc->max_successive_failures = 3; /* threshold for rate sampling*/ 1008 ssc->stale_failure_timeout = 10 * hz; /* 10 seconds */ 1009 ssc->min_switch = hz; /* 1 second */ 1010 ath_rate_sysctlattach(sc, ssc); 1011 return &ssc->arc; 1012} 1013 1014void 1015ath_rate_detach(struct ath_ratectrl *arc) 1016{ 1017 struct sample_softc *ssc = (struct sample_softc *) arc; 1018 1019 free(ssc, M_DEVBUF); 1020} 1021