onoe.c revision 184347
1138569Ssam/*-
2170375Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3138569Ssam * All rights reserved.
4138569Ssam *
5138569Ssam * Redistribution and use in source and binary forms, with or without
6138569Ssam * modification, are permitted provided that the following conditions
7138569Ssam * are met:
8138569Ssam * 1. Redistributions of source code must retain the above copyright
9138569Ssam *    notice, this list of conditions and the following disclaimer,
10138569Ssam *    without modification.
11138569Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12138569Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13138569Ssam *    redistribution must be conditioned upon including a substantially
14138569Ssam *    similar Disclaimer requirement for further binary redistribution.
15138569Ssam *
16138569Ssam * NO WARRANTY
17138569Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18138569Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19138569Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20138569Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21138569Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22138569Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23138569Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24138569Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25138569Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26138569Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27138569Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28138569Ssam */
29138569Ssam
30138569Ssam#include <sys/cdefs.h>
31138569Ssam__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/onoe/onoe.c 184347 2008-10-27 17:03:24Z sam $");
32138569Ssam
33138569Ssam/*
34138569Ssam * Atsushi Onoe's rate control algorithm.
35138569Ssam */
36138569Ssam#include "opt_inet.h"
37178354Ssam#include "opt_wlan.h"
38138569Ssam
39138569Ssam#include <sys/param.h>
40138569Ssam#include <sys/systm.h>
41138569Ssam#include <sys/sysctl.h>
42138569Ssam#include <sys/module.h>
43138569Ssam#include <sys/kernel.h>
44138569Ssam#include <sys/lock.h>
45138569Ssam#include <sys/mutex.h>
46138569Ssam#include <sys/errno.h>
47138569Ssam
48138569Ssam#include <machine/bus.h>
49138569Ssam#include <machine/resource.h>
50138569Ssam#include <sys/bus.h>
51138569Ssam
52138569Ssam#include <sys/socket.h>
53138569Ssam
54138569Ssam#include <net/if.h>
55138569Ssam#include <net/if_media.h>
56138569Ssam#include <net/if_arp.h>
57138569Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
58138569Ssam
59138569Ssam#include <net80211/ieee80211_var.h>
60138569Ssam
61138569Ssam#include <net/bpf.h>
62138569Ssam
63138569Ssam#ifdef INET
64138569Ssam#include <netinet/in.h>
65138569Ssam#include <netinet/if_ether.h>
66138569Ssam#endif
67138569Ssam
68138569Ssam#include <dev/ath/if_athvar.h>
69138569Ssam#include <dev/ath/ath_rate/onoe/onoe.h>
70138569Ssam#include <contrib/dev/ath/ah_desc.h>
71138569Ssam
72138569Ssam/*
73138569Ssam * Default parameters for the rate control algorithm.  These are
74138569Ssam * all tunable with sysctls.  The rate controller runs periodically
75138569Ssam * (each ath_rateinterval ms) analyzing transmit statistics for each
76138569Ssam * neighbor/station (when operating in station mode this is only the AP).
77138569Ssam * If transmits look to be working well over a sampling period then
78138569Ssam * it gives a "raise rate credit".  If transmits look to not be working
79138569Ssam * well than it deducts a credit.  If the credits cross a threshold then
80138569Ssam * the transmit rate is raised.  Various error conditions force the
81138569Ssam * the transmit rate to be dropped.
82138569Ssam *
83138569Ssam * The decision to issue/deduct a credit is based on the errors and
84138569Ssam * retries accumulated over the sampling period.  ath_rate_raise defines
85138569Ssam * the percent of retransmits for which a credit is issued/deducted.
86138569Ssam * ath_rate_raise_threshold defines the threshold on credits at which
87138569Ssam * the transmit rate is increased.
88138569Ssam *
89138569Ssam * XXX this algorithm is flawed.
90138569Ssam */
91138569Ssamstatic	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
92138569Ssamstatic	int ath_rate_raise = 10;		/* add credit threshold */
93138569Ssamstatic	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
94138569Ssam
95138569Ssamstatic void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
96138569Ssam			int rate);
97138569Ssamstatic void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
98138569Ssamstatic void	ath_rate_ctl(void *, struct ieee80211_node *);
99138569Ssam
100138569Ssamvoid
101138569Ssamath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
102138569Ssam{
103138569Ssam	/* NB: assumed to be zero'd by caller */
104138569Ssam}
105138569Ssam
106138569Ssamvoid
107138569Ssamath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
108138569Ssam{
109138569Ssam}
110138569Ssam
111138569Ssamvoid
112138569Ssamath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
113144546Ssam	int shortPreamble, size_t frameLen,
114138569Ssam	u_int8_t *rix, int *try0, u_int8_t *txrate)
115138569Ssam{
116138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
117138569Ssam
118138569Ssam	*rix = on->on_tx_rix0;
119138569Ssam	*try0 = on->on_tx_try0;
120138569Ssam	if (shortPreamble)
121138569Ssam		*txrate = on->on_tx_rate0sp;
122138569Ssam	else
123138569Ssam		*txrate = on->on_tx_rate0;
124138569Ssam}
125138569Ssam
126138569Ssamvoid
127138569Ssamath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
128144546Ssam	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
129138569Ssam{
130138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
131138569Ssam
132138569Ssam	ath_hal_setupxtxdesc(sc->sc_ah, ds
133138569Ssam		, on->on_tx_rate1sp, 2	/* series 1 */
134138569Ssam		, on->on_tx_rate2sp, 2	/* series 2 */
135138569Ssam		, on->on_tx_rate3sp, 2	/* series 3 */
136138569Ssam	);
137138569Ssam}
138138569Ssam
139138569Ssamvoid
140144347Ssamath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
141165185Ssam	const struct ath_buf *bf)
142138569Ssam{
143138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
144165185Ssam	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
145138569Ssam
146165185Ssam	if (ts->ts_status == 0)
147138569Ssam		on->on_tx_ok++;
148138569Ssam	else
149138569Ssam		on->on_tx_err++;
150165185Ssam	on->on_tx_retr += ts->ts_shortretry
151165185Ssam			+ ts->ts_longretry;
152178354Ssam	if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
153178354Ssam		ath_rate_ctl(sc, &an->an_node);
154178354Ssam		on->on_ticks = ticks;
155178354Ssam	}
156138569Ssam}
157138569Ssam
158138569Ssamvoid
159138569Ssamath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
160138569Ssam{
161138569Ssam	if (isnew)
162138569Ssam		ath_rate_ctl_start(sc, &an->an_node);
163138569Ssam}
164138569Ssam
165138569Ssamstatic void
166138569Ssamath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
167138569Ssam{
168138569Ssam	struct ath_node *an = ATH_NODE(ni);
169138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
170178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
171138569Ssam	const HAL_RATE_TABLE *rt = sc->sc_currates;
172138569Ssam	u_int8_t rix;
173138569Ssam
174138569Ssam	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
175138569Ssam
176178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
177178354Ssam	     "%s: set xmit rate to %dM", __func__,
178178354Ssam	     ni->ni_rates.rs_nrates > 0 ?
179138569Ssam		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
180138569Ssam
181138569Ssam	/*
182138569Ssam	 * Before associating a node has no rate set setup
183138569Ssam	 * so we can't calculate any transmit codes to use.
184138569Ssam	 * This is ok since we should never be sending anything
185138569Ssam	 * but management frames and those always go at the
186138569Ssam	 * lowest hardware rate.
187138569Ssam	 */
188138569Ssam	if (ni->ni_rates.rs_nrates == 0)
189138569Ssam		goto done;
190178354Ssam	on->on_rix = rate;
191178354Ssam	ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
192178354Ssam	on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
193138569Ssam	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
194138569Ssam
195138569Ssam	on->on_tx_rate0sp = on->on_tx_rate0 |
196138569Ssam		rt->info[on->on_tx_rix0].shortPreamble;
197138569Ssam	if (sc->sc_mrretry) {
198138569Ssam		/*
199138569Ssam		 * Hardware supports multi-rate retry; setup two
200138569Ssam		 * step-down retry rates and make the lowest rate
201138569Ssam		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
202138569Ssam		 * respectively (4 is set here, the rest are fixed
203138569Ssam		 * in the xmit routine).
204138569Ssam		 */
205138569Ssam		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
206138569Ssam		if (--rate >= 0) {
207138569Ssam			rix = sc->sc_rixmap[
208138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
209138569Ssam			on->on_tx_rate1 = rt->info[rix].rateCode;
210138569Ssam			on->on_tx_rate1sp = on->on_tx_rate1 |
211138569Ssam				rt->info[rix].shortPreamble;
212138569Ssam		} else {
213138569Ssam			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
214138569Ssam		}
215138569Ssam		if (--rate >= 0) {
216138569Ssam			rix = sc->sc_rixmap[
217138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
218138569Ssam			on->on_tx_rate2 = rt->info[rix].rateCode;
219138569Ssam			on->on_tx_rate2sp = on->on_tx_rate2 |
220138569Ssam				rt->info[rix].shortPreamble;
221138569Ssam		} else {
222138569Ssam			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
223138569Ssam		}
224138569Ssam		if (rate > 0) {
225138569Ssam			/* NB: only do this if we didn't already do it above */
226138569Ssam			on->on_tx_rate3 = rt->info[0].rateCode;
227138569Ssam			on->on_tx_rate3sp =
228155477Ssam				on->on_tx_rate3 | rt->info[0].shortPreamble;
229138569Ssam		} else {
230138569Ssam			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
231138569Ssam		}
232138569Ssam	} else {
233138569Ssam		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
234138569Ssam		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
235138569Ssam		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
236138569Ssam		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
237138569Ssam	}
238138569Ssamdone:
239138569Ssam	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
240178354Ssam
241178354Ssam	on->on_interval = ath_rateinterval;
242178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA)
243178354Ssam		on->on_interval /= 2;
244178354Ssam	on->on_interval = (on->on_interval * hz) / 1000;
245138569Ssam}
246138569Ssam
247138569Ssam/*
248138569Ssam * Set the starting transmit rate for a node.
249138569Ssam */
250138569Ssamstatic void
251138569Ssamath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
252138569Ssam{
253138569Ssam#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
254184347Ssam	const struct ieee80211_txparam *tp = ni->ni_txparms;
255138569Ssam	int srate;
256138569Ssam
257138569Ssam	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
258178354Ssam	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
259138569Ssam		/*
260138569Ssam		 * No fixed rate is requested. For 11b start with
261138569Ssam		 * the highest negotiated rate; otherwise, for 11g
262138569Ssam		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
263138569Ssam		 */
264138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
265138569Ssam		if (sc->sc_curmode != IEEE80211_MODE_11B) {
266138569Ssam			/*
267138569Ssam			 * Scan the negotiated rate set to find the
268138569Ssam			 * closest rate.
269138569Ssam			 */
270138569Ssam			/* NB: the rate set is assumed sorted */
271138569Ssam			for (; srate >= 0 && RATE(srate) > 72; srate--)
272138569Ssam				;
273138569Ssam		}
274138569Ssam	} else {
275138569Ssam		/*
276170530Ssam		 * A fixed rate is to be used; ic_fixed_rate is the
277170530Ssam		 * IEEE code for this rate (sans basic bit).  Convert this
278138569Ssam		 * to the index into the negotiated rate set for
279138569Ssam		 * the node.  We know the rate is there because the
280138569Ssam		 * rate set is checked when the station associates.
281138569Ssam		 */
282138569Ssam		/* NB: the rate set is assumed sorted */
283138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
284178354Ssam		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
285138569Ssam			;
286138569Ssam	}
287170530Ssam	/*
288170530Ssam	 * The selected rate may not be available due to races
289170530Ssam	 * and mode settings.  Also orphaned nodes created in
290170530Ssam	 * adhoc mode may not have any rate set so this lookup
291170530Ssam	 * can fail.  This is not fatal.
292170530Ssam	 */
293170530Ssam	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
294138569Ssam#undef RATE
295138569Ssam}
296138569Ssam
297138569Ssam/*
298138569Ssam * Examine and potentially adjust the transmit rate.
299138569Ssam */
300138569Ssamstatic void
301138569Ssamath_rate_ctl(void *arg, struct ieee80211_node *ni)
302138569Ssam{
303138569Ssam	struct ath_softc *sc = arg;
304138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
305138569Ssam	struct ieee80211_rateset *rs = &ni->ni_rates;
306138569Ssam	int dir = 0, nrate, enough;
307138569Ssam
308138569Ssam	/*
309138569Ssam	 * Rate control
310138569Ssam	 * XXX: very primitive version.
311138569Ssam	 */
312138569Ssam	enough = (on->on_tx_ok + on->on_tx_err >= 10);
313138569Ssam
314138569Ssam	/* no packet reached -> down */
315138569Ssam	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
316138569Ssam		dir = -1;
317138569Ssam
318138569Ssam	/* all packets needs retry in average -> down */
319138569Ssam	if (enough && on->on_tx_ok < on->on_tx_retr)
320138569Ssam		dir = -1;
321138569Ssam
322138569Ssam	/* no error and less than rate_raise% of packets need retry -> up */
323138569Ssam	if (enough && on->on_tx_err == 0 &&
324138569Ssam	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
325138569Ssam		dir = 1;
326138569Ssam
327178354Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
328178354Ssam	    "ok %d err %d retr %d upper %d dir %d",
329178354Ssam	    on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
330138569Ssam
331178354Ssam	nrate = on->on_rix;
332138569Ssam	switch (dir) {
333138569Ssam	case 0:
334138569Ssam		if (enough && on->on_tx_upper > 0)
335138569Ssam			on->on_tx_upper--;
336138569Ssam		break;
337138569Ssam	case -1:
338138569Ssam		if (nrate > 0) {
339138569Ssam			nrate--;
340138569Ssam			sc->sc_stats.ast_rate_drop++;
341138569Ssam		}
342138569Ssam		on->on_tx_upper = 0;
343138569Ssam		break;
344138569Ssam	case 1:
345138569Ssam		/* raise rate if we hit rate_raise_threshold */
346138569Ssam		if (++on->on_tx_upper < ath_rate_raise_threshold)
347138569Ssam			break;
348138569Ssam		on->on_tx_upper = 0;
349138569Ssam		if (nrate + 1 < rs->rs_nrates) {
350138569Ssam			nrate++;
351138569Ssam			sc->sc_stats.ast_rate_raise++;
352138569Ssam		}
353138569Ssam		break;
354138569Ssam	}
355138569Ssam
356178354Ssam	if (nrate != on->on_rix) {
357178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
358178354Ssam		    "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
359178354Ssam		    ni->ni_txrate / 2,
360138569Ssam		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
361138569Ssam		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
362138569Ssam		ath_rate_update(sc, ni, nrate);
363138569Ssam	} else if (enough)
364138569Ssam		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
365138569Ssam}
366138569Ssam
367138569Ssamstatic void
368138569Ssamath_rate_sysctlattach(struct ath_softc *sc)
369138569Ssam{
370138569Ssam	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
371138569Ssam	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
372138569Ssam
373138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
374138569Ssam		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
375138569Ssam		"rate control: operation interval (ms)");
376138569Ssam	/* XXX bounds check values */
377138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
378138569Ssam		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
379138569Ssam		"rate control: retry threshold to credit rate raise (%%)");
380138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
381138569Ssam		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
382138569Ssam		"rate control: # good periods before raising rate");
383138569Ssam}
384138569Ssam
385138569Ssamstruct ath_ratectrl *
386138569Ssamath_rate_attach(struct ath_softc *sc)
387138569Ssam{
388138569Ssam	struct onoe_softc *osc;
389138569Ssam
390138569Ssam	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
391138569Ssam	if (osc == NULL)
392138569Ssam		return NULL;
393138569Ssam	osc->arc.arc_space = sizeof(struct onoe_node);
394138569Ssam	ath_rate_sysctlattach(sc);
395138569Ssam
396138569Ssam	return &osc->arc;
397138569Ssam}
398138569Ssam
399138569Ssamvoid
400138569Ssamath_rate_detach(struct ath_ratectrl *arc)
401138569Ssam{
402138569Ssam	struct onoe_softc *osc = (struct onoe_softc *) arc;
403138569Ssam
404138569Ssam	free(osc, M_DEVBUF);
405138569Ssam}
406138569Ssam
407138569Ssam/*
408138569Ssam * Module glue.
409138569Ssam */
410138569Ssamstatic int
411138569Ssamonoe_modevent(module_t mod, int type, void *unused)
412138569Ssam{
413138569Ssam	switch (type) {
414138569Ssam	case MOD_LOAD:
415138569Ssam		if (bootverbose)
416138569Ssam			printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n");
417138569Ssam		return 0;
418138569Ssam	case MOD_UNLOAD:
419138569Ssam		return 0;
420138569Ssam	}
421138569Ssam	return EINVAL;
422138569Ssam}
423138569Ssam
424138569Ssamstatic moduledata_t onoe_mod = {
425138569Ssam	"ath_rate",
426138569Ssam	onoe_modevent,
427138569Ssam	0
428138569Ssam};
429138569SsamDECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
430138569SsamMODULE_VERSION(ath_rate, 1);
431138569SsamMODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
432