xref: /freebsd/sys/net80211/ieee80211_amrr.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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.h>
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_amrr.h>
48 
49 #define is_success(amn)	\
50 	((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
51 #define is_failure(amn)	\
52 	((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
53 #define is_enough(amn)		\
54 	((amn)->amn_txcnt > 10)
55 #define is_min_rate(ni)		\
56 	((ni)->ni_txrate == 0)
57 #define is_max_rate(ni)		\
58 	((ni)->ni_txrate == (ni)->ni_rates.rs_nrates - 1)
59 #define increase_rate(ni)	\
60 	((ni)->ni_txrate++)
61 #define decrease_rate(ni)	\
62 	((ni)->ni_txrate--)
63 #define reset_cnt(amn)		\
64 	do { (amn)->amn_txcnt = (amn)->amn_retrycnt = 0; } while (0)
65 
66 void
67 ieee80211_amrr_init(struct ieee80211_amrr *amrr,
68     struct ieee80211com *ic, int min, int max)
69 {
70 	/* XXX bounds check? */
71 	amrr->amrr_min_success_threshold = min;
72 	amrr->amrr_max_success_threshold = max;
73 	amrr->amrr_ic = ic;
74 }
75 
76 void
77 ieee80211_amrr_node_init(struct ieee80211_amrr *amrr,
78     struct ieee80211_amrr_node *amn)
79 {
80 	amn->amn_success = 0;
81 	amn->amn_recovery = 0;
82 	amn->amn_txcnt = amn->amn_retrycnt = 0;
83 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
84 }
85 
86 /*
87  * Update ni->ni_txrate.
88  */
89 void
90 ieee80211_amrr_choose(struct ieee80211_amrr *amrr, struct ieee80211_node *ni,
91     struct ieee80211_amrr_node *amn)
92 {
93 	int need_change = 0;
94 
95 	if (is_success(amn) && is_enough(amn)) {
96 		amn->amn_success++;
97 		if (amn->amn_success >= amn->amn_success_threshold &&
98 		    !is_max_rate(ni)) {
99 			amn->amn_recovery = 1;
100 			amn->amn_success = 0;
101 			increase_rate(ni);
102 			IEEE80211_DPRINTF(amrr->amrr_ic, IEEE80211_MSG_RATECTL,
103 			    "AMRR increasing rate %d (txcnt=%d "
104 			    "retrycnt=%d)\n",
105 			    ni->ni_rates.rs_rates[ni->ni_txrate] &
106 				IEEE80211_RATE_VAL,
107 			    amn->amn_txcnt, amn->amn_retrycnt);
108 			need_change = 1;
109 		} else {
110 			amn->amn_recovery = 0;
111 		}
112 	} else if (is_failure(amn)) {
113 		amn->amn_success = 0;
114 		if (!is_min_rate(ni)) {
115 			if (amn->amn_recovery) {
116 				amn->amn_success_threshold *= 2;
117 				if (amn->amn_success_threshold >
118 				    amrr->amrr_max_success_threshold)
119 					amn->amn_success_threshold =
120 					    amrr->amrr_max_success_threshold;
121 			} else {
122 				amn->amn_success_threshold =
123 				    amrr->amrr_min_success_threshold;
124 			}
125 			decrease_rate(ni);
126 			IEEE80211_DPRINTF(amrr->amrr_ic, IEEE80211_MSG_RATECTL,
127 			    "AMRR decreasing rate %d (txcnt=%d "
128 			    "retrycnt=%d)\n",
129 			    ni->ni_rates.rs_rates[ni->ni_txrate] &
130 				IEEE80211_RATE_VAL,
131 			    amn->amn_txcnt, amn->amn_retrycnt);
132 			need_change = 1;
133 		}
134 		amn->amn_recovery = 0;
135 	}
136 
137 	if (is_enough(amn) || need_change)
138 		reset_cnt(amn);
139 }
140 
141 /*
142  * Module glue.
143  */
144 static int
145 amrr_modevent(module_t mod, int type, void *unused)
146 {
147 	switch (type) {
148 	case MOD_LOAD:
149 		if (bootverbose)
150 			printf("wlan_amrr: <AMRR Transmit Rate Control Algorithm>\n");
151 		return 0;
152 	case MOD_UNLOAD:
153 		return 0;
154 	}
155 	return EINVAL;
156 }
157 
158 static moduledata_t amrr_mod = {
159 	"wlan_amrr",
160 	amrr_modevent,
161 	0
162 };
163 DECLARE_MODULE(wlan_amrr, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
164 MODULE_VERSION(wlan_amrr, 1);
165