A Discrete-Event Network Simulator
API
rraa-wifi-manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004,2005,2006 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Federico Maguolo <maguolof@dei.unipd.it>
18  */
19 
20 #include "rraa-wifi-manager.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/packet.h"
24 #include "ns3/simulator.h"
25 #include "ns3/wifi-mac.h"
26 #include "ns3/wifi-phy.h"
27 
28 #define Min(a, b) ((a < b) ? a : b)
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("RraaWifiManager");
34 
42 {
43  uint32_t m_counter;
44  uint32_t m_nFailed;
45  uint32_t m_adaptiveRtsWnd;
46  uint32_t m_rtsCounter;
51  uint8_t m_nRate;
52  uint8_t m_rateIndex;
53 
55 };
56 
58 
59 TypeId
61 {
62  static TypeId tid =
63  TypeId("ns3::RraaWifiManager")
65  .SetGroupName("Wifi")
66  .AddConstructor<RraaWifiManager>()
67  .AddAttribute(
68  "Basic",
69  "If true the RRAA-BASIC algorithm will be used, otherwise the RRAA will be used",
70  BooleanValue(false),
73  .AddAttribute("Timeout",
74  "Timeout for the RRAA BASIC loss estimation block",
75  TimeValue(Seconds(0.05)),
78  .AddAttribute("FrameLength",
79  "The Data frame length (in bytes) used for calculating mode TxTime.",
80  UintegerValue(1420),
82  MakeUintegerChecker<uint32_t>())
83  .AddAttribute("AckFrameLength",
84  "The Ack frame length (in bytes) used for calculating mode TxTime.",
85  UintegerValue(14),
87  MakeUintegerChecker<uint32_t>())
88  .AddAttribute("Alpha",
89  "Constant for calculating the MTL threshold.",
90  DoubleValue(1.25),
92  MakeDoubleChecker<double>(1))
93  .AddAttribute("Beta",
94  "Constant for calculating the ORI threshold.",
95  DoubleValue(2),
97  MakeDoubleChecker<double>(1))
98  .AddAttribute("Tau",
99  "Constant for calculating the EWND size.",
100  DoubleValue(0.012),
102  MakeDoubleChecker<double>(0))
103  .AddTraceSource("Rate",
104  "Traced value for rate changes (b/s)",
106  "ns3::TracedValueCallback::Uint64");
107  return tid;
108 }
109 
112  m_currentRate(0)
113 {
114  NS_LOG_FUNCTION(this);
115 }
116 
118 {
119  NS_LOG_FUNCTION(this);
120 }
121 
122 void
124 {
125  NS_LOG_FUNCTION(this << phy);
126  m_sifs = phy->GetSifs();
127  m_difs = m_sifs + 2 * phy->GetSlot();
128  for (const auto& mode : phy->GetModeList())
129  {
130  WifiTxVector txVector;
131  txVector.SetMode(mode);
133  /* Calculate the TX Time of the Data and the corresponding Ack */
134  Time dataTxTime = phy->CalculateTxDuration(m_frameLength, txVector, phy->GetPhyBand());
135  Time ackTxTime = phy->CalculateTxDuration(m_ackLength, txVector, phy->GetPhyBand());
136  NS_LOG_DEBUG("Calculating TX times: Mode= " << mode << " DataTxTime= " << dataTxTime
137  << " AckTxTime= " << ackTxTime);
138  AddCalcTxTime(mode, dataTxTime + ackTxTime);
139  }
141 }
142 
143 void
145 {
146  NS_LOG_FUNCTION(this);
148 }
149 
150 void
152 {
153  NS_LOG_FUNCTION(this);
154  if (GetHtSupported())
155  {
156  NS_FATAL_ERROR("WifiRemoteStationManager selected does not support HT rates");
157  }
158  if (GetVhtSupported())
159  {
160  NS_FATAL_ERROR("WifiRemoteStationManager selected does not support VHT rates");
161  }
162  if (GetHeSupported())
163  {
164  NS_FATAL_ERROR("WifiRemoteStationManager selected does not support HE rates");
165  }
166 }
167 
168 Time
170 {
171  NS_LOG_FUNCTION(this << mode);
172  for (auto i = m_calcTxTime.begin(); i != m_calcTxTime.end(); i++)
173  {
174  if (mode == i->second)
175  {
176  return i->first;
177  }
178  }
179  NS_ASSERT(false);
180  return Seconds(0);
181 }
182 
183 void
185 {
186  NS_LOG_FUNCTION(this << mode << t);
187  m_calcTxTime.emplace_back(t, mode);
188 }
189 
192 {
193  NS_LOG_FUNCTION(this << station << mode);
194  WifiRraaThresholds threshold;
195  for (auto i = station->m_thresholds.begin(); i != station->m_thresholds.end(); i++)
196  {
197  if (mode == i->second)
198  {
199  return i->first;
200  }
201  }
202  NS_ABORT_MSG("No thresholds for mode " << mode << " found");
203  return threshold; // Silence compiler warning
204 }
205 
208 {
209  auto station = new RraaWifiRemoteStation();
210  station->m_initialized = false;
211  station->m_adaptiveRtsWnd = 0;
212  station->m_rtsCounter = 0;
213  station->m_adaptiveRtsOn = false;
214  station->m_lastFrameFail = false;
215  return station;
216 }
217 
218 void
220 {
221  NS_LOG_FUNCTION(this << station);
222  if (!station->m_initialized)
223  {
224  // Note: we appear to be doing late initialization of the table
225  // to make sure that the set of supported rates has been initialized
226  // before we perform our own initialization.
227  station->m_nRate = GetNSupported(station);
228  // Initialize at maximal rate
229  station->m_rateIndex = GetMaxRate(station);
230 
231  station->m_initialized = true;
232 
233  station->m_thresholds = RraaThresholdsTable(station->m_nRate);
234  InitThresholds(station);
235  ResetCountersBasic(station);
236  }
237 }
238 
239 void
241 {
242  NS_LOG_FUNCTION(this << station);
243  NS_LOG_DEBUG("InitThresholds = " << station);
244 
245  double nextCritical = 0;
246  double nextMtl = 0;
247  double mtl = 0;
248  double ori = 0;
249  for (uint8_t i = 0; i < station->m_nRate; i++)
250  {
251  WifiMode mode = GetSupported(station, i);
252  Time totalTxTime = GetCalcTxTime(mode) + m_sifs + m_difs;
253  if (i == GetMaxRate(station))
254  {
255  ori = 0;
256  }
257  else
258  {
259  WifiMode nextMode = GetSupported(station, i + 1);
260  Time nextTotalTxTime = GetCalcTxTime(nextMode) + m_sifs + m_difs;
261  nextCritical = 1 - (nextTotalTxTime.GetSeconds() / totalTxTime.GetSeconds());
262  nextMtl = m_alpha * nextCritical;
263  ori = nextMtl / m_beta;
264  }
265  if (i == 0)
266  {
267  mtl = 1;
268  }
270  th.m_ewnd = static_cast<uint32_t>(ceil(m_tau / totalTxTime.GetSeconds()));
271  th.m_ori = ori;
272  th.m_mtl = mtl;
273  station->m_thresholds.emplace_back(th, mode);
274  mtl = nextMtl;
275  NS_LOG_DEBUG(mode << " " << th.m_ewnd << " " << th.m_mtl << " " << th.m_ori);
276  }
277 }
278 
279 void
281 {
282  NS_LOG_FUNCTION(this << station);
283  station->m_nFailed = 0;
284  station->m_counter = GetThresholds(station, station->m_rateIndex).m_ewnd;
285  station->m_lastReset = Simulator::Now();
286 }
287 
288 uint8_t
290 {
291  return station->m_nRate - 1;
292 }
293 
294 void
296 {
297  NS_LOG_FUNCTION(this << st);
298 }
299 
300 void
302 {
303  NS_LOG_FUNCTION(this << st);
304  auto station = static_cast<RraaWifiRemoteStation*>(st);
305  station->m_lastFrameFail = true;
306  CheckTimeout(station);
307  station->m_counter--;
308  station->m_nFailed++;
309  RunBasicAlgorithm(station);
310 }
311 
312 void
314 {
315  NS_LOG_FUNCTION(this << st << rxSnr << txMode);
316 }
317 
318 void
320  double ctsSnr,
321  WifiMode ctsMode,
322  double rtsSnr)
323 {
324  NS_LOG_FUNCTION(this << st << ctsSnr << ctsMode << rtsSnr);
325 }
326 
327 void
329  double ackSnr,
330  WifiMode ackMode,
331  double dataSnr,
332  uint16_t dataChannelWidth,
333  uint8_t dataNss)
334 {
335  NS_LOG_FUNCTION(this << st << ackSnr << ackMode << dataSnr << dataChannelWidth << +dataNss);
336  auto station = static_cast<RraaWifiRemoteStation*>(st);
337  station->m_lastFrameFail = false;
338  CheckTimeout(station);
339  station->m_counter--;
340  RunBasicAlgorithm(station);
341 }
342 
343 void
345 {
346  NS_LOG_FUNCTION(this << st);
347 }
348 
349 void
351 {
352  NS_LOG_FUNCTION(this << st);
353 }
354 
357 {
358  NS_LOG_FUNCTION(this << st << allowedWidth);
359  auto station = static_cast<RraaWifiRemoteStation*>(st);
360  uint16_t channelWidth = GetChannelWidth(station);
361  if (channelWidth > 20 && channelWidth != 22)
362  {
363  channelWidth = 20;
364  }
365  CheckInit(station);
366  WifiMode mode = GetSupported(station, station->m_rateIndex);
367  uint64_t rate = mode.GetDataRate(channelWidth);
368  if (m_currentRate != rate)
369  {
370  NS_LOG_DEBUG("New datarate: " << rate);
371  m_currentRate = rate;
372  }
373  return WifiTxVector(
374  mode,
377  800,
378  1,
379  1,
380  0,
381  channelWidth,
382  GetAggregation(station));
383 }
384 
387 {
388  NS_LOG_FUNCTION(this << st);
389  auto station = static_cast<RraaWifiRemoteStation*>(st);
390  uint16_t channelWidth = GetChannelWidth(station);
391  if (channelWidth > 20 && channelWidth != 22)
392  {
393  channelWidth = 20;
394  }
395  WifiMode mode;
396  if (!GetUseNonErpProtection())
397  {
398  mode = GetSupported(station, 0);
399  }
400  else
401  {
402  mode = GetNonErpSupported(station, 0);
403  }
404  return WifiTxVector(
405  mode,
408  800,
409  1,
410  1,
411  0,
412  channelWidth,
413  GetAggregation(station));
414 }
415 
416 bool
417 RraaWifiManager::DoNeedRts(WifiRemoteStation* st, uint32_t size, bool normally)
418 {
419  NS_LOG_FUNCTION(this << st << size << normally);
420  auto station = static_cast<RraaWifiRemoteStation*>(st);
421  CheckInit(station);
422  if (m_basic)
423  {
424  return normally;
425  }
426  ARts(station);
427  return station->m_adaptiveRtsOn;
428 }
429 
430 void
432 {
433  NS_LOG_FUNCTION(this << station);
434  Time d = Simulator::Now() - station->m_lastReset;
435  if (station->m_counter == 0 || d > m_timeout)
436  {
437  ResetCountersBasic(station);
438  }
439 }
440 
441 void
443 {
444  NS_LOG_FUNCTION(this << station);
445  WifiRraaThresholds thresholds = GetThresholds(station, station->m_rateIndex);
446  auto ploss = (static_cast<double>(station->m_nFailed) / thresholds.m_ewnd);
447  if (station->m_counter == 0 || ploss > thresholds.m_mtl)
448  {
449  if (ploss > thresholds.m_mtl)
450  {
451  station->m_rateIndex--;
452  }
453  else if (station->m_rateIndex < GetMaxRate(station) && ploss < thresholds.m_ori)
454  {
455  station->m_rateIndex++;
456  }
457  ResetCountersBasic(station);
458  }
459 }
460 
461 void
463 {
464  if (!station->m_adaptiveRtsOn && station->m_lastFrameFail)
465  {
466  station->m_adaptiveRtsWnd++;
467  station->m_rtsCounter = station->m_adaptiveRtsWnd;
468  }
469  else if ((station->m_adaptiveRtsOn && station->m_lastFrameFail) ||
470  (!station->m_adaptiveRtsOn && !station->m_lastFrameFail))
471  {
472  station->m_adaptiveRtsWnd = station->m_adaptiveRtsWnd / 2;
473  station->m_rtsCounter = station->m_adaptiveRtsWnd;
474  }
475  if (station->m_rtsCounter > 0)
476  {
477  station->m_adaptiveRtsOn = true;
478  station->m_rtsCounter--;
479  }
480  else
481  {
482  station->m_adaptiveRtsOn = false;
483  }
484 }
485 
488 {
489  NS_LOG_FUNCTION(this << station << +index);
490  WifiMode mode = GetSupported(station, index);
491  return GetThresholds(station, mode);
492 }
493 
494 } // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Robust Rate Adaptation Algorithm.
WifiRemoteStation * DoCreateStation() const override
double m_tau
Tau value for RRAA (value for calculating EWND size).
WifiTxVector DoGetRtsTxVector(WifiRemoteStation *station) override
void ARts(RraaWifiRemoteStation *station)
Activate the use of RTS for the given station if the conditions are met.
void CheckInit(RraaWifiRemoteStation *station)
Check for initializations.
void DoReportDataFailed(WifiRemoteStation *station) override
This method is a pure virtual method that must be implemented by the sub-class.
void SetupPhy(const Ptr< WifiPhy > phy) override
Set up PHY associated with this device since it is the object that knows the full set of transmit rat...
TracedValue< uint64_t > m_currentRate
Trace rate changes.
static TypeId GetTypeId()
Get the type ID.
uint32_t m_frameLength
Data frame length used to calculate mode TxTime.
bool DoNeedRts(WifiRemoteStation *st, uint32_t size, bool normally) override
Time m_difs
Value of DIFS configured in the device.
void AddCalcTxTime(WifiMode mode, Time t)
Add transmission time for the given mode to an internal list.
void DoReportFinalRtsFailed(WifiRemoteStation *station) override
This method is a pure virtual method that must be implemented by the sub-class.
void SetupMac(const Ptr< WifiMac > mac) override
Set up MAC associated with this device since it is the object that knows the full set of timing param...
WifiTxVector DoGetDataTxVector(WifiRemoteStation *station, uint16_t allowedWidth) override
void RunBasicAlgorithm(RraaWifiRemoteStation *station)
Find an appropriate rate for the given station, using a basic algorithm.
Time GetCalcTxTime(WifiMode mode) const
Get the estimated TxTime of a packet with a given mode.
void ResetCountersBasic(RraaWifiRemoteStation *station)
Reset the counters of the given station.
TxTime m_calcTxTime
To hold all the calculated TxTime for all modes.
void DoReportRtsFailed(WifiRemoteStation *station) override
This method is a pure virtual method that must be implemented by the sub-class.
void DoReportRxOk(WifiRemoteStation *station, double rxSnr, WifiMode txMode) override
This method is a pure virtual method that must be implemented by the sub-class.
double m_beta
Beta value for RRAA (value for calculating ORI threshold).
void CheckTimeout(RraaWifiRemoteStation *station)
Check if the counter should be reset.
void DoReportRtsOk(WifiRemoteStation *station, double ctsSnr, WifiMode ctsMode, double rtsSnr) override
This method is a pure virtual method that must be implemented by the sub-class.
void DoReportFinalDataFailed(WifiRemoteStation *station) override
This method is a pure virtual method that must be implemented by the sub-class.
void DoReportDataOk(WifiRemoteStation *station, double ackSnr, WifiMode ackMode, double dataSnr, uint16_t dataChannelWidth, uint8_t dataNss) override
This method is a pure virtual method that must be implemented by the sub-class.
WifiRraaThresholds GetThresholds(RraaWifiRemoteStation *station, WifiMode mode) const
Get the thresholds for the given station and mode.
void DoInitialize() override
Initialize() implementation.
uint32_t m_ackLength
Ack frame length used to calculate mode TxTime.
double m_alpha
Alpha value for RRAA (value for calculating MTL threshold)
Time m_sifs
Value of SIFS configured in the device.
void InitThresholds(RraaWifiRemoteStation *station)
Initialize the thresholds internal list for the given station.
uint8_t GetMaxRate(RraaWifiRemoteStation *station) const
Return the index for the maximum transmission rate for the given station.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
represent a single transmission mode
Definition: wifi-mode.h:51
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:185
uint64_t GetDataRate(uint16_t channelWidth, uint16_t guardInterval, uint8_t nss) const
Definition: wifi-mode.cc:122
hold a list of per-remote-station state.
uint16_t GetChannelWidth(const WifiRemoteStation *station) const
Return the channel width supported by the station.
uint8_t GetNSupported(const WifiRemoteStation *station) const
Return the number of modes supported by the given station.
bool GetAggregation(const WifiRemoteStation *station) const
Return whether the given station supports A-MPDU.
bool GetHtSupported() const
Return whether the device has HT capability support enabled.
WifiMode GetNonErpSupported(const WifiRemoteStation *station, uint8_t i) const
Return whether non-ERP mode associated with the specified station at the specified index.
virtual void SetupPhy(const Ptr< WifiPhy > phy)
Set up PHY associated with this device since it is the object that knows the full set of transmit rat...
bool GetUseNonErpProtection() const
Return whether the device supports protection of non-ERP stations.
bool GetVhtSupported() const
Return whether the device has VHT capability support enabled.
bool GetShortPreambleEnabled() const
Return whether the device uses short PHY preambles.
WifiMode GetSupported(const WifiRemoteStation *station, uint8_t i) const
Return whether mode associated with the specified station at the specified index.
bool GetHeSupported() const
Return whether the device has HE capability support enabled.
virtual void SetupMac(const Ptr< WifiMac > mac)
Set up MAC associated with this device since it is the object that knows the full set of timing param...
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
void SetMode(WifiMode mode)
Sets the selected payload transmission mode.
void SetPreambleType(WifiPreamble preamble)
Sets the preamble type.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
@ WIFI_PREAMBLE_LONG
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
std::vector< std::pair< WifiRraaThresholds, WifiMode > > RraaThresholdsTable
List of thresholds for each mode.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
WifiPreamble GetPreambleForTransmission(WifiModulationClass modulation, bool useShortPreamble)
Return the preamble to be used for the transmission.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
mac
Definition: third.py:92
phy
Definition: third.py:89
hold per-remote-station state for RRAA Wifi manager.
uint8_t m_nRate
Number of supported rates.
Time m_lastReset
Time of the last reset.
RraaThresholdsTable m_thresholds
RRAA thresholds for this station.
bool m_initialized
For initializing variables.
uint32_t m_counter
Counter for transmission attempts.
uint32_t m_rtsCounter
Counter for RTS transmission attempts.
uint32_t m_adaptiveRtsWnd
Window size for the Adaptive RTS mechanism.
bool m_lastFrameFail
Flag if the last frame sent has failed.
bool m_adaptiveRtsOn
Check if Adaptive RTS mechanism is on.
uint8_t m_rateIndex
Current rate index.
uint32_t m_nFailed
Number of failed transmission attempts.
hold per-remote-station state.
WifiRraaThresholds structure.
double m_mtl
Maximum Tolerable Loss threshold.
uint32_t m_ewnd
Evaluation Window.
double m_ori
Opportunistic Rate Increase threshold.