onoe.c revision 178354
1130803Smarcel/*-
2130803Smarcel * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3130803Smarcel * All rights reserved.
4130803Smarcel *
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 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/onoe/onoe.c 178354 2008-04-20 20:35:46Z sam $");
32
33/*
34 * Atsushi Onoe's rate control algorithm.
35 */
36#include "opt_inet.h"
37#include "opt_wlan.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/sysctl.h>
42#include <sys/module.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/errno.h>
47
48#include <machine/bus.h>
49#include <machine/resource.h>
50#include <sys/bus.h>
51
52#include <sys/socket.h>
53
54#include <net/if.h>
55#include <net/if_media.h>
56#include <net/if_arp.h>
57#include <net/ethernet.h>		/* XXX for ether_sprintf */
58
59#include <net80211/ieee80211_var.h>
60
61#include <net/bpf.h>
62
63#ifdef INET
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#endif
67
68#include <dev/ath/if_athvar.h>
69#include <dev/ath/ath_rate/onoe/onoe.h>
70#include <contrib/dev/ath/ah_desc.h>
71
72/*
73 * Default parameters for the rate control algorithm.  These are
74 * all tunable with sysctls.  The rate controller runs periodically
75 * (each ath_rateinterval ms) analyzing transmit statistics for each
76 * neighbor/station (when operating in station mode this is only the AP).
77 * If transmits look to be working well over a sampling period then
78 * it gives a "raise rate credit".  If transmits look to not be working
79 * well than it deducts a credit.  If the credits cross a threshold then
80 * the transmit rate is raised.  Various error conditions force the
81 * the transmit rate to be dropped.
82 *
83 * The decision to issue/deduct a credit is based on the errors and
84 * retries accumulated over the sampling period.  ath_rate_raise defines
85 * the percent of retransmits for which a credit is issued/deducted.
86 * ath_rate_raise_threshold defines the threshold on credits at which
87 * the transmit rate is increased.
88 *
89 * XXX this algorithm is flawed.
90 */
91static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
92static	int ath_rate_raise = 10;		/* add credit threshold */
93static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
94
95static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
96			int rate);
97static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
98static void	ath_rate_ctl(void *, struct ieee80211_node *);
99
100void
101ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
102{
103	/* NB: assumed to be zero'd by caller */
104}
105
106void
107ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
108{
109}
110
111void
112ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
113	int shortPreamble, size_t frameLen,
114	u_int8_t *rix, int *try0, u_int8_t *txrate)
115{
116	struct onoe_node *on = ATH_NODE_ONOE(an);
117
118	*rix = on->on_tx_rix0;
119	*try0 = on->on_tx_try0;
120	if (shortPreamble)
121		*txrate = on->on_tx_rate0sp;
122	else
123		*txrate = on->on_tx_rate0;
124}
125
126void
127ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
128	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
129{
130	struct onoe_node *on = ATH_NODE_ONOE(an);
131
132	ath_hal_setupxtxdesc(sc->sc_ah, ds
133		, on->on_tx_rate1sp, 2	/* series 1 */
134		, on->on_tx_rate2sp, 2	/* series 2 */
135		, on->on_tx_rate3sp, 2	/* series 3 */
136	);
137}
138
139void
140ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
141	const struct ath_buf *bf)
142{
143	struct onoe_node *on = ATH_NODE_ONOE(an);
144	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
145
146	if (ts->ts_status == 0)
147		on->on_tx_ok++;
148	else
149		on->on_tx_err++;
150	on->on_tx_retr += ts->ts_shortretry
151			+ ts->ts_longretry;
152	if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
153		ath_rate_ctl(sc, &an->an_node);
154		on->on_ticks = ticks;
155	}
156}
157
158void
159ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
160{
161	if (isnew)
162		ath_rate_ctl_start(sc, &an->an_node);
163}
164
165static void
166ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
167{
168	struct ath_node *an = ATH_NODE(ni);
169	struct onoe_node *on = ATH_NODE_ONOE(an);
170	struct ieee80211vap *vap = ni->ni_vap;
171	const HAL_RATE_TABLE *rt = sc->sc_currates;
172	u_int8_t rix;
173
174	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
175
176	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
177	     "%s: set xmit rate to %dM", __func__,
178	     ni->ni_rates.rs_nrates > 0 ?
179		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
180
181	/*
182	 * Before associating a node has no rate set setup
183	 * so we can't calculate any transmit codes to use.
184	 * This is ok since we should never be sending anything
185	 * but management frames and those always go at the
186	 * lowest hardware rate.
187	 */
188	if (ni->ni_rates.rs_nrates == 0)
189		goto done;
190	on->on_rix = rate;
191	ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
192	on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
193	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
194
195	on->on_tx_rate0sp = on->on_tx_rate0 |
196		rt->info[on->on_tx_rix0].shortPreamble;
197	if (sc->sc_mrretry) {
198		/*
199		 * Hardware supports multi-rate retry; setup two
200		 * step-down retry rates and make the lowest rate
201		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
202		 * respectively (4 is set here, the rest are fixed
203		 * in the xmit routine).
204		 */
205		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
206		if (--rate >= 0) {
207			rix = sc->sc_rixmap[
208				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
209			on->on_tx_rate1 = rt->info[rix].rateCode;
210			on->on_tx_rate1sp = on->on_tx_rate1 |
211				rt->info[rix].shortPreamble;
212		} else {
213			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
214		}
215		if (--rate >= 0) {
216			rix = sc->sc_rixmap[
217				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
218			on->on_tx_rate2 = rt->info[rix].rateCode;
219			on->on_tx_rate2sp = on->on_tx_rate2 |
220				rt->info[rix].shortPreamble;
221		} else {
222			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
223		}
224		if (rate > 0) {
225			/* NB: only do this if we didn't already do it above */
226			on->on_tx_rate3 = rt->info[0].rateCode;
227			on->on_tx_rate3sp =
228				on->on_tx_rate3 | rt->info[0].shortPreamble;
229		} else {
230			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
231		}
232	} else {
233		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
234		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
235		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
236		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
237	}
238done:
239	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
240
241	on->on_interval = ath_rateinterval;
242	if (vap->iv_opmode == IEEE80211_M_STA)
243		on->on_interval /= 2;
244	on->on_interval = (on->on_interval * hz) / 1000;
245}
246
247/*
248 * Set the starting transmit rate for a node.
249 */
250static void
251ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
252{
253#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
254	struct ath_node *an = ATH_NODE(ni);
255	const struct ieee80211_txparam *tp = an->an_tp;
256	int srate;
257
258	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
259	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
260		/*
261		 * No fixed rate is requested. For 11b start with
262		 * the highest negotiated rate; otherwise, for 11g
263		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
264		 */
265		srate = ni->ni_rates.rs_nrates - 1;
266		if (sc->sc_curmode != IEEE80211_MODE_11B) {
267			/*
268			 * Scan the negotiated rate set to find the
269			 * closest rate.
270			 */
271			/* NB: the rate set is assumed sorted */
272			for (; srate >= 0 && RATE(srate) > 72; srate--)
273				;
274		}
275	} else {
276		/*
277		 * A fixed rate is to be used; ic_fixed_rate is the
278		 * IEEE code for this rate (sans basic bit).  Convert this
279		 * to the index into the negotiated rate set for
280		 * the node.  We know the rate is there because the
281		 * rate set is checked when the station associates.
282		 */
283		/* NB: the rate set is assumed sorted */
284		srate = ni->ni_rates.rs_nrates - 1;
285		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
286			;
287	}
288	/*
289	 * The selected rate may not be available due to races
290	 * and mode settings.  Also orphaned nodes created in
291	 * adhoc mode may not have any rate set so this lookup
292	 * can fail.  This is not fatal.
293	 */
294	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
295#undef RATE
296}
297
298static void
299ath_rate_cb(void *arg, struct ieee80211_node *ni)
300{
301	struct ath_softc *sc = arg;
302
303	ath_rate_update(sc, ni, 0);
304}
305
306/*
307 * Reset the rate control state for each 802.11 state transition.
308 */
309void
310ath_rate_newstate(struct ieee80211vap *vap, enum ieee80211_state state)
311{
312	struct ieee80211com *ic = vap->iv_ic;
313	struct ath_softc *sc = ic->ic_ifp->if_softc;
314	struct ieee80211_node *ni;
315
316	if (state == IEEE80211_S_INIT)
317		return;
318	if (vap->iv_opmode == IEEE80211_M_STA) {
319		/*
320		 * Reset local xmit state; this is really only
321		 * meaningful when operating in station mode.
322		 */
323		ni = vap->iv_bss;
324		if (state == IEEE80211_S_RUN) {
325			ath_rate_ctl_start(sc, ni);
326		} else {
327			ath_rate_update(sc, ni, 0);
328		}
329	} else {
330		/*
331		 * When operating as a station the node table holds
332		 * the AP's that were discovered during scanning.
333		 * For any other operating mode we want to reset the
334		 * tx rate state of each node.
335		 */
336		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
337		ath_rate_update(sc, vap->iv_bss, 0);
338	}
339}
340
341/*
342 * Examine and potentially adjust the transmit rate.
343 */
344static void
345ath_rate_ctl(void *arg, struct ieee80211_node *ni)
346{
347	struct ath_softc *sc = arg;
348	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
349	struct ieee80211_rateset *rs = &ni->ni_rates;
350	int dir = 0, nrate, enough;
351
352	/*
353	 * Rate control
354	 * XXX: very primitive version.
355	 */
356	enough = (on->on_tx_ok + on->on_tx_err >= 10);
357
358	/* no packet reached -> down */
359	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
360		dir = -1;
361
362	/* all packets needs retry in average -> down */
363	if (enough && on->on_tx_ok < on->on_tx_retr)
364		dir = -1;
365
366	/* no error and less than rate_raise% of packets need retry -> up */
367	if (enough && on->on_tx_err == 0 &&
368	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
369		dir = 1;
370
371	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
372	    "ok %d err %d retr %d upper %d dir %d",
373	    on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
374
375	nrate = on->on_rix;
376	switch (dir) {
377	case 0:
378		if (enough && on->on_tx_upper > 0)
379			on->on_tx_upper--;
380		break;
381	case -1:
382		if (nrate > 0) {
383			nrate--;
384			sc->sc_stats.ast_rate_drop++;
385		}
386		on->on_tx_upper = 0;
387		break;
388	case 1:
389		/* raise rate if we hit rate_raise_threshold */
390		if (++on->on_tx_upper < ath_rate_raise_threshold)
391			break;
392		on->on_tx_upper = 0;
393		if (nrate + 1 < rs->rs_nrates) {
394			nrate++;
395			sc->sc_stats.ast_rate_raise++;
396		}
397		break;
398	}
399
400	if (nrate != on->on_rix) {
401		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
402		    "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
403		    ni->ni_txrate / 2,
404		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
405		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
406		ath_rate_update(sc, ni, nrate);
407	} else if (enough)
408		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
409}
410
411static void
412ath_rate_sysctlattach(struct ath_softc *sc)
413{
414	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
415	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
416
417	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
418		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
419		"rate control: operation interval (ms)");
420	/* XXX bounds check values */
421	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
422		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
423		"rate control: retry threshold to credit rate raise (%%)");
424	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
425		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
426		"rate control: # good periods before raising rate");
427}
428
429struct ath_ratectrl *
430ath_rate_attach(struct ath_softc *sc)
431{
432	struct onoe_softc *osc;
433
434	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
435	if (osc == NULL)
436		return NULL;
437	osc->arc.arc_space = sizeof(struct onoe_node);
438	ath_rate_sysctlattach(sc);
439
440	return &osc->arc;
441}
442
443void
444ath_rate_detach(struct ath_ratectrl *arc)
445{
446	struct onoe_softc *osc = (struct onoe_softc *) arc;
447
448	free(osc, M_DEVBUF);
449}
450
451/*
452 * Module glue.
453 */
454static int
455onoe_modevent(module_t mod, int type, void *unused)
456{
457	switch (type) {
458	case MOD_LOAD:
459		if (bootverbose)
460			printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n");
461		return 0;
462	case MOD_UNLOAD:
463		return 0;
464	}
465	return EINVAL;
466}
467
468static moduledata_t onoe_mod = {
469	"ath_rate",
470	onoe_modevent,
471	0
472};
473DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
474MODULE_VERSION(ath_rate, 1);
475MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
476