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 "wifi-mac.h"
29 #include "wifi-mac-queue.h"
30 #include "mac-tx-middle.h"
32 #include "wifi-mac-trailer.h"
33 
34 #undef NS_LOG_APPEND_CONTEXT
35 #define NS_LOG_APPEND_CONTEXT if (m_stationManager != 0 && m_stationManager->GetMac () != 0) { std::clog << "[mac=" << m_stationManager->GetMac ()->GetAddress () << "] "; }
36 
37 namespace ns3 {
38 
40 
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::Txop")
48  .SetGroupName ("Wifi")
49  .AddConstructor<Txop> ()
50  .AddAttribute ("MinCw", "The minimum value of the contention window.",
51  UintegerValue (15),
54  MakeUintegerChecker<uint32_t> ())
55  .AddAttribute ("MaxCw", "The maximum value of the contention window.",
56  UintegerValue (1023),
59  MakeUintegerChecker<uint32_t> ())
60  .AddAttribute ("Aifsn", "The AIFSN: the default value conforms to non-QOS.",
61  UintegerValue (2),
64  MakeUintegerChecker<uint8_t> ())
65  .AddAttribute ("TxopLimit", "The TXOP limit: the default value conforms to non-QoS.",
66  TimeValue (MilliSeconds (0)),
69  MakeTimeChecker ())
70  .AddAttribute ("Queue", "The WifiMacQueue object",
71  PointerValue (),
73  MakePointerChecker<WifiMacQueue> ())
74  .AddTraceSource ("BackoffTrace",
75  "Trace source for backoff values",
77  "ns3::TracedCallback::Uint32Callback")
78  .AddTraceSource ("CwTrace",
79  "Trace source for contention window values",
81  "ns3::TracedValueCallback::Uint32")
82  ;
83  return tid;
84 }
85 
87  : m_channelAccessManager (0),
88  m_cwMin (0),
89  m_cwMax (0),
90  m_cw (0),
91  m_backoff (0),
92  m_access (NOT_REQUESTED),
93  m_backoffSlots (0),
94  m_backoffStart (Seconds (0.0))
95 {
96  NS_LOG_FUNCTION (this);
97  m_queue = CreateObject<WifiMacQueue> ();
98  m_rng = CreateObject<UniformRandomVariable> ();
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION (this);
110  m_queue = 0;
111  m_stationManager = 0;
112  m_rng = 0;
113  m_txMiddle = 0;
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this << manager);
121  m_channelAccessManager = manager;
122  m_channelAccessManager->Add (this);
123 }
124 
125 void Txop::SetTxMiddle (const Ptr<MacTxMiddle> txMiddle)
126 {
127  NS_LOG_FUNCTION (this);
128  m_txMiddle = txMiddle;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this << remoteManager);
135  m_stationManager = remoteManager;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << &callback);
142  m_droppedMpduCallback = callback;
143  m_queue->TraceConnectWithoutContext ("DropBeforeEnqueue",
145  m_queue->TraceConnectWithoutContext ("Expired",
147 }
148 
151 {
152  NS_LOG_FUNCTION (this);
153  return m_queue;
154 }
155 
156 void
157 Txop::SetMinCw (uint32_t minCw)
158 {
159  NS_LOG_FUNCTION (this << minCw);
160  bool changed = (m_cwMin != minCw);
161  m_cwMin = minCw;
162  if (changed == true)
163  {
164  ResetCw ();
165  }
166 }
167 
168 void
169 Txop::SetMaxCw (uint32_t maxCw)
170 {
171  NS_LOG_FUNCTION (this << maxCw);
172  bool changed = (m_cwMax != maxCw);
173  m_cwMax = maxCw;
174  if (changed == true)
175  {
176  ResetCw ();
177  }
178 }
179 
180 uint32_t
181 Txop::GetCw (void) const
182 {
183  return m_cw;
184 }
185 
186 void
188 {
189  NS_LOG_FUNCTION (this);
190  m_cw = m_cwMin;
191  m_cwTrace = m_cw;
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this);
198  //see 802.11-2012, section 9.19.2.5
199  m_cw = std::min ( 2 * (m_cw + 1) - 1, m_cwMax);
200  m_cwTrace = m_cw;
201 }
202 
203 uint32_t
205 {
206  return m_backoffSlots;
207 }
208 
209 Time
211 {
212  return m_backoffStart;
213 }
214 
215 void
216 Txop::UpdateBackoffSlotsNow (uint32_t nSlots, Time backoffUpdateBound)
217 {
218  NS_LOG_FUNCTION (this << nSlots << backoffUpdateBound);
219  m_backoffSlots -= nSlots;
220  m_backoffStart = backoffUpdateBound;
221  NS_LOG_DEBUG ("update slots=" << nSlots << " slots, backoff=" << m_backoffSlots);
222 }
223 
224 void
225 Txop::StartBackoffNow (uint32_t nSlots)
226 {
227  NS_LOG_FUNCTION (this << nSlots);
228  if (m_backoffSlots != 0)
229  {
230  NS_LOG_DEBUG ("reset backoff from " << m_backoffSlots << " to " << nSlots << " slots");
231  }
232  else
233  {
234  NS_LOG_DEBUG ("start backoff=" << nSlots << " slots");
235  }
236  m_backoffSlots = nSlots;
238 }
239 
240 void
241 Txop::SetAifsn (uint8_t aifsn)
242 {
243  NS_LOG_FUNCTION (this << +aifsn);
244  m_aifsn = aifsn;
245 }
246 
247 void
249 {
250  NS_LOG_FUNCTION (this << txopLimit);
251  NS_ASSERT_MSG ((txopLimit.GetMicroSeconds () % 32 == 0), "The TXOP limit must be expressed in multiple of 32 microseconds!");
252  m_txopLimit = txopLimit;
253 }
254 
255 uint32_t
256 Txop::GetMinCw (void) const
257 {
258  return m_cwMin;
259 }
260 
261 uint32_t
262 Txop::GetMaxCw (void) const
263 {
264  return m_cwMax;
265 }
266 
267 uint8_t
268 Txop::GetAifsn (void) const
269 {
270  return m_aifsn;
271 }
272 
273 Time
274 Txop::GetTxopLimit (void) const
275 {
276  return m_txopLimit;
277 }
278 
279 bool
281 {
282  bool ret = (!m_queue->IsEmpty ());
283  NS_LOG_FUNCTION (this << ret);
284  return ret;
285 }
286 
287 void
289 {
290  NS_LOG_FUNCTION (this << packet << &hdr);
291  // remove the priority tag attached, if any
292  SocketPriorityTag priorityTag;
293  packet->RemovePacketTag (priorityTag);
294  if (m_channelAccessManager->NeedBackoffUponAccess (this))
295  {
296  GenerateBackoff ();
297  }
298  m_queue->Enqueue (Create<WifiMacQueueItem> (packet, hdr));
300 }
301 
302 int64_t
303 Txop::AssignStreams (int64_t stream)
304 {
305  NS_LOG_FUNCTION (this << stream);
306  m_rng->SetStream (stream);
307  return 1;
308 }
309 
310 void
312 {
313  NS_LOG_FUNCTION (this);
315  {
316  m_channelAccessManager->RequestAccess (this);
317  }
318 }
319 
320 void
322 {
323  NS_LOG_FUNCTION (this);
324  ResetCw ();
325  GenerateBackoff ();
326 }
327 
330 {
331  return m_access;
332 }
333 
334 void
336 {
337  NS_LOG_FUNCTION (this);
339 }
340 
341 void
343 {
344  NS_LOG_FUNCTION (this << txopDuration);
345  m_access = GRANTED;
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION (this);
353  GenerateBackoff ();
354  if (HasFramesToTransmit ())
355  {
357  }
358 }
359 
360 void
362 {
363  if (m_access == NOT_REQUESTED)
364  {
365  m_channelAccessManager->RequestAccess (this);
366  }
367 }
368 
369 void
371 {
372  NS_LOG_FUNCTION (this);
373  m_backoff = m_rng->GetInteger (0, GetCw ());
376 }
377 
378 void
380 {
381  NS_LOG_FUNCTION (this);
382  GenerateBackoff ();
384 }
385 
386 void
388 {
389  NS_LOG_FUNCTION (this);
390  m_queue->Flush ();
391 }
392 
393 void
395 {
396  NS_LOG_FUNCTION (this);
397 }
398 
399 void
401 {
402  NS_LOG_FUNCTION (this);
403  m_queue->Flush ();
404 }
405 
406 void
408 {
409  NS_LOG_FUNCTION (this);
411 }
412 
413 void
415 {
416  NS_LOG_FUNCTION (this);
418 }
419 
420 bool
422 {
423  return false;
424 }
425 
426 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint8_t m_aifsn
the AIFSN
Definition: txop.h:344
Ptr< MacTxMiddle > m_txMiddle
the MacTxMiddle
Definition: txop.h:327
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
TracedCallback< uint32_t > m_backoffTrace
backoff trace value
Definition: txop.h:347
TracedValue< uint32_t > m_cwTrace
CW trace value.
Definition: txop.h:348
Time m_txopLimit
the TXOP limit time
Definition: txop.h:345
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
virtual void StartAccessIfNeeded(void)
Request access from Txop if needed.
Definition: txop.cc:311
uint8_t GetAifsn(void) const
Return the number of slots that make up an AIFS.
Definition: txop.cc:268
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
#define min(a, b)
Definition: 80211b.c:42
uint32_t m_backoffSlots
the number of backoff slots
Definition: txop.h:336
virtual bool HasFramesToTransmit(void)
Check if the Txop has frames to transmit.
Definition: txop.cc:280
uint32_t m_backoff
the current backoff
Definition: txop.h:334
void RequestAccess(void)
Request access to the ChannelAccessManager.
Definition: txop.cc:361
void SetChannelAccessManager(const Ptr< ChannelAccessManager > manager)
Set ChannelAccessManager this Txop is associated to.
Definition: txop.cc:118
Ptr< ChannelAccessManager > m_channelAccessManager
the channel access manager
Definition: txop.h:324
virtual void DoInitialize(void)
Initialize() implementation.
Definition: txop.cc:321
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
Time GetBackoffStart(void) const
Return the time when the backoff procedure started.
Definition: txop.cc:210
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1329
Ptr< UniformRandomVariable > m_rng
the random stream
Definition: txop.h:329
void UpdateBackoffSlotsNow(uint32_t nSlots, Time backoffUpdateBound)
Update backoff slots that nSlots has passed.
Definition: txop.cc:216
virtual void GenerateBackoff(void)
Generate a new backoff now.
Definition: txop.cc:370
virtual ChannelAccessStatus GetAccessStatus(void) const
Definition: txop.cc:329
uint32_t GetMinCw(void) const
Return the minimum contention window size.
Definition: txop.cc:256
uint32_t m_cwMax
the maximum contention window
Definition: txop.h:332
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void NotifyOff(void)
When off operation occurs, the queue gets cleaned up.
Definition: txop.cc:400
ChannelAccessStatus
Enumeration for channel access status.
Definition: txop.h:87
void UpdateFailedCw(void)
Update the value of the CW variable to take into account a transmission failure.
Definition: txop.cc:195
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
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
AttributeValue implementation for Time.
Definition: nstime.h:1353
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: txop.cc:303
Hold an unsigned integer type.
Definition: uinteger.h:44
indicates whether the socket has a priority set.
Definition: socket.h:1307
virtual void NotifyChannelReleased(void)
Called by the FrameExchangeManager to notify the completion of the transmissions. ...
Definition: txop.cc:349
static TypeId GetTypeId(void)
Get the type ID.
Definition: txop.cc:44
ChannelAccessStatus m_access
channel access status
Definition: txop.h:335
virtual bool IsQosTxop() const
Check for QoS TXOP.
Definition: txop.cc:421
uint32_t m_cwMin
the minimum contention window
Definition: txop.h:331
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:388
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t GetCw(void) const
Definition: txop.cc:181
Hold objects of type Ptr<T>.
Definition: pointer.h:36
virtual void NotifySleep(void)
When sleep operation occurs, if there is a pending packet transmission, it will be reinserted to the ...
Definition: txop.cc:394
uint32_t GetMaxCw(void) const
Return the maximum contention window size.
Definition: txop.cc:262
virtual void NotifyOn(void)
When on operation occurs, channel access will be started.
Definition: txop.cc:414
Ptr< WifiMacQueue > m_queue
the wifi MAC queue
Definition: txop.h:326
virtual void DoDispose(void)
Destructor implementation.
Definition: txop.cc:107
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
virtual void NotifyChannelSwitching(void)
When a channel switching occurs, enqueued packets are removed.
Definition: txop.cc:387
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
DroppedMpdu m_droppedMpduCallback
the dropped MPDU callback
Definition: txop.h:325
virtual void NotifyInternalCollision(void)
Notify the Txop that internal collision has occurred.
Definition: txop.cc:379
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:342
#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
virtual void SetDroppedMpduCallback(DroppedMpdu callback)
Definition: txop.cc:139
uint32_t m_cw
the current contention window
Definition: txop.h:333
Txop()
Definition: txop.cc:86
void SetMinCw(uint32_t minCw)
Set the minimum contention window size.
Definition: txop.cc:157
void SetMaxCw(uint32_t maxCw)
Set the maximum contention window size.
Definition: txop.cc:169
void SetTxopLimit(Time txopLimit)
Set the TXOP limit.
Definition: txop.cc:248
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:342
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
void SetTxMiddle(const Ptr< MacTxMiddle > txMiddle)
Set MacTxMiddle this Txop is associated to.
Definition: txop.cc:125
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
uint32_t GetBackoffSlots(void) const
Return the current number of backoff slots.
Definition: txop.cc:204
Ptr< WifiMacQueue > GetWifiMacQueue() const
Return the packet queue associated with this Txop.
Definition: txop.cc:150
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
virtual void SetWifiRemoteStationManager(const Ptr< WifiRemoteStationManager > remoteManager)
Set WifiRemoteStationsManager this Txop is associated to.
Definition: txop.cc:132
void SetAifsn(uint8_t aifsn)
Set the number of slots that make up an AIFS.
Definition: txop.cc:241
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual void NotifyAccessRequested(void)
Notify that access request has been received.
Definition: txop.cc:335
virtual ~Txop()
Definition: txop.cc:101
Ptr< WifiRemoteStationManager > m_stationManager
the wifi remote station manager
Definition: txop.h:328
virtual void Queue(Ptr< Packet > packet, const WifiMacHeader &hdr)
Definition: txop.cc:288
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
void ResetCw(void)
Update the value of the CW variable to take into account a transmission success or a transmission abo...
Definition: txop.cc:187
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
void StartBackoffNow(uint32_t nSlots)
Definition: txop.cc:225
virtual void NotifyWakeUp(void)
When wake up operation occurs, channel access will be restarted.
Definition: txop.cc:407
Implements the IEEE 802.11 MAC header.
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:64
Time GetTxopLimit(void) const
Return the TXOP limit.
Definition: txop.cc:274