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 (TxTime::const_iterator 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 (RraaThresholdsTable::const_iterator i = station->m_thresholds.begin();
196  i != station->m_thresholds.end();
197  i++)
198  {
199  if (mode == i->second)
200  {
201  return i->first;
202  }
203  }
204  NS_ABORT_MSG("No thresholds for mode " << mode << " found");
205  return threshold; // Silence compiler warning
206 }
207 
210 {
212  station->m_initialized = false;
213  station->m_adaptiveRtsWnd = 0;
214  station->m_rtsCounter = 0;
215  station->m_adaptiveRtsOn = false;
216  station->m_lastFrameFail = false;
217  return station;
218 }
219 
220 void
222 {
223  NS_LOG_FUNCTION(this << station);
224  if (!station->m_initialized)
225  {
226  // Note: we appear to be doing late initialization of the table
227  // to make sure that the set of supported rates has been initialized
228  // before we perform our own initialization.
229  station->m_nRate = GetNSupported(station);
230  // Initialize at maximal rate
231  station->m_rateIndex = GetMaxRate(station);
232 
233  station->m_initialized = true;
234 
235  station->m_thresholds = RraaThresholdsTable(station->m_nRate);
236  InitThresholds(station);
237  ResetCountersBasic(station);
238  }
239 }
240 
241 void
243 {
244  NS_LOG_FUNCTION(this << station);
245  NS_LOG_DEBUG("InitThresholds = " << station);
246 
247  double nextCritical = 0;
248  double nextMtl = 0;
249  double mtl = 0;
250  double ori = 0;
251  for (uint8_t i = 0; i < station->m_nRate; i++)
252  {
253  WifiMode mode = GetSupported(station, i);
254  Time totalTxTime = GetCalcTxTime(mode) + m_sifs + m_difs;
255  if (i == GetMaxRate(station))
256  {
257  ori = 0;
258  }
259  else
260  {
261  WifiMode nextMode = GetSupported(station, i + 1);
262  Time nextTotalTxTime = GetCalcTxTime(nextMode) + m_sifs + m_difs;
263  nextCritical = 1 - (nextTotalTxTime.GetSeconds() / totalTxTime.GetSeconds());
264  nextMtl = m_alpha * nextCritical;
265  ori = nextMtl / m_beta;
266  }
267  if (i == 0)
268  {
269  mtl = 1;
270  }
272  th.m_ewnd = static_cast<uint32_t>(ceil(m_tau / totalTxTime.GetSeconds()));
273  th.m_ori = ori;
274  th.m_mtl = mtl;
275  station->m_thresholds.emplace_back(th, mode);
276  mtl = nextMtl;
277  NS_LOG_DEBUG(mode << " " << th.m_ewnd << " " << th.m_mtl << " " << th.m_ori);
278  }
279 }
280 
281 void
283 {
284  NS_LOG_FUNCTION(this << station);
285  station->m_nFailed = 0;
286  station->m_counter = GetThresholds(station, station->m_rateIndex).m_ewnd;
287  station->m_lastReset = Simulator::Now();
288 }
289 
290 uint8_t
292 {
293  return station->m_nRate - 1;
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION(this << st);
300 }
301 
302 void
304 {
305  NS_LOG_FUNCTION(this << st);
306  RraaWifiRemoteStation* station = static_cast<RraaWifiRemoteStation*>(st);
307  station->m_lastFrameFail = true;
308  CheckTimeout(station);
309  station->m_counter--;
310  station->m_nFailed++;
311  RunBasicAlgorithm(station);
312 }
313 
314 void
316 {
317  NS_LOG_FUNCTION(this << st << rxSnr << txMode);
318 }
319 
320 void
322  double ctsSnr,
323  WifiMode ctsMode,
324  double rtsSnr)
325 {
326  NS_LOG_FUNCTION(this << st << ctsSnr << ctsMode << rtsSnr);
327 }
328 
329 void
331  double ackSnr,
332  WifiMode ackMode,
333  double dataSnr,
334  uint16_t dataChannelWidth,
335  uint8_t dataNss)
336 {
337  NS_LOG_FUNCTION(this << st << ackSnr << ackMode << dataSnr << dataChannelWidth << +dataNss);
338  RraaWifiRemoteStation* station = static_cast<RraaWifiRemoteStation*>(st);
339  station->m_lastFrameFail = false;
340  CheckTimeout(station);
341  station->m_counter--;
342  RunBasicAlgorithm(station);
343 }
344 
345 void
347 {
348  NS_LOG_FUNCTION(this << st);
349 }
350 
351 void
353 {
354  NS_LOG_FUNCTION(this << st);
355 }
356 
359 {
360  NS_LOG_FUNCTION(this << st << allowedWidth);
361  RraaWifiRemoteStation* station = static_cast<RraaWifiRemoteStation*>(st);
362  uint16_t channelWidth = GetChannelWidth(station);
363  if (channelWidth > 20 && channelWidth != 22)
364  {
365  channelWidth = 20;
366  }
367  CheckInit(station);
368  WifiMode mode = GetSupported(station, station->m_rateIndex);
369  uint64_t rate = mode.GetDataRate(channelWidth);
370  if (m_currentRate != rate)
371  {
372  NS_LOG_DEBUG("New datarate: " << rate);
373  m_currentRate = rate;
374  }
375  return WifiTxVector(
376  mode,
379  800,
380  1,
381  1,
382  0,
383  channelWidth,
384  GetAggregation(station));
385 }
386 
389 {
390  NS_LOG_FUNCTION(this << st);
391  RraaWifiRemoteStation* station = static_cast<RraaWifiRemoteStation*>(st);
392  uint16_t channelWidth = GetChannelWidth(station);
393  if (channelWidth > 20 && channelWidth != 22)
394  {
395  channelWidth = 20;
396  }
397  WifiMode mode;
398  if (GetUseNonErpProtection() == false)
399  {
400  mode = GetSupported(station, 0);
401  }
402  else
403  {
404  mode = GetNonErpSupported(station, 0);
405  }
406  return WifiTxVector(
407  mode,
410  800,
411  1,
412  1,
413  0,
414  channelWidth,
415  GetAggregation(station));
416 }
417 
418 bool
419 RraaWifiManager::DoNeedRts(WifiRemoteStation* st, uint32_t size, bool normally)
420 {
421  NS_LOG_FUNCTION(this << st << size << normally);
422  RraaWifiRemoteStation* station = static_cast<RraaWifiRemoteStation*>(st);
423  CheckInit(station);
424  if (m_basic)
425  {
426  return normally;
427  }
428  ARts(station);
429  return station->m_adaptiveRtsOn;
430 }
431 
432 void
434 {
435  NS_LOG_FUNCTION(this << station);
436  Time d = Simulator::Now() - station->m_lastReset;
437  if (station->m_counter == 0 || d > m_timeout)
438  {
439  ResetCountersBasic(station);
440  }
441 }
442 
443 void
445 {
446  NS_LOG_FUNCTION(this << station);
447  WifiRraaThresholds thresholds = GetThresholds(station, station->m_rateIndex);
448  double ploss = (station->m_nFailed / thresholds.m_ewnd);
449  if (station->m_counter == 0 || ploss > thresholds.m_mtl)
450  {
451  if (ploss > thresholds.m_mtl)
452  {
453  station->m_rateIndex--;
454  }
455  else if (station->m_rateIndex < GetMaxRate(station) && ploss < thresholds.m_ori)
456  {
457  station->m_rateIndex++;
458  }
459  ResetCountersBasic(station);
460  }
461 }
462 
463 void
465 {
466  if (!station->m_adaptiveRtsOn && station->m_lastFrameFail)
467  {
468  station->m_adaptiveRtsWnd++;
469  station->m_rtsCounter = station->m_adaptiveRtsWnd;
470  }
471  else if ((station->m_adaptiveRtsOn && station->m_lastFrameFail) ||
472  (!station->m_adaptiveRtsOn && !station->m_lastFrameFail))
473  {
474  station->m_adaptiveRtsWnd = station->m_adaptiveRtsWnd / 2;
475  station->m_rtsCounter = station->m_adaptiveRtsWnd;
476  }
477  if (station->m_rtsCounter > 0)
478  {
479  station->m_adaptiveRtsOn = true;
480  station->m_rtsCounter--;
481  }
482  else
483  {
484  station->m_adaptiveRtsOn = false;
485  }
486 }
487 
490 {
491  NS_LOG_FUNCTION(this << station << +index);
492  WifiMode mode = GetSupported(station, index);
493  return GetThresholds(station, mode);
494 }
495 
496 } // namespace ns3
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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:199
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:402
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
represent a single transmission mode
Definition: wifi-mode.h:50
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
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:43
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1424
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:46
#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:1336
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 > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535
std::vector< std::pair< WifiRraaThresholds, WifiMode > > RraaThresholdsTable
List of thresholds for each mode.
WifiPreamble GetPreambleForTransmission(WifiModulationClass modulation, bool useShortPreamble)
Return the preamble to be used for the transmission.
mac
Definition: third.py:85
phy
Definition: third.py:82
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.