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