A Discrete-Event Network Simulator
API
txop.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/pointer.h"
23 #include "ns3/simulator.h"
24 #include "ns3/random-variable-stream.h"
25 #include "ns3/socket.h"
26 #include "txop.h"
27 #include "channel-access-manager.h"
28 #include "regular-wifi-mac.h"
29 #include "wifi-mac-queue.h"
30 #include "mac-tx-middle.h"
31 #include "wifi-mac-trailer.h"
32 
33 #undef NS_LOG_APPEND_CONTEXT
34 #define NS_LOG_APPEND_CONTEXT if (m_mac != 0) { std::clog << "[mac=" << m_mac->GetAddress () << "] "; }
35 
36 namespace ns3 {
37 
39 
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::Txop")
47  .SetGroupName ("Wifi")
48  .AddConstructor<Txop> ()
49  .AddAttribute ("MinCw", "The minimum value of the contention window.",
50  UintegerValue (15),
53  MakeUintegerChecker<uint32_t> ())
54  .AddAttribute ("MaxCw", "The maximum value of the contention window.",
55  UintegerValue (1023),
58  MakeUintegerChecker<uint32_t> ())
59  .AddAttribute ("Aifsn", "The AIFSN: the default value conforms to non-QOS.",
60  UintegerValue (2),
63  MakeUintegerChecker<uint8_t> ())
64  .AddAttribute ("TxopLimit", "The TXOP limit: the default value conforms to non-QoS.",
65  TimeValue (MilliSeconds (0)),
68  MakeTimeChecker ())
69  .AddAttribute ("Queue", "The WifiMacQueue object",
70  PointerValue (),
72  MakePointerChecker<WifiMacQueue> ())
73  .AddTraceSource ("BackoffTrace",
74  "Trace source for backoff values",
76  "ns3::TracedCallback::Uint32Callback")
77  .AddTraceSource ("CwTrace",
78  "Trace source for contention window values",
80  "ns3::TracedValueCallback::Uint32")
81  ;
82  return tid;
83 }
84 
86  : m_channelAccessManager (0),
87  m_cwMin (0),
88  m_cwMax (0),
89  m_cw (0),
90  m_backoff (0),
91  m_access (NOT_REQUESTED),
92  m_backoffSlots (0),
93  m_backoffStart (Seconds (0.0))
94 {
95  NS_LOG_FUNCTION (this);
96  m_queue = CreateObject<WifiMacQueue> ();
97  m_rng = CreateObject<UniformRandomVariable> ();
98 }
99 
101 {
102  NS_LOG_FUNCTION (this);
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION (this);
109  m_queue = 0;
110  m_mac = 0;
111  m_rng = 0;
112  m_txMiddle = 0;
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION (this << manager);
120  m_channelAccessManager = manager;
121  m_channelAccessManager->Add (this);
122 }
123 
124 void Txop::SetTxMiddle (const Ptr<MacTxMiddle> txMiddle)
125 {
126  NS_LOG_FUNCTION (this);
127  m_txMiddle = txMiddle;
128 }
129 
130 void
132 {
133  NS_LOG_FUNCTION (this << mac);
134  m_mac = mac;
135 }
136 
137 void
139 {
140  NS_LOG_FUNCTION (this << &callback);
141  m_droppedMpduCallback = callback;
142  m_queue->TraceConnectWithoutContext ("DropBeforeEnqueue",
144  m_queue->TraceConnectWithoutContext ("Expired",
146 }
147 
150 {
151  NS_LOG_FUNCTION (this);
152  return m_queue;
153 }
154 
155 void
156 Txop::SetMinCw (uint32_t minCw)
157 {
158  NS_LOG_FUNCTION (this << minCw);
159  bool changed = (m_cwMin != minCw);
160  m_cwMin = minCw;
161  if (changed == true)
162  {
163  ResetCw ();
164  }
165 }
166 
167 void
168 Txop::SetMaxCw (uint32_t maxCw)
169 {
170  NS_LOG_FUNCTION (this << maxCw);
171  bool changed = (m_cwMax != maxCw);
172  m_cwMax = maxCw;
173  if (changed == true)
174  {
175  ResetCw ();
176  }
177 }
178 
179 uint32_t
180 Txop::GetCw (void) const
181 {
182  return m_cw;
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION (this);
189  m_cw = GetMinCw ();
190  m_cwTrace = m_cw;
191 }
192 
193 void
195 {
196  NS_LOG_FUNCTION (this);
197  //see 802.11-2012, section 9.19.2.5
198  m_cw = std::min ( 2 * (m_cw + 1) - 1, GetMaxCw ());
199  // if the MU EDCA timer is running, CW cannot be less than MU CW min
200  m_cw = std::max (m_cw, GetMinCw ());
201  m_cwTrace = m_cw;
202 }
203 
204 uint32_t
206 {
207  return m_backoffSlots;
208 }
209 
210 Time
212 {
213  return m_backoffStart;
214 }
215 
216 void
217 Txop::UpdateBackoffSlotsNow (uint32_t nSlots, Time backoffUpdateBound)
218 {
219  NS_LOG_FUNCTION (this << nSlots << backoffUpdateBound);
220  m_backoffSlots -= nSlots;
221  m_backoffStart = backoffUpdateBound;
222  NS_LOG_DEBUG ("update slots=" << nSlots << " slots, backoff=" << m_backoffSlots);
223 }
224 
225 void
226 Txop::StartBackoffNow (uint32_t nSlots)
227 {
228  NS_LOG_FUNCTION (this << nSlots);
229  if (m_backoffSlots != 0)
230  {
231  NS_LOG_DEBUG ("reset backoff from " << m_backoffSlots << " to " << nSlots << " slots");
232  }
233  else
234  {
235  NS_LOG_DEBUG ("start backoff=" << nSlots << " slots");
236  }
237  m_backoffSlots = nSlots;
239 }
240 
241 void
242 Txop::SetAifsn (uint8_t aifsn)
243 {
244  NS_LOG_FUNCTION (this << +aifsn);
245  m_aifsn = aifsn;
246 }
247 
248 void
250 {
251  NS_LOG_FUNCTION (this << txopLimit);
252  NS_ASSERT_MSG ((txopLimit.GetMicroSeconds () % 32 == 0), "The TXOP limit must be expressed in multiple of 32 microseconds!");
253  m_txopLimit = txopLimit;
254 }
255 
256 uint32_t
257 Txop::GetMinCw (void) const
258 {
259  return m_cwMin;
260 }
261 
262 uint32_t
263 Txop::GetMaxCw (void) const
264 {
265  return m_cwMax;
266 }
267 
268 uint8_t
269 Txop::GetAifsn (void) const
270 {
271  return m_aifsn;
272 }
273 
274 Time
275 Txop::GetTxopLimit (void) const
276 {
277  return m_txopLimit;
278 }
279 
280 bool
282 {
283  bool ret = (!m_queue->IsEmpty ());
284  NS_LOG_FUNCTION (this << ret);
285  return ret;
286 }
287 
288 void
290 {
291  NS_LOG_FUNCTION (this << packet << &hdr);
292  // remove the priority tag attached, if any
293  SocketPriorityTag priorityTag;
294  packet->RemovePacketTag (priorityTag);
295  if (m_channelAccessManager->NeedBackoffUponAccess (this))
296  {
297  GenerateBackoff ();
298  }
299  m_queue->Enqueue (Create<WifiMacQueueItem> (packet, hdr));
301 }
302 
303 int64_t
304 Txop::AssignStreams (int64_t stream)
305 {
306  NS_LOG_FUNCTION (this << stream);
307  m_rng->SetStream (stream);
308  return 1;
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (this);
316  {
317  m_channelAccessManager->RequestAccess (this);
318  }
319 }
320 
321 void
323 {
324  NS_LOG_FUNCTION (this);
325  ResetCw ();
326  GenerateBackoff ();
327 }
328 
331 {
332  return m_access;
333 }
334 
335 void
337 {
338  NS_LOG_FUNCTION (this);
340 }
341 
342 void
344 {
345  NS_LOG_FUNCTION (this << txopDuration);
346  m_access = GRANTED;
347 }
348 
349 void
351 {
352  NS_LOG_FUNCTION (this);
354  GenerateBackoff ();
355  if (HasFramesToTransmit ())
356  {
358  }
359 }
360 
361 void
363 {
364  if (m_access == NOT_REQUESTED)
365  {
366  m_channelAccessManager->RequestAccess (this);
367  }
368 }
369 
370 void
372 {
373  NS_LOG_FUNCTION (this);
374  m_backoff = m_rng->GetInteger (0, GetCw ());
377 }
378 
379 void
381 {
382  NS_LOG_FUNCTION (this);
383  m_queue->Flush ();
384 }
385 
386 void
388 {
389  NS_LOG_FUNCTION (this);
390 }
391 
392 void
394 {
395  NS_LOG_FUNCTION (this);
396  m_queue->Flush ();
397 }
398 
399 void
401 {
402  NS_LOG_FUNCTION (this);
404 }
405 
406 void
408 {
409  NS_LOG_FUNCTION (this);
411 }
412 
413 bool
415 {
416  return false;
417 }
418 
419 AcIndex
421 {
422  return AC_BE_NQOS;
423 }
424 
425 } //namespace ns3
ns3::Txop::SetDroppedMpduCallback
virtual void SetDroppedMpduCallback(DroppedMpdu callback)
Definition: txop.cc:138
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Txop::m_txMiddle
Ptr< MacTxMiddle > m_txMiddle
the MacTxMiddle
Definition: txop.h:330
mac-tx-middle.h
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::Txop::m_cw
uint32_t m_cw
the current contention window
Definition: txop.h:336
min
#define min(a, b)
Definition: 80211b.c:42
regular-wifi-mac.h
ns3::Txop::NotifyChannelReleased
virtual void NotifyChannelReleased(void)
Called by the FrameExchangeManager to notify the completion of the transmissions.
Definition: txop.cc:350
ns3::Txop::SetMaxCw
void SetMaxCw(uint32_t maxCw)
Set the maximum contention window size.
Definition: txop.cc:168
ns3::Callback< void, WifiMacDropReason, Ptr< const WifiMacQueueItem > >
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Txop::SetChannelAccessManager
void SetChannelAccessManager(const Ptr< ChannelAccessManager > manager)
Set ChannelAccessManager this Txop is associated to.
Definition: txop.cc:117
ns3::Txop::m_cwTrace
TracedValue< uint32_t > m_cwTrace
CW trace value.
Definition: txop.h:351
ns3::Txop::m_channelAccessManager
Ptr< ChannelAccessManager > m_channelAccessManager
the channel access manager
Definition: txop.h:327
ns3::Txop::GetAccessStatus
virtual ChannelAccessStatus GetAccessStatus(void) const
Definition: txop.cc:330
ns3::Txop::m_backoff
uint32_t m_backoff
the current backoff
Definition: txop.h:337
ns3::Txop::NotifyWakeUp
virtual void NotifyWakeUp(void)
When wake up operation occurs, channel access will be restarted.
Definition: txop.cc:400
ns3::Txop::ResetCw
void ResetCw(void)
Update the value of the CW variable to take into account a transmission success or a transmission abo...
Definition: txop.cc:186
ns3::WIFI_MAC_DROP_EXPIRED_LIFETIME
@ WIFI_MAC_DROP_EXPIRED_LIFETIME
Definition: wifi-mac.h:57
ns3::Txop::GRANTED
@ GRANTED
Definition: txop.h:92
ns3::PointerValue
Hold objects of type Ptr<T>.
Definition: pointer.h:37
ns3::Txop::m_cwMin
uint32_t m_cwMin
the minimum contention window
Definition: txop.h:334
ns3::Txop::m_cwMax
uint32_t m_cwMax
the maximum contention window
Definition: txop.h:335
third.mac
mac
Definition: third.py:99
ns3::Txop::AssignStreams
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Definition: txop.cc:304
ns3::Txop::StartBackoffNow
void StartBackoffNow(uint32_t nSlots)
Definition: txop.cc:226
ns3::Txop::m_rng
Ptr< UniformRandomVariable > m_rng
the random stream
Definition: txop.h:332
ns3::UniformRandomVariable::GetInteger
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
Definition: random-variable-stream.cc:193
ns3::Txop::GetTxopLimit
Time GetTxopLimit(void) const
Return the TXOP limit.
Definition: txop.cc:275
ns3::Txop::NotifyChannelAccessed
virtual void NotifyChannelAccessed(Time txopDuration=Seconds(0))
Called by the FrameExchangeManager to notify that channel access has been granted for the given amoun...
Definition: txop.cc:343
txop.h
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::Txop::UpdateFailedCw
void UpdateFailedCw(void)
Update the value of the CW variable to take into account a transmission failure.
Definition: txop.cc:194
ns3::Txop::GetAccessCategory
virtual AcIndex GetAccessCategory(void) const
Get the access category.
Definition: txop.cc:420
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
wifi-mac-queue.h
ns3::Txop::NotifyChannelSwitching
virtual void NotifyChannelSwitching(void)
When a channel switching occurs, enqueued packets are removed.
Definition: txop.cc:380
ns3::Txop::m_mac
Ptr< RegularWifiMac > m_mac
the wifi MAC
Definition: txop.h:331
ns3::Txop::SetMinCw
void SetMinCw(uint32_t minCw)
Set the minimum contention window size.
Definition: txop.cc:156
ns3::Txop::GenerateBackoff
virtual void GenerateBackoff(void)
Generate a new backoff now.
Definition: txop.cc:371
ns3::Txop::m_queue
Ptr< WifiMacQueue > m_queue
the wifi MAC queue
Definition: txop.h:329
ns3::WifiMacHeader
Implements the IEEE 802.11 MAC header.
Definition: wifi-mac-header.h:85
ns3::Txop::NotifyOff
virtual void NotifyOff(void)
When off operation occurs, the queue gets cleaned up.
Definition: txop.cc:393
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Txop::m_aifsn
uint8_t m_aifsn
the AIFSN
Definition: txop.h:347
ns3::Txop::m_backoffSlots
uint32_t m_backoffSlots
the number of backoff slots
Definition: txop.h:339
ns3::Txop::NotifyOn
virtual void NotifyOn(void)
When on operation occurs, channel access will be started.
Definition: txop.cc:407
ns3::Txop::GetMinCw
virtual uint32_t GetMinCw(void) const
Return the minimum contention window size.
Definition: txop.cc:257
ns3::Txop::StartAccessIfNeeded
virtual void StartAccessIfNeeded(void)
Request access from Txop if needed.
Definition: txop.cc:312
ns3::Txop::REQUESTED
@ REQUESTED
Definition: txop.h:91
max
#define max(a, b)
Definition: 80211b.c:43
ns3::Time::GetMicroSeconds
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:388
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
ns3::Callback::Bind
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1329
ns3::Txop::Txop
Txop()
Definition: txop.cc:85
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::Txop::NotifySleep
virtual void NotifySleep(void)
When sleep operation occurs, if there is a pending packet transmission, it will be reinserted to the ...
Definition: txop.cc:387
ns3::Txop::GetWifiMacQueue
Ptr< WifiMacQueue > GetWifiMacQueue() const
Return the packet queue associated with this Txop.
Definition: txop.cc:149
ns3::AC_BE_NQOS
@ AC_BE_NQOS
Total number of ACs.
Definition: qos-utils.h:81
ns3::Txop::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: txop.cc:43
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::Txop::IsQosTxop
virtual bool IsQosTxop() const
Check for QoS TXOP.
Definition: txop.cc:414
NS_ASSERT_MSG
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
ns3::Txop::UpdateBackoffSlotsNow
void UpdateBackoffSlotsNow(uint32_t nSlots, Time backoffUpdateBound)
Update backoff slots that nSlots has passed.
Definition: txop.cc:217
ns3::WIFI_MAC_DROP_FAILED_ENQUEUE
@ WIFI_MAC_DROP_FAILED_ENQUEUE
Definition: wifi-mac.h:56
ns3::Txop::GetMaxCw
virtual uint32_t GetMaxCw(void) const
Return the maximum contention window size.
Definition: txop.cc:263
ns3::MakePointerAccessor
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: pointer.h:227
ns3::Txop::m_droppedMpduCallback
DroppedMpdu m_droppedMpduCallback
the dropped MPDU callback
Definition: txop.h:328
ns3::SocketPriorityTag
indicates whether the socket has a priority set.
Definition: socket.h:1309
ns3::Txop::m_txopLimit
Time m_txopLimit
the TXOP limit time
Definition: txop.h:348
ns3::Txop::DoDispose
void DoDispose(void) override
Destructor implementation.
Definition: txop.cc:106
ns3::Txop::ChannelAccessStatus
ChannelAccessStatus
Enumeration for channel access status.
Definition: txop.h:89
ns3::Txop::~Txop
virtual ~Txop()
Definition: txop.cc:100
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::Txop::SetWifiMac
virtual void SetWifiMac(const Ptr< RegularWifiMac > mac)
Set the wifi MAC this Txop is associated to.
Definition: txop.cc:131
ns3::Txop::DoInitialize
void DoInitialize(void) override
Initialize() implementation.
Definition: txop.cc:322
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Packet::RemovePacketTag
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::Txop::m_backoffTrace
TracedCallback< uint32_t > m_backoffTrace
backoff trace value
Definition: txop.h:350
ns3::AcIndex
AcIndex
This enumeration defines the Access Categories as an enumeration with values corresponding to the AC ...
Definition: qos-utils.h:71
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::Txop::SetTxopLimit
void SetTxopLimit(Time txopLimit)
Set the TXOP limit.
Definition: txop.cc:249
ns3::Txop::GetBackoffStart
Time GetBackoffStart(void) const
Return the time when the backoff procedure started.
Definition: txop.cc:211
ns3::Txop::GetAifsn
virtual uint8_t GetAifsn(void) const
Return the number of slots that make up an AIFS.
Definition: txop.cc:269
ns3::Txop::NotifyAccessRequested
virtual void NotifyAccessRequested(void)
Notify that access request has been received.
Definition: txop.cc:336
ns3::Txop::GetCw
uint32_t GetCw(void) const
Definition: txop.cc:180
ns3::Txop::m_access
ChannelAccessStatus m_access
channel access status
Definition: txop.h:338
ns3::RandomVariableStream::SetStream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Definition: random-variable-stream.cc:100
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Txop::HasFramesToTransmit
virtual bool HasFramesToTransmit(void)
Check if the Txop has frames to transmit.
Definition: txop.cc:281
ns3::MakeUintegerAccessor
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:45
ns3::Txop::RequestAccess
void RequestAccess(void)
Request access to the ChannelAccessManager.
Definition: txop.cc:362
ns3::Txop::SetTxMiddle
void SetTxMiddle(const Ptr< MacTxMiddle > txMiddle)
Set MacTxMiddle this Txop is associated to.
Definition: txop.cc:124
ns3::Txop::m_backoffStart
Time m_backoffStart
the backoffStart variable is used to keep track of the time at which a backoff was started or the tim...
Definition: txop.h:345
wifi-mac-trailer.h
ns3::Txop::NOT_REQUESTED
@ NOT_REQUESTED
Definition: txop.h:90
ns3::Txop::Queue
virtual void Queue(Ptr< Packet > packet, const WifiMacHeader &hdr)
Definition: txop.cc:289
channel-access-manager.h
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
ns3::MakeTimeAccessor
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:1354
ns3::Txop::SetAifsn
void SetAifsn(uint8_t aifsn)
Set the number of slots that make up an AIFS.
Definition: txop.cc:242
ns3::Txop::GetBackoffSlots
uint32_t GetBackoffSlots(void) const
Return the current number of backoff slots.
Definition: txop.cc:205
ns3::Txop
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:66