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 
87 {
88 }
89 
91  : m_channelAccessManager (0),
92  m_queue (queue),
93  m_cwMin (0),
94  m_cwMax (0),
95  m_cw (0),
96  m_backoff (0),
97  m_access (NOT_REQUESTED),
98  m_backoffSlots (0),
99  m_backoffStart (Seconds (0.0))
100 {
101  NS_LOG_FUNCTION (this);
102  m_rng = CreateObject<UniformRandomVariable> ();
103 }
104 
106 {
107  NS_LOG_FUNCTION (this);
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION (this);
114  m_queue = 0;
115  m_mac = 0;
116  m_rng = 0;
117  m_txMiddle = 0;
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this << manager);
125  m_channelAccessManager = manager;
126  m_channelAccessManager->Add (this);
127 }
128 
129 void Txop::SetTxMiddle (const Ptr<MacTxMiddle> txMiddle)
130 {
131  NS_LOG_FUNCTION (this);
132  m_txMiddle = txMiddle;
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION (this << mac);
139  m_mac = mac;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this << &callback);
146  m_droppedMpduCallback = callback;
147  m_queue->TraceConnectWithoutContext ("DropBeforeEnqueue",
149  m_queue->TraceConnectWithoutContext ("Expired",
151 }
152 
155 {
156  NS_LOG_FUNCTION (this);
157  return m_queue;
158 }
159 
160 void
161 Txop::SetMinCw (uint32_t minCw)
162 {
163  NS_LOG_FUNCTION (this << minCw);
164  bool changed = (m_cwMin != minCw);
165  m_cwMin = minCw;
166  if (changed == true)
167  {
168  ResetCw ();
169  }
170 }
171 
172 void
173 Txop::SetMaxCw (uint32_t maxCw)
174 {
175  NS_LOG_FUNCTION (this << maxCw);
176  bool changed = (m_cwMax != maxCw);
177  m_cwMax = maxCw;
178  if (changed == true)
179  {
180  ResetCw ();
181  }
182 }
183 
184 uint32_t
185 Txop::GetCw (void) const
186 {
187  return m_cw;
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this);
194  m_cw = GetMinCw ();
195  m_cwTrace = m_cw;
196 }
197 
198 void
200 {
201  NS_LOG_FUNCTION (this);
202  //see 802.11-2012, section 9.19.2.5
203  m_cw = std::min ( 2 * (m_cw + 1) - 1, GetMaxCw ());
204  // if the MU EDCA timer is running, CW cannot be less than MU CW min
205  m_cw = std::max (m_cw, GetMinCw ());
206  m_cwTrace = m_cw;
207 }
208 
209 uint32_t
211 {
212  return m_backoffSlots;
213 }
214 
215 Time
217 {
218  return m_backoffStart;
219 }
220 
221 void
222 Txop::UpdateBackoffSlotsNow (uint32_t nSlots, Time backoffUpdateBound)
223 {
224  NS_LOG_FUNCTION (this << nSlots << backoffUpdateBound);
225  m_backoffSlots -= nSlots;
226  m_backoffStart = backoffUpdateBound;
227  NS_LOG_DEBUG ("update slots=" << nSlots << " slots, backoff=" << m_backoffSlots);
228 }
229 
230 void
231 Txop::StartBackoffNow (uint32_t nSlots)
232 {
233  NS_LOG_FUNCTION (this << nSlots);
234  if (m_backoffSlots != 0)
235  {
236  NS_LOG_DEBUG ("reset backoff from " << m_backoffSlots << " to " << nSlots << " slots");
237  }
238  else
239  {
240  NS_LOG_DEBUG ("start backoff=" << nSlots << " slots");
241  }
242  m_backoffSlots = nSlots;
244 }
245 
246 void
247 Txop::SetAifsn (uint8_t aifsn)
248 {
249  NS_LOG_FUNCTION (this << +aifsn);
250  m_aifsn = aifsn;
251 }
252 
253 void
255 {
256  NS_LOG_FUNCTION (this << txopLimit);
257  NS_ASSERT_MSG ((txopLimit.GetMicroSeconds () % 32 == 0), "The TXOP limit must be expressed in multiple of 32 microseconds!");
258  m_txopLimit = txopLimit;
259 }
260 
261 uint32_t
262 Txop::GetMinCw (void) const
263 {
264  return m_cwMin;
265 }
266 
267 uint32_t
268 Txop::GetMaxCw (void) const
269 {
270  return m_cwMax;
271 }
272 
273 uint8_t
274 Txop::GetAifsn (void) const
275 {
276  return m_aifsn;
277 }
278 
279 Time
280 Txop::GetTxopLimit (void) const
281 {
282  return m_txopLimit;
283 }
284 
285 bool
287 {
288  bool ret = (!m_queue->IsEmpty ());
289  NS_LOG_FUNCTION (this << ret);
290  return ret;
291 }
292 
293 void
295 {
296  NS_LOG_FUNCTION (this << packet << &hdr);
297  // remove the priority tag attached, if any
298  SocketPriorityTag priorityTag;
299  packet->RemovePacketTag (priorityTag);
300  if (m_channelAccessManager->NeedBackoffUponAccess (this))
301  {
302  GenerateBackoff ();
303  }
304  m_queue->Enqueue (Create<WifiMacQueueItem> (packet, hdr));
306 }
307 
308 int64_t
309 Txop::AssignStreams (int64_t stream)
310 {
311  NS_LOG_FUNCTION (this << stream);
312  m_rng->SetStream (stream);
313  return 1;
314 }
315 
316 void
318 {
319  NS_LOG_FUNCTION (this);
321  {
322  m_channelAccessManager->RequestAccess (this);
323  }
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this);
330  ResetCw ();
331  GenerateBackoff ();
332 }
333 
336 {
337  return m_access;
338 }
339 
340 void
342 {
343  NS_LOG_FUNCTION (this);
345 }
346 
347 void
349 {
350  NS_LOG_FUNCTION (this << txopDuration);
351  m_access = GRANTED;
352 }
353 
354 void
356 {
357  NS_LOG_FUNCTION (this);
359  GenerateBackoff ();
360  if (HasFramesToTransmit ())
361  {
363  }
364 }
365 
366 void
368 {
369  if (m_access == NOT_REQUESTED)
370  {
371  m_channelAccessManager->RequestAccess (this);
372  }
373 }
374 
375 void
377 {
378  NS_LOG_FUNCTION (this);
379  m_backoff = m_rng->GetInteger (0, GetCw ());
382 }
383 
384 void
386 {
387  NS_LOG_FUNCTION (this);
388  m_queue->Flush ();
389 }
390 
391 void
393 {
394  NS_LOG_FUNCTION (this);
395 }
396 
397 void
399 {
400  NS_LOG_FUNCTION (this);
401  m_queue->Flush ();
402 }
403 
404 void
406 {
407  NS_LOG_FUNCTION (this);
409 }
410 
411 void
413 {
414  NS_LOG_FUNCTION (this);
416 }
417 
418 bool
420 {
421  return false;
422 }
423 
424 } //namespace ns3
ns3::Txop::SetDroppedMpduCallback
virtual void SetDroppedMpduCallback(DroppedMpdu callback)
Definition: txop.cc:143
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:328
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:334
min
#define min(a, b)
Definition: 80211b.c:42
ns3::WifiMacQueue
This queue implements the timeout procedure described in (Section 9.19.2.6 "Retransmit procedures" pa...
Definition: wifi-mac-queue.h:61
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:355
ns3::Txop::SetMaxCw
void SetMaxCw(uint32_t maxCw)
Set the maximum contention window size.
Definition: txop.cc:173
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:122
ns3::Txop::m_cwTrace
TracedValue< uint32_t > m_cwTrace
CW trace value.
Definition: txop.h:349
ns3::Txop::m_channelAccessManager
Ptr< ChannelAccessManager > m_channelAccessManager
the channel access manager
Definition: txop.h:325
ns3::Txop::GetAccessStatus
virtual ChannelAccessStatus GetAccessStatus(void) const
Definition: txop.cc:335
ns3::Txop::m_backoff
uint32_t m_backoff
the current backoff
Definition: txop.h:335
ns3::Txop::NotifyWakeUp
virtual void NotifyWakeUp(void)
When wake up operation occurs, channel access will be restarted.
Definition: txop.cc:405
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:191
ns3::WIFI_MAC_DROP_EXPIRED_LIFETIME
@ WIFI_MAC_DROP_EXPIRED_LIFETIME
Definition: wifi-mac.h:57
ns3::Txop::GRANTED
@ GRANTED
Definition: txop.h:96
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:332
ns3::Txop::m_cwMax
uint32_t m_cwMax
the maximum contention window
Definition: txop.h:333
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:309
ns3::Txop::StartBackoffNow
void StartBackoffNow(uint32_t nSlots)
Definition: txop.cc:231
ns3::Txop::m_rng
Ptr< UniformRandomVariable > m_rng
the random stream
Definition: txop.h:330
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:280
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:348
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:199
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:385
ns3::Txop::m_mac
Ptr< RegularWifiMac > m_mac
the wifi MAC
Definition: txop.h:329
ns3::Txop::SetMinCw
void SetMinCw(uint32_t minCw)
Set the minimum contention window size.
Definition: txop.cc:161
ns3::Txop::GenerateBackoff
virtual void GenerateBackoff(void)
Generate a new backoff now.
Definition: txop.cc:376
ns3::Txop::m_queue
Ptr< WifiMacQueue > m_queue
the wifi MAC queue
Definition: txop.h:327
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:398
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:345
ns3::Txop::m_backoffSlots
uint32_t m_backoffSlots
the number of backoff slots
Definition: txop.h:337
ns3::Txop::NotifyOn
virtual void NotifyOn(void)
When on operation occurs, channel access will be started.
Definition: txop.cc:412
ns3::Txop::GetMinCw
virtual uint32_t GetMinCw(void) const
Return the minimum contention window size.
Definition: txop.cc:262
ns3::Txop::StartAccessIfNeeded
virtual void StartAccessIfNeeded(void)
Request access from Txop if needed.
Definition: txop.cc:317
ns3::Txop::REQUESTED
@ REQUESTED
Definition: txop.h:95
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:392
ns3::Txop::GetWifiMacQueue
Ptr< WifiMacQueue > GetWifiMacQueue() const
Return the packet queue associated with this Txop.
Definition: txop.cc:154
ns3::AC_BE_NQOS
@ AC_BE_NQOS
Non-QoS.
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:419
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:222
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:268
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:326
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:346
ns3::Txop::DoDispose
void DoDispose(void) override
Destructor implementation.
Definition: txop.cc:111
ns3::Txop::ChannelAccessStatus
ChannelAccessStatus
Enumeration for channel access status.
Definition: txop.h:93
ns3::Txop::~Txop
virtual ~Txop()
Definition: txop.cc:105
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:136
ns3::Txop::DoInitialize
void DoInitialize(void) override
Initialize() implementation.
Definition: txop.cc:327
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:348
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:254
ns3::Txop::GetBackoffStart
Time GetBackoffStart(void) const
Return the time when the backoff procedure started.
Definition: txop.cc:216
ns3::Txop::GetAifsn
virtual uint8_t GetAifsn(void) const
Return the number of slots that make up an AIFS.
Definition: txop.cc:274
ns3::Txop::NotifyAccessRequested
virtual void NotifyAccessRequested(void)
Notify that access request has been received.
Definition: txop.cc:341
ns3::Txop::GetCw
uint32_t GetCw(void) const
Definition: txop.cc:185
ns3::Txop::m_access
ChannelAccessStatus m_access
channel access status
Definition: txop.h:336
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:286
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:367
ns3::Txop::SetTxMiddle
void SetTxMiddle(const Ptr< MacTxMiddle > txMiddle)
Set MacTxMiddle this Txop is associated to.
Definition: txop.cc:129
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
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:343
wifi-mac-trailer.h
ns3::Txop::NOT_REQUESTED
@ NOT_REQUESTED
Definition: txop.h:94
ns3::Txop::Queue
virtual void Queue(Ptr< Packet > packet, const WifiMacHeader &hdr)
Definition: txop.cc:294
channel-access-manager.h
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
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:247
ns3::Txop::GetBackoffSlots
uint32_t GetBackoffSlots(void) const
Return the current number of backoff slots.
Definition: txop.cc:210
ns3::Txop
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:65