00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "amrr-wifi-manager.h"
00022 #include "ns3/simulator.h"
00023 #include "ns3/log.h"
00024 #include "ns3/uinteger.h"
00025 #include "ns3/double.h"
00026
00027 NS_LOG_COMPONENT_DEFINE ("AmrrWifiRemoteStation");
00028
00029 namespace ns3 {
00030
00031 NS_OBJECT_ENSURE_REGISTERED (AmrrWifiManager);
00032
00033 TypeId
00034 AmrrWifiManager::GetTypeId (void)
00035 {
00036 static TypeId tid = TypeId ("ns3::AmrrWifiManager")
00037 .SetParent<WifiRemoteStationManager> ()
00038 .AddConstructor<AmrrWifiManager> ()
00039 .AddAttribute ("UpdatePeriod",
00040 "The interval between decisions about rate control changes",
00041 TimeValue (Seconds (1.0)),
00042 MakeTimeAccessor (&AmrrWifiManager::m_updatePeriod),
00043 MakeTimeChecker ())
00044 .AddAttribute ("FailureRatio",
00045 "Ratio of minimum erronous transmissions needed to switch to a lower rate",
00046 DoubleValue (1.0/3.0),
00047 MakeDoubleAccessor (&AmrrWifiManager::m_failureRatio),
00048 MakeDoubleChecker<double> (0.0, 1.0))
00049 .AddAttribute ("SuccessRatio",
00050 "Ratio of maximum erronous transmissions needed to switch to a higher rate",
00051 DoubleValue (1.0/10.0),
00052 MakeDoubleAccessor (&AmrrWifiManager::m_successRatio),
00053 MakeDoubleChecker<double> (0.0, 1.0))
00054 .AddAttribute ("MaxSuccessThreshold",
00055 "Maximum number of consecutive success periods needed to switch to a higher rate",
00056 UintegerValue (10),
00057 MakeUintegerAccessor (&AmrrWifiManager::m_maxSuccessThreshold),
00058 MakeUintegerChecker<uint32_t> ())
00059 .AddAttribute ("MinSuccessThreshold",
00060 "Minimum number of consecutive success periods needed to switch to a higher rate",
00061 UintegerValue (1),
00062 MakeUintegerAccessor (&AmrrWifiManager::m_minSuccessThreshold),
00063 MakeUintegerChecker<uint32_t> ())
00064 ;
00065 return tid;
00066 }
00067
00068 AmrrWifiManager::AmrrWifiManager ()
00069 {}
00070 WifiRemoteStation *
00071 AmrrWifiManager::CreateStation (void)
00072 {
00073 return new AmrrWifiRemoteStation (this);
00074 }
00075
00076 AmrrWifiRemoteStation::AmrrWifiRemoteStation (Ptr<AmrrWifiManager> stations)
00077 : m_stations (stations),
00078 m_nextModeUpdate (Simulator::Now () + stations->m_updatePeriod),
00079 m_tx_ok (0),
00080 m_tx_err (0),
00081 m_tx_retr (0),
00082 m_retry (0),
00083 m_txrate (0),
00084 m_successThreshold (m_stations->m_minSuccessThreshold),
00085 m_success (0),
00086 m_recovery (false)
00087 {}
00088 AmrrWifiRemoteStation::~AmrrWifiRemoteStation ()
00089 {}
00090
00091 void
00092 AmrrWifiRemoteStation::DoReportRxOk (double rxSnr, WifiMode txMode)
00093 {}
00094 void
00095 AmrrWifiRemoteStation::DoReportRtsFailed (void)
00096 {}
00097 void
00098 AmrrWifiRemoteStation::DoReportDataFailed (void)
00099 {
00100 m_retry++;
00101 m_tx_retr++;
00102 }
00103 void
00104 AmrrWifiRemoteStation::DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr)
00105 {}
00106 void
00107 AmrrWifiRemoteStation::DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr)
00108 {
00109 m_retry = 0;
00110 m_tx_ok++;
00111 }
00112 void
00113 AmrrWifiRemoteStation::DoReportFinalRtsFailed (void)
00114 {}
00115 void
00116 AmrrWifiRemoteStation::DoReportFinalDataFailed (void)
00117 {
00118 m_retry = 0;
00119 m_tx_err++;
00120 }
00121 bool
00122 AmrrWifiRemoteStation::IsMinRate (void) const
00123 {
00124 return (m_txrate == 0);
00125 }
00126 bool
00127 AmrrWifiRemoteStation::IsMaxRate (void) const
00128 {
00129 NS_ASSERT (m_txrate + 1 <= GetNSupportedModes ());
00130 return (m_txrate + 1 == GetNSupportedModes ());
00131 }
00132 bool
00133 AmrrWifiRemoteStation::IsSuccess (void) const
00134 {
00135 return (m_tx_retr + m_tx_err) < m_tx_ok * m_stations->m_successRatio;
00136 }
00137 bool
00138 AmrrWifiRemoteStation::IsFailure (void) const
00139 {
00140 return (m_tx_retr + m_tx_err) > m_tx_ok * m_stations->m_failureRatio;
00141 }
00142 bool
00143 AmrrWifiRemoteStation::IsEnough (void) const
00144 {
00145 return (m_tx_retr + m_tx_err + m_tx_ok) > 10;
00146 }
00147 void
00148 AmrrWifiRemoteStation::ResetCnt (void)
00149 {
00150 m_tx_ok = 0;
00151 m_tx_err = 0;
00152 m_tx_retr = 0;
00153 }
00154 void
00155 AmrrWifiRemoteStation::IncreaseRate (void)
00156 {
00157 m_txrate++;
00158 NS_ASSERT (m_txrate < GetNSupportedModes ());
00159 }
00160 void
00161 AmrrWifiRemoteStation::DecreaseRate (void)
00162 {
00163 m_txrate--;
00164 }
00165
00166 void
00167 AmrrWifiRemoteStation::UpdateMode (void)
00168 {
00169 if (Simulator::Now () < m_nextModeUpdate)
00170 {
00171 return;
00172 }
00173 m_nextModeUpdate = Simulator::Now () + m_stations->m_updatePeriod;
00174 NS_LOG_DEBUG ("Update");
00175
00176 bool needChange = false;
00177
00178 if (IsSuccess () && IsEnough ())
00179 {
00180 m_success++;
00181 NS_LOG_DEBUG ("++ success="<<m_success<<" successThreshold="<<m_successThreshold<<
00182 " tx_ok="<<m_tx_ok<<" tx_err="<<m_tx_err<<" tx_retr="<<m_tx_retr<<
00183 " rate="<<m_txrate<<" n-supported-rates="<<GetNSupportedModes ());
00184 if (m_success >= m_successThreshold &&
00185 !IsMaxRate ())
00186 {
00187 m_recovery = true;
00188 m_success = 0;
00189 IncreaseRate ();
00190 needChange = true;
00191 }
00192 else
00193 {
00194 m_recovery = false;
00195 }
00196 }
00197 else if (IsFailure ())
00198 {
00199 m_success = 0;
00200 NS_LOG_DEBUG ("-- success="<<m_success<<" successThreshold="<<m_successThreshold<<
00201 " tx_ok="<<m_tx_ok<<" tx_err="<<m_tx_err<<" tx_retr="<<m_tx_retr<<
00202 " rate="<<m_txrate<<" n-supported-rates="<<GetNSupportedModes ());
00203 if (!IsMinRate ())
00204 {
00205 if (m_recovery)
00206 {
00207 m_successThreshold *= 2;
00208 m_successThreshold = std::min (m_successThreshold,
00209 m_stations->m_maxSuccessThreshold);
00210 }
00211 else
00212 {
00213 m_successThreshold = m_stations->m_minSuccessThreshold;
00214 }
00215 m_recovery = false;
00216 DecreaseRate ();
00217 needChange = true;
00218 }
00219 else
00220 {
00221 m_recovery = false;
00222 }
00223 }
00224 if (IsEnough () || needChange)
00225 {
00226 NS_LOG_DEBUG ("Reset");
00227 ResetCnt ();
00228 }
00229 }
00230
00231 Ptr<WifiRemoteStationManager>
00232 AmrrWifiRemoteStation::GetManager (void) const
00233 {
00234 return m_stations;
00235 }
00236 WifiMode
00237 AmrrWifiRemoteStation::DoGetDataMode (uint32_t size)
00238 {
00239 UpdateMode ();
00240 NS_ASSERT (m_txrate < GetNSupportedModes ());
00241 uint32_t rateIndex;
00242 if (m_retry < 1)
00243 {
00244 rateIndex = m_txrate;
00245 }
00246 else if (m_retry < 2)
00247 {
00248 if (m_txrate > 0)
00249 {
00250 rateIndex = m_txrate - 1;
00251 }
00252 else
00253 {
00254 rateIndex = m_txrate;
00255 }
00256 }
00257 else if (m_retry < 3)
00258 {
00259 if (m_txrate > 1)
00260 {
00261 rateIndex = m_txrate - 2;
00262 }
00263 else
00264 {
00265 rateIndex = m_txrate;
00266 }
00267 }
00268 else
00269 {
00270 if (m_txrate > 2)
00271 {
00272 rateIndex = m_txrate - 3;
00273 }
00274 else
00275 {
00276 rateIndex = m_txrate;
00277 }
00278 }
00279
00280 return GetSupportedMode (rateIndex);
00281 }
00282 WifiMode
00283 AmrrWifiRemoteStation::DoGetRtsMode (void)
00284 {
00285 UpdateMode ();
00286
00287 return GetSupportedMode (0);
00288 }
00289
00290 }