00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "wifi-remote-station-manager.h"
00022 #include "ns3/assert.h"
00023 #include "ns3/log.h"
00024 #include "ns3/tag.h"
00025 #include "ns3/boolean.h"
00026 #include "ns3/uinteger.h"
00027 #include "ns3/wifi-phy.h"
00028 #include "ns3/trace-source-accessor.h"
00029
00030 NS_LOG_COMPONENT_DEFINE ("WifiRemoteStationManager");
00031
00032 namespace ns3 {
00033
00034
00035
00036
00037
00038
00039
00040
00041 class NonUnicastWifiRemoteStation : public WifiRemoteStation
00042 {
00043 public:
00044 NonUnicastWifiRemoteStation (Ptr<WifiRemoteStationManager> stations);
00045 protected:
00046 virtual void DoReportRxOk (double rxSnr, WifiMode txMode);
00047 virtual void DoReportRtsFailed (void);
00048 virtual void DoReportDataFailed (void);
00049 virtual void DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr);
00050 virtual void DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr);
00051 virtual void DoReportFinalRtsFailed (void);
00052 virtual void DoReportFinalDataFailed (void);
00053 private:
00054 virtual Ptr<WifiRemoteStationManager> GetManager (void) const;
00055 virtual WifiMode DoGetDataMode (uint32_t size);
00056 virtual WifiMode DoGetRtsMode (void);
00057 Ptr<WifiRemoteStationManager> m_stations;
00058 };
00059
00060 NonUnicastWifiRemoteStation::NonUnicastWifiRemoteStation (Ptr<WifiRemoteStationManager> stations)
00061 : m_stations (stations)
00062 {
00063 RecordDisassociated ();
00064 }
00065 void
00066 NonUnicastWifiRemoteStation::DoReportRxOk (double rxSnr, WifiMode txMode)
00067 {
00068 NS_ASSERT (false);
00069 }
00070 void
00071 NonUnicastWifiRemoteStation::DoReportRtsFailed (void)
00072 {
00073 NS_ASSERT (false);
00074 }
00075 void
00076 NonUnicastWifiRemoteStation::DoReportDataFailed (void)
00077 {
00078 NS_ASSERT (false);
00079 }
00080 void
00081 NonUnicastWifiRemoteStation::DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr)
00082 {
00083 NS_ASSERT (false);
00084 }
00085 void
00086 NonUnicastWifiRemoteStation::DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr)
00087 {
00088 NS_ASSERT (false);
00089 }
00090 void
00091 NonUnicastWifiRemoteStation::DoReportFinalRtsFailed (void)
00092 {}
00093 void
00094 NonUnicastWifiRemoteStation::DoReportFinalDataFailed (void)
00095 {}
00096
00097 WifiMode
00098 NonUnicastWifiRemoteStation::DoGetDataMode (uint32_t size)
00099 {
00100 WifiMode mode = m_stations->GetBasicMode (0);
00101 NS_LOG_DEBUG ("non-unicast size="<<size<<", mode="<<mode);
00102 return mode;
00103 }
00104 WifiMode
00105 NonUnicastWifiRemoteStation::DoGetRtsMode (void)
00106 {
00107 NS_ASSERT (false);
00108
00109 return m_stations->GetBasicMode (0);
00110 }
00111 Ptr<WifiRemoteStationManager>
00112 NonUnicastWifiRemoteStation::GetManager (void) const
00113 {
00114 return m_stations;
00115 }
00116
00117
00118 }
00119
00120 namespace ns3 {
00121
00122 NS_OBJECT_ENSURE_REGISTERED (WifiRemoteStationManager);
00123
00124 TypeId
00125 WifiRemoteStationManager::GetTypeId (void)
00126 {
00127 static TypeId tid = TypeId ("ns3::WifiRemoteStationManager")
00128 .SetParent<Object> ()
00129 .AddAttribute ("IsLowLatency", "If true, we attempt to modelize a so-called low-latency device: a device"
00130 " where decisions about tx parameters can be made on a per-packet basis and feedback about the"
00131 " transmission of each packet is obtained before sending the next. Otherwise, we modelize a "
00132 " high-latency device, that is a device where we cannot update our decision about tx parameters"
00133 " after every packet transmission.",
00134 BooleanValue (true),
00135 MakeBooleanAccessor (&WifiRemoteStationManager::m_isLowLatency),
00136 MakeBooleanChecker ())
00137 .AddAttribute ("MaxSsrc", "The maximum number of retransmission attempts for an RTS. This value"
00138 " will not have any effect on some rate control algorithms.",
00139 UintegerValue (7),
00140 MakeUintegerAccessor (&WifiRemoteStationManager::m_maxSsrc),
00141 MakeUintegerChecker<uint32_t> ())
00142 .AddAttribute ("MaxSlrc", "The maximum number of retransmission attempts for a DATA packet. This value"
00143 " will not have any effect on some rate control algorithms.",
00144 UintegerValue (7),
00145 MakeUintegerAccessor (&WifiRemoteStationManager::m_maxSlrc),
00146 MakeUintegerChecker<uint32_t> ())
00147 .AddAttribute ("RtsCtsThreshold", "If a data packet is bigger than this value, we use an RTS/CTS handshake"
00148 " before sending the data. This value will not have any effect on some rate control algorithms.",
00149 UintegerValue (1500),
00150 MakeUintegerAccessor (&WifiRemoteStationManager::m_rtsCtsThreshold),
00151 MakeUintegerChecker<uint32_t> ())
00152 .AddAttribute ("FragmentationThreshold", "If a data packet is bigger than this value, we fragment it such that"
00153 " the size of the fragments are equal or smaller than this value. This value will not have any effect"
00154 " on some rate control algorithms.",
00155 UintegerValue (1500),
00156 MakeUintegerAccessor (&WifiRemoteStationManager::m_fragmentationThreshold),
00157 MakeUintegerChecker<uint32_t> ())
00158 ;
00159 return tid;
00160 }
00161
00162 WifiRemoteStationManager::WifiRemoteStationManager ()
00163 : m_nonUnicast (new NonUnicastWifiRemoteStation (this))
00164 {}
00165
00166 WifiRemoteStationManager::~WifiRemoteStationManager ()
00167 {
00168 }
00169 void
00170 WifiRemoteStationManager::DoDispose (void)
00171 {
00172 for (Stations::const_iterator i = m_stations.begin (); i != m_stations.end (); i++)
00173 {
00174 delete (*i).second;
00175 }
00176 m_stations.clear ();
00177 delete m_nonUnicast;
00178 }
00179 void
00180 WifiRemoteStationManager::SetupPhy (Ptr<WifiPhy> phy)
00181 {
00182 m_defaultTxMode = phy->GetMode (0);
00183 Reset ();
00184 }
00185
00186 uint32_t
00187 WifiRemoteStationManager::GetMaxSsrc (void) const
00188 {
00189 return m_maxSsrc;
00190 }
00191 uint32_t
00192 WifiRemoteStationManager::GetMaxSlrc (void) const
00193 {
00194 return m_maxSlrc;
00195 }
00196 uint32_t
00197 WifiRemoteStationManager::GetRtsCtsThreshold (void) const
00198 {
00199 return m_rtsCtsThreshold;
00200 }
00201 uint32_t
00202 WifiRemoteStationManager::GetFragmentationThreshold (void) const
00203 {
00204 return m_fragmentationThreshold;
00205 }
00206 void
00207 WifiRemoteStationManager::SetMaxSsrc (uint32_t maxSsrc)
00208 {
00209 m_maxSsrc = maxSsrc;
00210 }
00211 void
00212 WifiRemoteStationManager::SetMaxSlrc (uint32_t maxSlrc)
00213 {
00214 m_maxSlrc = maxSlrc;
00215 }
00216 void
00217 WifiRemoteStationManager::SetRtsCtsThreshold (uint32_t threshold)
00218 {
00219 m_rtsCtsThreshold = threshold;
00220 }
00221 void
00222 WifiRemoteStationManager::SetFragmentationThreshold (uint32_t threshold)
00223 {
00224 m_fragmentationThreshold = threshold;
00225 }
00226
00227 WifiRemoteStation *
00228 WifiRemoteStationManager::Lookup (Mac48Address address)
00229 {
00230 if (address.IsBroadcast () ||
00231 address.IsMulticast ())
00232 {
00233 return m_nonUnicast;
00234 }
00235 for (Stations::const_iterator i = m_stations.begin (); i != m_stations.end (); i++)
00236 {
00237 if ((*i).first == address)
00238 {
00239 return (*i).second;
00240 }
00241 }
00242 WifiRemoteStation *station = CreateStation ();
00243 station->Reset ();
00244 m_stations.push_back (std::make_pair (address, station));
00245 return station;
00246 }
00247
00248 WifiRemoteStation *
00249 WifiRemoteStationManager::LookupNonUnicast (void)
00250 {
00251 return m_nonUnicast;
00252 }
00253
00254 WifiMode
00255 WifiRemoteStationManager::GetDefaultMode (void) const
00256 {
00257 return m_defaultTxMode;
00258 }
00259 void
00260 WifiRemoteStationManager::Reset (void)
00261 {
00262 for (Stations::const_iterator i = m_stations.begin (); i != m_stations.end (); i++)
00263 {
00264 delete i->second;
00265 }
00266 m_stations.clear ();
00267 m_basicModes.clear ();
00268 m_basicModes.push_back (m_defaultTxMode);
00269 NS_ASSERT (m_defaultTxMode.IsMandatory ());
00270 }
00271 void
00272 WifiRemoteStationManager::AddBasicMode (WifiMode mode)
00273 {
00274 for (uint32_t i = 0; i < GetNBasicModes (); i++)
00275 {
00276 if (GetBasicMode (i) == mode)
00277 {
00278 return;
00279 }
00280 }
00281 m_basicModes.push_back (mode);
00282 }
00283 uint32_t
00284 WifiRemoteStationManager::GetNBasicModes (void) const
00285 {
00286 return m_basicModes.size ();
00287 }
00288 WifiMode
00289 WifiRemoteStationManager::GetBasicMode (uint32_t i) const
00290 {
00291 NS_ASSERT (i < m_basicModes.size ());
00292 return m_basicModes[i];
00293 }
00294 WifiRemoteStationManager::BasicModesIterator
00295 WifiRemoteStationManager::BeginBasicModes (void) const
00296 {
00297 return m_basicModes.begin ();
00298 }
00299 WifiRemoteStationManager::BasicModesIterator
00300 WifiRemoteStationManager::EndBasicModes (void) const
00301 {
00302 return m_basicModes.end ();
00303 }
00304 bool
00305 WifiRemoteStationManager::IsLowLatency (void) const
00306 {
00307 return m_isLowLatency;
00308 }
00309
00310 }
00311
00312
00313
00314
00315
00316 namespace ns3 {
00317
00318 class TxModeTag : public Tag
00319 {
00320 public:
00321 TxModeTag ();
00322 TxModeTag (WifiMode rtsMode, WifiMode dataMode);
00323 WifiMode GetRtsMode (void) const;
00324 WifiMode GetDataMode (void) const;
00325
00326 static TypeId GetTypeId (void);
00327 virtual TypeId GetInstanceTypeId (void) const;
00328 virtual uint32_t GetSerializedSize (void) const;
00329 virtual void Serialize (TagBuffer i) const;
00330 virtual void Deserialize (TagBuffer i);
00331 virtual void Print (std::ostream &os) const;
00332 private:
00333 WifiMode m_rtsMode;
00334 WifiMode m_dataMode;
00335 };
00336
00337 TxModeTag::TxModeTag ()
00338 {}
00339 TxModeTag::TxModeTag (WifiMode rtsMode, WifiMode dataMode)
00340 : m_rtsMode (rtsMode),
00341 m_dataMode (dataMode)
00342 {}
00343 WifiMode
00344 TxModeTag::GetRtsMode (void) const
00345 {
00346 return m_rtsMode;
00347 }
00348 WifiMode
00349 TxModeTag::GetDataMode (void) const
00350 {
00351 return m_dataMode;
00352 }
00353 TypeId
00354 TxModeTag::GetTypeId (void)
00355 {
00356 static TypeId tid = TypeId ("ns3::TxModeTag")
00357 .SetParent<Tag> ()
00358 .AddConstructor<TxModeTag> ()
00359 .AddAttribute ("RtsTxMode",
00360 "Tx mode of rts to use later",
00361 EmptyAttributeValue (),
00362 MakeWifiModeAccessor (&TxModeTag::GetRtsMode),
00363 MakeWifiModeChecker ())
00364 .AddAttribute ("DataTxMode",
00365 "Tx mode of data to use later",
00366 EmptyAttributeValue (),
00367 MakeWifiModeAccessor (&TxModeTag::GetDataMode),
00368 MakeWifiModeChecker ())
00369 ;
00370 return tid;
00371 }
00372 TypeId
00373 TxModeTag::GetInstanceTypeId (void) const
00374 {
00375 return GetTypeId ();
00376 }
00377 uint32_t
00378 TxModeTag::GetSerializedSize (void) const
00379 {
00380 return sizeof (WifiMode) * 2;
00381 }
00382 void
00383 TxModeTag::Serialize (TagBuffer i) const
00384 {
00385 i.Write ((uint8_t *)&m_rtsMode, sizeof (WifiMode));
00386 i.Write ((uint8_t *)&m_dataMode, sizeof (WifiMode));
00387 }
00388 void
00389 TxModeTag::Deserialize (TagBuffer i)
00390 {
00391 i.Read ((uint8_t *)&m_rtsMode, sizeof (WifiMode));
00392 i.Read ((uint8_t *)&m_dataMode, sizeof (WifiMode));
00393 }
00394 void
00395 TxModeTag::Print (std::ostream &os) const
00396 {
00397 os << "Rts=" << m_rtsMode << ", Data=" << m_dataMode;
00398 }
00399
00400 }
00401
00402
00403
00404
00405
00406
00407 namespace ns3 {
00408
00409 TypeId
00410 WifiRemoteStation::GetTypeId (void)
00411 {
00412 static TypeId tid = TypeId ("ns3::WifiRemoteStation")
00413 .SetParent<Object> ()
00414 .AddTraceSource ("Ssrc", "The value of the ssrc counter: indicates the number of retransmissions of RTS.",
00415 MakeTraceSourceAccessor (&WifiRemoteStation::m_ssrc))
00416 .AddTraceSource ("Slrc", "The value of the slrc counter: indicates the number of retransmissions of DATA.",
00417 MakeTraceSourceAccessor (&WifiRemoteStation::m_slrc))
00418 ;
00419 return tid;
00420 }
00421
00422 WifiRemoteStation::WifiRemoteStation ()
00423 : m_state (BRAND_NEW),
00424 m_ssrc (0),
00425 m_slrc (0)
00426 {}
00427 WifiRemoteStation::~WifiRemoteStation ()
00428 {}
00429
00430 bool
00431 WifiRemoteStation::IsBrandNew (void) const
00432 {
00433 return m_state == BRAND_NEW;
00434 }
00435
00436 bool
00437 WifiRemoteStation::IsAssociated (void) const
00438 {
00439 return m_state == GOT_ASSOC_TX_OK;
00440 }
00441 bool
00442 WifiRemoteStation::IsWaitAssocTxOk (void) const
00443 {
00444 return m_state == WAIT_ASSOC_TX_OK;
00445 }
00446 void
00447 WifiRemoteStation::RecordWaitAssocTxOk (void)
00448 {
00449 m_state = WAIT_ASSOC_TX_OK;
00450 }
00451 void
00452 WifiRemoteStation::RecordGotAssocTxOk (void)
00453 {
00454 m_state = GOT_ASSOC_TX_OK;
00455 }
00456 void
00457 WifiRemoteStation::RecordGotAssocTxFailed (void)
00458 {
00459 m_state = DISASSOC;
00460 }
00461 void
00462 WifiRemoteStation::RecordDisassociated (void)
00463 {
00464 m_state = DISASSOC;
00465 }
00466
00467 void
00468 WifiRemoteStation::Reset (void)
00469 {
00470 m_modes.clear ();
00471 AddSupportedMode (GetManager ()->GetDefaultMode ());
00472 }
00473 void
00474 WifiRemoteStation::AddSupportedMode (WifiMode mode)
00475 {
00476 if (IsIn (mode))
00477 {
00478 return;
00479 }
00480 m_modes.push_back (mode);
00481 }
00482
00483 bool
00484 WifiRemoteStation::IsIn (WifiMode mode) const
00485 {
00486 for (SupportedModes::const_iterator i = m_modes.begin (); i != m_modes.end (); i++)
00487 {
00488 if ((*i) == mode)
00489 {
00490 return true;
00491 }
00492 }
00493 return false;
00494 }
00495
00496 WifiMode
00497 WifiRemoteStation::GetControlAnswerMode (WifiMode reqMode)
00498 {
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 WifiMode mode = GetManager ()->GetDefaultMode ();
00520 bool found = false;
00521
00522
00523 for (WifiRemoteStationManager::BasicModesIterator i = GetManager ()->BeginBasicModes ();
00524 i != GetManager ()->EndBasicModes (); i++)
00525 {
00526 if (i->GetPhyRate () > mode.GetPhyRate () &&
00527 i->GetPhyRate () <= reqMode.GetPhyRate () &&
00528 i->GetModulationType () == reqMode.GetModulationType ())
00529 {
00530 mode = *i;
00531 found = true;
00532 }
00533 }
00534
00535
00536 return mode;
00537 }
00538
00539 WifiMode
00540 WifiRemoteStation::GetCtsMode (WifiMode rtsMode)
00541 {
00542 return GetControlAnswerMode (rtsMode);
00543 }
00544 WifiMode
00545 WifiRemoteStation::GetAckMode (WifiMode dataMode)
00546 {
00547 return GetControlAnswerMode (dataMode);
00548 }
00549
00550 uint32_t
00551 WifiRemoteStation::GetNSupportedModes (void) const
00552 {
00553 return m_modes.size ();
00554 }
00555 WifiMode
00556 WifiRemoteStation::GetSupportedMode (uint32_t i) const
00557 {
00558 NS_ASSERT (i < m_modes.size ());
00559 return m_modes[i];
00560 }
00561 void
00562 WifiRemoteStation::PrepareForQueue (Ptr<const Packet> packet, uint32_t fullPacketSize)
00563 {
00564 if (GetManager ()->IsLowLatency ())
00565 {
00566 return;
00567 }
00568 TxModeTag tag = TxModeTag (DoGetRtsMode (), DoGetDataMode (fullPacketSize));
00569 packet->AddTag (tag);
00570 }
00571 WifiMode
00572 WifiRemoteStation::GetDataMode (Ptr<const Packet> packet, uint32_t fullPacketSize)
00573 {
00574 if (GetManager ()->IsLowLatency ())
00575 {
00576 return DoGetDataMode (fullPacketSize);
00577 }
00578 TxModeTag tag;
00579 bool found;
00580 found = packet->FindFirstMatchingTag (tag);
00581 NS_ASSERT (found);
00582 return tag.GetDataMode ();
00583 }
00584 WifiMode
00585 WifiRemoteStation::GetRtsMode (Ptr<const Packet> packet)
00586 {
00587 if (GetManager ()->IsLowLatency ())
00588 {
00589 return DoGetRtsMode ();
00590 }
00591 TxModeTag tag;
00592 bool found;
00593 found = packet->FindFirstMatchingTag (tag);
00594 NS_ASSERT (found);
00595 return tag.GetRtsMode ();
00596 }
00597
00598 bool
00599 WifiRemoteStation::NeedRts (Ptr<const Packet> packet)
00600 {
00601 if (packet->GetSize () > GetManager ()->GetRtsCtsThreshold ())
00602 {
00603 return true;
00604 }
00605 else
00606 {
00607 return false;
00608 }
00609 }
00610 bool
00611 WifiRemoteStation::NeedRtsRetransmission (Ptr<const Packet> packet)
00612 {
00613 return (m_ssrc < GetManager ()->GetMaxSsrc ());
00614 }
00615
00616 bool
00617 WifiRemoteStation::NeedDataRetransmission (Ptr<const Packet> packet)
00618 {
00619 return (m_slrc < GetManager ()->GetMaxSlrc ());
00620 }
00621
00622 bool
00623 WifiRemoteStation::NeedFragmentation (Ptr<const Packet> packet)
00624 {
00625 if (packet->GetSize () > GetManager ()->GetFragmentationThreshold ())
00626 {
00627 return true;
00628 }
00629 else
00630 {
00631 return false;
00632 }
00633 }
00634 uint32_t
00635 WifiRemoteStation::GetNFragments (Ptr<const Packet> packet)
00636 {
00637 uint32_t nFragments = packet->GetSize () / GetManager ()->GetFragmentationThreshold () + 1;
00638 return nFragments;
00639 }
00640
00641 uint32_t
00642 WifiRemoteStation::GetFragmentSize (Ptr<const Packet> packet, uint32_t fragmentNumber)
00643 {
00644 uint32_t nFragment = GetNFragments (packet);
00645 if (fragmentNumber >= nFragment)
00646 {
00647 return 0;
00648 }
00649 if (fragmentNumber == nFragment - 1)
00650 {
00651 uint32_t lastFragmentSize = packet->GetSize () % GetManager ()->GetFragmentationThreshold ();
00652 return lastFragmentSize;
00653 }
00654 else
00655 {
00656 return GetManager ()->GetFragmentationThreshold ();
00657 }
00658 }
00659 uint32_t
00660 WifiRemoteStation::GetFragmentOffset (Ptr<const Packet> packet, uint32_t fragmentNumber)
00661 {
00662 NS_ASSERT (fragmentNumber < GetNFragments (packet));
00663 uint32_t fragmentOffset = fragmentNumber * GetManager ()->GetFragmentationThreshold ();
00664 return fragmentOffset;
00665 }
00666
00667 bool
00668 WifiRemoteStation::IsLastFragment (Ptr<const Packet> packet, uint32_t fragmentNumber)
00669 {
00670 if (fragmentNumber == (GetNFragments (packet) - 1))
00671 {
00672 return true;
00673 }
00674 else
00675 {
00676 return false;
00677 }
00678 }
00679
00680 void
00681 WifiRemoteStation::ReportRtsFailed (void)
00682 {
00683 m_ssrc++;
00684 DoReportRtsFailed ();
00685 }
00686
00687 void
00688 WifiRemoteStation::ReportDataFailed (void)
00689 {
00690 m_slrc++;
00691 DoReportDataFailed ();
00692 }
00693
00694 void
00695 WifiRemoteStation::ReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr)
00696 {
00697 m_ssrc = 0;
00698 DoReportRtsOk (ctsSnr, ctsMode, rtsSnr);
00699 }
00700
00701 void
00702 WifiRemoteStation::ReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr)
00703 {
00704 m_slrc = 0;
00705 DoReportDataOk (ackSnr, ackMode, dataSnr);
00706 }
00707
00708 void
00709 WifiRemoteStation::ReportFinalRtsFailed (void)
00710 {
00711 m_ssrc = 0;
00712 DoReportFinalRtsFailed ();
00713 }
00714
00715 void
00716 WifiRemoteStation::ReportFinalDataFailed (void)
00717 {
00718 m_slrc = 0;
00719 DoReportFinalDataFailed ();
00720 }
00721
00722 void
00723 WifiRemoteStation::ReportRxOk (double rxSnr, WifiMode txMode)
00724 {
00725 DoReportRxOk (rxSnr, txMode);
00726 }
00727 }
00728