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