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 "opt_wlan.h" 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/socket.h> 37 #include <sys/sysctl.h> 38 39 #include <net/if.h> 40 #include <net/if_media.h> 41 42 #ifdef INET 43 #include <netinet/in.h> 44 #include <netinet/if_ether.h> 45 #endif 46 47 #include <net80211/ieee80211_var.h> 48 #include <net80211/ieee80211_amrr.h> 49 50 #define is_success(amn) \ 51 ((amn)->amn_retrycnt < (amn)->amn_txcnt / 10) 52 #define is_failure(amn) \ 53 ((amn)->amn_retrycnt > (amn)->amn_txcnt / 3) 54 #define is_enough(amn) \ 55 ((amn)->amn_txcnt > 10) 56 57 static void amrr_sysctlattach(struct ieee80211_amrr *amrr, 58 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree); 59 60 /* number of references from net80211 layer */ 61 static int nrefs = 0; 62 63 void 64 ieee80211_amrr_setinterval(struct ieee80211_amrr *amrr, int msecs) 65 { 66 int t; 67 68 if (msecs < 100) 69 msecs = 100; 70 t = msecs_to_ticks(msecs); 71 amrr->amrr_interval = (t < 1) ? 1 : t; 72 } 73 74 void 75 ieee80211_amrr_init(struct ieee80211_amrr *amrr, 76 struct ieee80211vap *vap, int amin, int amax, int interval) 77 { 78 /* XXX bounds check? */ 79 amrr->amrr_min_success_threshold = amin; 80 amrr->amrr_max_success_threshold = amax; 81 ieee80211_amrr_setinterval(amrr, interval); 82 83 amrr_sysctlattach(amrr, vap->iv_sysctl, vap->iv_oid); 84 } 85 86 void 87 ieee80211_amrr_cleanup(struct ieee80211_amrr *amrr) 88 { 89 } 90 91 void 92 ieee80211_amrr_node_init(struct ieee80211_amrr *amrr, 93 struct ieee80211_amrr_node *amn, struct ieee80211_node *ni) 94 { 95 const struct ieee80211_rateset *rs = &ni->ni_rates; 96 97 amn->amn_amrr = amrr; 98 amn->amn_success = 0; 99 amn->amn_recovery = 0; 100 amn->amn_txcnt = amn->amn_retrycnt = 0; 101 amn->amn_success_threshold = amrr->amrr_min_success_threshold; 102 103 /* pick initial rate */ 104 for (amn->amn_rix = rs->rs_nrates - 1; 105 amn->amn_rix > 0 && (rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL) > 72; 106 amn->amn_rix--) 107 ; 108 ni->ni_txrate = rs->rs_rates[amn->amn_rix] & IEEE80211_RATE_VAL; 109 amn->amn_ticks = ticks; 110 111 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 112 "AMRR initial rate %d", ni->ni_txrate); 113 } 114 115 static int 116 amrr_update(struct ieee80211_amrr *amrr, struct ieee80211_amrr_node *amn, 117 struct ieee80211_node *ni) 118 { 119 int rix = amn->amn_rix; 120 121 KASSERT(is_enough(amn), ("txcnt %d", amn->amn_txcnt)); 122 123 if (is_success(amn)) { 124 amn->amn_success++; 125 if (amn->amn_success >= amn->amn_success_threshold && 126 rix + 1 < ni->ni_rates.rs_nrates) { 127 amn->amn_recovery = 1; 128 amn->amn_success = 0; 129 rix++; 130 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 131 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)", 132 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL, 133 amn->amn_txcnt, amn->amn_retrycnt); 134 } else { 135 amn->amn_recovery = 0; 136 } 137 } else if (is_failure(amn)) { 138 amn->amn_success = 0; 139 if (rix > 0) { 140 if (amn->amn_recovery) { 141 amn->amn_success_threshold *= 2; 142 if (amn->amn_success_threshold > 143 amrr->amrr_max_success_threshold) 144 amn->amn_success_threshold = 145 amrr->amrr_max_success_threshold; 146 } else { 147 amn->amn_success_threshold = 148 amrr->amrr_min_success_threshold; 149 } 150 rix--; 151 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 152 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)", 153 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL, 154 amn->amn_txcnt, amn->amn_retrycnt); 155 } 156 amn->amn_recovery = 0; 157 } 158 159 /* reset counters */ 160 amn->amn_txcnt = 0; 161 amn->amn_retrycnt = 0; 162 163 return rix; 164 } 165 166 /* 167 * Return the rate index to use in sending a data frame. 168 * Update our internal state if it's been long enough. 169 * If the rate changes we also update ni_txrate to match. 170 */ 171 int 172 ieee80211_amrr_choose(struct ieee80211_node *ni, 173 struct ieee80211_amrr_node *amn) 174 { 175 struct ieee80211_amrr *amrr = amn->amn_amrr; 176 int rix; 177 178 if (is_enough(amn) && (ticks - amn->amn_ticks) > amrr->amrr_interval) { 179 rix = amrr_update(amrr, amn, ni); 180 if (rix != amn->amn_rix) { 181 /* update public rate */ 182 ni->ni_txrate = 183 ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL; 184 amn->amn_rix = rix; 185 } 186 amn->amn_ticks = ticks; 187 } else 188 rix = amn->amn_rix; 189 return rix; 190 } 191 192 static int 193 amrr_sysctl_interval(SYSCTL_HANDLER_ARGS) 194 { 195 struct ieee80211_amrr *amrr = arg1; 196 int msecs = ticks_to_msecs(amrr->amrr_interval); 197 int error; 198 199 error = sysctl_handle_int(oidp, &msecs, 0, req); 200 if (error || !req->newptr) 201 return error; 202 ieee80211_amrr_setinterval(amrr, msecs); 203 return 0; 204 } 205 206 static void 207 amrr_sysctlattach(struct ieee80211_amrr *amrr, 208 struct sysctl_ctx_list *ctx, struct sysctl_oid *tree) 209 { 210 211 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 212 "amrr_rate_interval", CTLTYPE_INT | CTLFLAG_RW, amrr, 213 0, amrr_sysctl_interval, "I", "amrr operation interval (ms)"); 214 /* XXX bounds check values */ 215 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 216 "amrr_max_sucess_threshold", CTLFLAG_RW, 217 &amrr->amrr_max_success_threshold, 0, ""); 218 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 219 "amrr_min_sucess_threshold", CTLFLAG_RW, 220 &amrr->amrr_min_success_threshold, 0, ""); 221 } 222 223 /* 224 * Module glue. 225 */ 226 IEEE80211_RATE_MODULE(amrr, 1); 227