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