xref: /freebsd/sys/net80211/ieee80211_amrr.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*	$OpenBSD: ieee80211_amrr.c,v 1.1 2006/06/17 19:07:19 damien Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 /*-
24  * Naive implementation of the Adaptive Multi Rate Retry algorithm:
25  *
26  * "IEEE 802.11 Rate Adaptation: A Practical Approach"
27  *  Mathieu Lacage, Hossein Manshaei, Thierry Turletti
28  *  INRIA Sophia - Projet Planete
29  *  http://www-sop.inria.fr/rapports/sophia/RR-5208.html
30  */
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 
37 #include <net/if.h>
38 #include <net/if_media.h>
39 
40 #ifdef INET
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 #endif
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_amrr.h>
47 
48 #define is_success(amn)	\
49 	((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
50 #define is_failure(amn)	\
51 	((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
52 #define is_enough(amn)		\
53 	((amn)->amn_txcnt > 10)
54 #define is_min_rate(ni)		\
55 	((ni)->ni_txrate == 0)
56 #define is_max_rate(ni)		\
57 	((ni)->ni_txrate == (ni)->ni_rates.rs_nrates - 1)
58 #define increase_rate(ni)	\
59 	((ni)->ni_txrate++)
60 #define decrease_rate(ni)	\
61 	((ni)->ni_txrate--)
62 #define reset_cnt(amn)		\
63 	do { (amn)->amn_txcnt = (amn)->amn_retrycnt = 0; } while (0)
64 
65 void
66 ieee80211_amrr_init(struct ieee80211_amrr *amrr,
67     struct ieee80211com *ic, int amin, int amax)
68 {
69 	/* XXX bounds check? */
70 	amrr->amrr_min_success_threshold = amin;
71 	amrr->amrr_max_success_threshold = amax;
72 	amrr->amrr_ic = ic;
73 }
74 
75 void
76 ieee80211_amrr_node_init(struct ieee80211_amrr *amrr,
77     struct ieee80211_amrr_node *amn)
78 {
79 	amn->amn_success = 0;
80 	amn->amn_recovery = 0;
81 	amn->amn_txcnt = amn->amn_retrycnt = 0;
82 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
83 }
84 
85 /*
86  * Update ni->ni_txrate.
87  */
88 void
89 ieee80211_amrr_choose(struct ieee80211_amrr *amrr, struct ieee80211_node *ni,
90     struct ieee80211_amrr_node *amn)
91 {
92 	int need_change = 0;
93 
94 	if (is_success(amn) && is_enough(amn)) {
95 		amn->amn_success++;
96 		if (amn->amn_success >= amn->amn_success_threshold &&
97 		    !is_max_rate(ni)) {
98 			amn->amn_recovery = 1;
99 			amn->amn_success = 0;
100 			increase_rate(ni);
101 			IEEE80211_DPRINTF(amrr->amrr_ic, IEEE80211_MSG_RATECTL,
102 			    "AMRR increasing rate %d (txcnt=%d "
103 			    "retrycnt=%d)\n",
104 			    ni->ni_rates.rs_rates[ni->ni_txrate] &
105 				IEEE80211_RATE_VAL,
106 			    amn->amn_txcnt, amn->amn_retrycnt);
107 			need_change = 1;
108 		} else {
109 			amn->amn_recovery = 0;
110 		}
111 	} else if (is_failure(amn)) {
112 		amn->amn_success = 0;
113 		if (!is_min_rate(ni)) {
114 			if (amn->amn_recovery) {
115 				amn->amn_success_threshold *= 2;
116 				if (amn->amn_success_threshold >
117 				    amrr->amrr_max_success_threshold)
118 					amn->amn_success_threshold =
119 					    amrr->amrr_max_success_threshold;
120 			} else {
121 				amn->amn_success_threshold =
122 				    amrr->amrr_min_success_threshold;
123 			}
124 			decrease_rate(ni);
125 			IEEE80211_DPRINTF(amrr->amrr_ic, IEEE80211_MSG_RATECTL,
126 			    "AMRR decreasing rate %d (txcnt=%d "
127 			    "retrycnt=%d)\n",
128 			    ni->ni_rates.rs_rates[ni->ni_txrate] &
129 				IEEE80211_RATE_VAL,
130 			    amn->amn_txcnt, amn->amn_retrycnt);
131 			need_change = 1;
132 		}
133 		amn->amn_recovery = 0;
134 	}
135 
136 	if (is_enough(amn) || need_change)
137 		reset_cnt(amn);
138 }
139 
140 /*
141  * Module glue.
142  */
143 static int
144 amrr_modevent(module_t mod, int type, void *unused)
145 {
146 	switch (type) {
147 	case MOD_LOAD:
148 		if (bootverbose)
149 			printf("wlan_amrr: <AMRR Transmit Rate Control Algorithm>\n");
150 		return 0;
151 	case MOD_UNLOAD:
152 		return 0;
153 	}
154 	return EINVAL;
155 }
156 
157 static moduledata_t amrr_mod = {
158 	"wlan_amrr",
159 	amrr_modevent,
160 	0
161 };
162 DECLARE_MODULE(wlan_amrr, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
163 MODULE_VERSION(wlan_amrr, 1);
164 MODULE_DEPEND(wlan_amrr, wlan, 1, 1, 1);
165