onoe.c revision 155477
1/*-
2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
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#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/onoe/onoe.c 155477 2006-02-09 20:49:55Z sam $");
39
40/*
41 * Atsushi Onoe's rate control algorithm.
42 */
43#include "opt_inet.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysctl.h>
48#include <sys/module.h>
49#include <sys/kernel.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#include <sys/errno.h>
53
54#include <machine/bus.h>
55#include <machine/resource.h>
56#include <sys/bus.h>
57
58#include <sys/socket.h>
59
60#include <net/if.h>
61#include <net/if_media.h>
62#include <net/if_arp.h>
63#include <net/ethernet.h>		/* XXX for ether_sprintf */
64
65#include <net80211/ieee80211_var.h>
66
67#include <net/bpf.h>
68
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72#endif
73
74#include <dev/ath/if_athvar.h>
75#include <dev/ath/ath_rate/onoe/onoe.h>
76#include <contrib/dev/ath/ah_desc.h>
77
78#define	ONOE_DEBUG
79#ifdef ONOE_DEBUG
80enum {
81	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
82};
83#define	DPRINTF(sc, _fmt, ...) do {				\
84	if (sc->sc_debug & ATH_DEBUG_RATE)			\
85		printf(_fmt, __VA_ARGS__);			\
86} while (0)
87#else
88#define	DPRINTF(sc, _fmt, ...)
89#endif
90
91/*
92 * Default parameters for the rate control algorithm.  These are
93 * all tunable with sysctls.  The rate controller runs periodically
94 * (each ath_rateinterval ms) analyzing transmit statistics for each
95 * neighbor/station (when operating in station mode this is only the AP).
96 * If transmits look to be working well over a sampling period then
97 * it gives a "raise rate credit".  If transmits look to not be working
98 * well than it deducts a credit.  If the credits cross a threshold then
99 * the transmit rate is raised.  Various error conditions force the
100 * the transmit rate to be dropped.
101 *
102 * The decision to issue/deduct a credit is based on the errors and
103 * retries accumulated over the sampling period.  ath_rate_raise defines
104 * the percent of retransmits for which a credit is issued/deducted.
105 * ath_rate_raise_threshold defines the threshold on credits at which
106 * the transmit rate is increased.
107 *
108 * XXX this algorithm is flawed.
109 */
110static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
111static	int ath_rate_raise = 10;		/* add credit threshold */
112static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
113
114static void	ath_ratectl(void *);
115static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
116			int rate);
117static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
118static void	ath_rate_ctl(void *, struct ieee80211_node *);
119
120void
121ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
122{
123	/* NB: assumed to be zero'd by caller */
124	ath_rate_update(sc, &an->an_node, 0);
125}
126
127void
128ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
129{
130}
131
132void
133ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
134	int shortPreamble, size_t frameLen,
135	u_int8_t *rix, int *try0, u_int8_t *txrate)
136{
137	struct onoe_node *on = ATH_NODE_ONOE(an);
138
139	*rix = on->on_tx_rix0;
140	*try0 = on->on_tx_try0;
141	if (shortPreamble)
142		*txrate = on->on_tx_rate0sp;
143	else
144		*txrate = on->on_tx_rate0;
145}
146
147void
148ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
149	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
150{
151	struct onoe_node *on = ATH_NODE_ONOE(an);
152
153	ath_hal_setupxtxdesc(sc->sc_ah, ds
154		, on->on_tx_rate1sp, 2	/* series 1 */
155		, on->on_tx_rate2sp, 2	/* series 2 */
156		, on->on_tx_rate3sp, 2	/* series 3 */
157	);
158}
159
160void
161ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
162	const struct ath_desc *ds, const struct ath_desc *ds0)
163{
164	struct onoe_node *on = ATH_NODE_ONOE(an);
165
166	if (ds->ds_txstat.ts_status == 0)
167		on->on_tx_ok++;
168	else
169		on->on_tx_err++;
170	on->on_tx_retr += ds->ds_txstat.ts_shortretry
171			+ ds->ds_txstat.ts_longretry;
172}
173
174void
175ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
176{
177	if (isnew)
178		ath_rate_ctl_start(sc, &an->an_node);
179}
180
181static void
182ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
183{
184	struct ath_node *an = ATH_NODE(ni);
185	struct onoe_node *on = ATH_NODE_ONOE(an);
186	const HAL_RATE_TABLE *rt = sc->sc_currates;
187	u_int8_t rix;
188
189	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
190
191	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
192	    __func__, ether_sprintf(ni->ni_macaddr),
193	    ni->ni_rates.rs_nrates > 0 ?
194		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
195
196	ni->ni_txrate = rate;
197	/*
198	 * Before associating a node has no rate set setup
199	 * so we can't calculate any transmit codes to use.
200	 * This is ok since we should never be sending anything
201	 * but management frames and those always go at the
202	 * lowest hardware rate.
203	 */
204	if (ni->ni_rates.rs_nrates == 0)
205		goto done;
206	on->on_tx_rix0 = sc->sc_rixmap[
207		ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
208	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
209
210	on->on_tx_rate0sp = on->on_tx_rate0 |
211		rt->info[on->on_tx_rix0].shortPreamble;
212	if (sc->sc_mrretry) {
213		/*
214		 * Hardware supports multi-rate retry; setup two
215		 * step-down retry rates and make the lowest rate
216		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
217		 * respectively (4 is set here, the rest are fixed
218		 * in the xmit routine).
219		 */
220		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
221		if (--rate >= 0) {
222			rix = sc->sc_rixmap[
223				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
224			on->on_tx_rate1 = rt->info[rix].rateCode;
225			on->on_tx_rate1sp = on->on_tx_rate1 |
226				rt->info[rix].shortPreamble;
227		} else {
228			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
229		}
230		if (--rate >= 0) {
231			rix = sc->sc_rixmap[
232				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
233			on->on_tx_rate2 = rt->info[rix].rateCode;
234			on->on_tx_rate2sp = on->on_tx_rate2 |
235				rt->info[rix].shortPreamble;
236		} else {
237			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
238		}
239		if (rate > 0) {
240			/* NB: only do this if we didn't already do it above */
241			on->on_tx_rate3 = rt->info[0].rateCode;
242			on->on_tx_rate3sp =
243				on->on_tx_rate3 | rt->info[0].shortPreamble;
244		} else {
245			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
246		}
247	} else {
248		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
249		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
250		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
251		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
252	}
253done:
254	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
255}
256
257/*
258 * Set the starting transmit rate for a node.
259 */
260static void
261ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
262{
263#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
264	struct ieee80211com *ic = &sc->sc_ic;
265	int srate;
266
267	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
268	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
269		/*
270		 * No fixed rate is requested. For 11b start with
271		 * the highest negotiated rate; otherwise, for 11g
272		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
273		 */
274		srate = ni->ni_rates.rs_nrates - 1;
275		if (sc->sc_curmode != IEEE80211_MODE_11B) {
276			/*
277			 * Scan the negotiated rate set to find the
278			 * closest rate.
279			 */
280			/* NB: the rate set is assumed sorted */
281			for (; srate >= 0 && RATE(srate) > 72; srate--)
282				;
283			KASSERT(srate >= 0, ("bogus rate set"));
284		}
285	} else {
286		/*
287		 * A fixed rate is to be used; ic_fixed_rate is an
288		 * index into the supported rate set.  Convert this
289		 * to the index into the negotiated rate set for
290		 * the node.  We know the rate is there because the
291		 * rate set is checked when the station associates.
292		 */
293		const struct ieee80211_rateset *rs =
294			&ic->ic_sup_rates[ic->ic_curmode];
295		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
296		/* NB: the rate set is assumed sorted */
297		srate = ni->ni_rates.rs_nrates - 1;
298		for (; srate >= 0 && RATE(srate) != r; srate--)
299			;
300		KASSERT(srate >= 0,
301			("fixed rate %d not in rate set", ic->ic_fixed_rate));
302	}
303	ath_rate_update(sc, ni, srate);
304#undef RATE
305}
306
307static void
308ath_rate_cb(void *arg, struct ieee80211_node *ni)
309{
310	struct ath_softc *sc = arg;
311
312	ath_rate_update(sc, ni, 0);
313}
314
315/*
316 * Reset the rate control state for each 802.11 state transition.
317 */
318void
319ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
320{
321	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
322	struct ieee80211com *ic = &sc->sc_ic;
323	struct ieee80211_node *ni;
324
325	if (state == IEEE80211_S_INIT) {
326		callout_stop(&osc->timer);
327		return;
328	}
329	if (ic->ic_opmode == IEEE80211_M_STA) {
330		/*
331		 * Reset local xmit state; this is really only
332		 * meaningful when operating in station mode.
333		 */
334		ni = ic->ic_bss;
335		if (state == IEEE80211_S_RUN) {
336			ath_rate_ctl_start(sc, ni);
337		} else {
338			ath_rate_update(sc, ni, 0);
339		}
340	} else {
341		/*
342		 * When operating as a station the node table holds
343		 * the AP's that were discovered during scanning.
344		 * For any other operating mode we want to reset the
345		 * tx rate state of each node.
346		 */
347		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
348		ath_rate_update(sc, ic->ic_bss, 0);
349	}
350	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
351	    state == IEEE80211_S_RUN) {
352		int interval;
353		/*
354		 * Start the background rate control thread if we
355		 * are not configured to use a fixed xmit rate.
356		 */
357		interval = ath_rateinterval;
358		if (ic->ic_opmode == IEEE80211_M_STA)
359			interval /= 2;
360		callout_reset(&osc->timer, (interval * hz) / 1000,
361			ath_ratectl, sc->sc_ifp);
362	}
363}
364
365/*
366 * Examine and potentially adjust the transmit rate.
367 */
368static void
369ath_rate_ctl(void *arg, struct ieee80211_node *ni)
370{
371	struct ath_softc *sc = arg;
372	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
373	struct ieee80211_rateset *rs = &ni->ni_rates;
374	int dir = 0, nrate, enough;
375
376	/*
377	 * Rate control
378	 * XXX: very primitive version.
379	 */
380	enough = (on->on_tx_ok + on->on_tx_err >= 10);
381
382	/* no packet reached -> down */
383	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
384		dir = -1;
385
386	/* all packets needs retry in average -> down */
387	if (enough && on->on_tx_ok < on->on_tx_retr)
388		dir = -1;
389
390	/* no error and less than rate_raise% of packets need retry -> up */
391	if (enough && on->on_tx_err == 0 &&
392	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
393		dir = 1;
394
395	DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
396		ether_sprintf(ni->ni_macaddr),
397		on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
398		on->on_tx_upper, dir);
399
400	nrate = ni->ni_txrate;
401	switch (dir) {
402	case 0:
403		if (enough && on->on_tx_upper > 0)
404			on->on_tx_upper--;
405		break;
406	case -1:
407		if (nrate > 0) {
408			nrate--;
409			sc->sc_stats.ast_rate_drop++;
410		}
411		on->on_tx_upper = 0;
412		break;
413	case 1:
414		/* raise rate if we hit rate_raise_threshold */
415		if (++on->on_tx_upper < ath_rate_raise_threshold)
416			break;
417		on->on_tx_upper = 0;
418		if (nrate + 1 < rs->rs_nrates) {
419			nrate++;
420			sc->sc_stats.ast_rate_raise++;
421		}
422		break;
423	}
424
425	if (nrate != ni->ni_txrate) {
426		DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
427		    __func__,
428		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
429		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
430		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
431		ath_rate_update(sc, ni, nrate);
432	} else if (enough)
433		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
434}
435
436static void
437ath_ratectl(void *arg)
438{
439	struct ifnet *ifp = arg;
440	struct ath_softc *sc = ifp->if_softc;
441	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
442	struct ieee80211com *ic = &sc->sc_ic;
443	int interval;
444
445	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
446		sc->sc_stats.ast_rate_calls++;
447
448		if (ic->ic_opmode == IEEE80211_M_STA)
449			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
450		else
451			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
452	}
453	interval = ath_rateinterval;
454	if (ic->ic_opmode == IEEE80211_M_STA)
455		interval /= 2;
456	callout_reset(&osc->timer, (interval * hz) / 1000,
457		ath_ratectl, sc->sc_ifp);
458}
459
460static void
461ath_rate_sysctlattach(struct ath_softc *sc)
462{
463	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
464	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
465
466	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
467		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
468		"rate control: operation interval (ms)");
469	/* XXX bounds check values */
470	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
471		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
472		"rate control: retry threshold to credit rate raise (%%)");
473	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
474		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
475		"rate control: # good periods before raising rate");
476}
477
478struct ath_ratectrl *
479ath_rate_attach(struct ath_softc *sc)
480{
481	struct onoe_softc *osc;
482
483	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
484	if (osc == NULL)
485		return NULL;
486	osc->arc.arc_space = sizeof(struct onoe_node);
487	callout_init(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
488	ath_rate_sysctlattach(sc);
489
490	return &osc->arc;
491}
492
493void
494ath_rate_detach(struct ath_ratectrl *arc)
495{
496	struct onoe_softc *osc = (struct onoe_softc *) arc;
497
498	callout_drain(&osc->timer);
499	free(osc, M_DEVBUF);
500}
501
502/*
503 * Module glue.
504 */
505static int
506onoe_modevent(module_t mod, int type, void *unused)
507{
508	switch (type) {
509	case MOD_LOAD:
510		if (bootverbose)
511			printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n");
512		return 0;
513	case MOD_UNLOAD:
514		return 0;
515	}
516	return EINVAL;
517}
518
519static moduledata_t onoe_mod = {
520	"ath_rate",
521	onoe_modevent,
522	0
523};
524DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
525MODULE_VERSION(ath_rate, 1);
526MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
527