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"
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 }
389 
390 void
392 {
393  NS_LOG_FUNCTION (this);
394  m_queue->Flush ();
395 }
396 
397 void
399 {
400  NS_LOG_FUNCTION (this);
402 }
403 
404 void
406 {
407  NS_LOG_FUNCTION (this);
409 }
410 
411 bool
413 {
414  return false;
415 }
416 
417 } //namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1329
A base class which provides memory management and object aggregation.
Definition: object.h:88
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
indicates whether the socket has a priority set.
Definition: socket.h:1309
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:387
AttributeValue implementation for Time.
Definition: nstime.h:1308
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:65
Ptr< WifiMac > m_mac
the wifi MAC
Definition: txop.h:325
void DoInitialize(void) override
Initialize() implementation.
Definition: txop.cc:327
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
virtual ChannelAccessStatus GetAccessStatus(void) const
Definition: txop.cc:335
void UpdateFailedCw(void)
Update the value of the CW variable to take into account a transmission failure.
Definition: txop.cc:199
Ptr< WifiMacQueue > m_queue
the wifi MAC queue
Definition: txop.h:323
virtual void NotifyChannelReleased(void)
Called by the FrameExchangeManager to notify the completion of the transmissions.
Definition: txop.cc:355
Ptr< UniformRandomVariable > m_rng
the random stream
Definition: txop.h:326
uint32_t m_backoff
the current backoff
Definition: txop.h:331
uint32_t GetCw(void) const
Definition: txop.cc:185
void SetMaxCw(uint32_t maxCw)
Set the maximum contention window size.
Definition: txop.cc:173
virtual void NotifySleep(void)
When sleep operation occurs, if there is a pending packet transmission, it will be reinserted to the ...
Definition: txop.cc:385
uint32_t m_cw
the current contention window
Definition: txop.h:330
ChannelAccessStatus m_access
channel access status
Definition: txop.h:332
TracedValue< uint32_t > m_cwTrace
CW trace value.
Definition: txop.h:345
ChannelAccessStatus
Enumeration for channel access status.
Definition: txop.h:93
@ GRANTED
Definition: txop.h:96
@ NOT_REQUESTED
Definition: txop.h:94
@ REQUESTED
Definition: txop.h:95
Ptr< WifiMacQueue > GetWifiMacQueue() const
Return the packet queue associated with this Txop.
Definition: txop.cc:154
Time GetTxopLimit(void) const
Return the TXOP limit.
Definition: txop.cc:280
virtual void SetWifiMac(const Ptr< WifiMac > mac)
Set the wifi MAC this Txop is associated to.
Definition: txop.cc:136
TracedCallback< uint32_t > m_backoffTrace
backoff trace value
Definition: txop.h:344
uint8_t m_aifsn
the AIFSN
Definition: txop.h:341
virtual void NotifyAccessRequested(void)
Notify that access request has been received.
Definition: txop.cc:341
uint32_t m_cwMin
the minimum contention window
Definition: txop.h:328
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:339
void SetTxopLimit(Time txopLimit)
Set the TXOP limit.
Definition: txop.cc:254
virtual void NotifyOn(void)
When on operation occurs, channel access will be started.
Definition: txop.cc:405
static TypeId GetTypeId(void)
Get the type ID.
Definition: txop.cc:43
Txop()
Definition: txop.cc:85
virtual bool IsQosTxop() const
Check for QoS TXOP.
Definition: txop.cc:412
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
Time m_txopLimit
the TXOP limit time
Definition: txop.h:342
DroppedMpdu m_droppedMpduCallback
the dropped MPDU callback
Definition: txop.h:322
void SetTxMiddle(const Ptr< MacTxMiddle > txMiddle)
Set MacTxMiddle this Txop is associated to.
Definition: txop.cc:129
void SetAifsn(uint8_t aifsn)
Set the number of slots that make up an AIFS.
Definition: txop.cc:247
Ptr< ChannelAccessManager > m_channelAccessManager
the channel access manager
Definition: txop.h:321
virtual void SetDroppedMpduCallback(DroppedMpdu callback)
Definition: txop.cc:143
void UpdateBackoffSlotsNow(uint32_t nSlots, Time backoffUpdateBound)
Update backoff slots that nSlots has passed.
Definition: txop.cc:222
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
uint32_t m_backoffSlots
the number of backoff slots
Definition: txop.h:333
void RequestAccess(void)
Request access to the ChannelAccessManager.
Definition: txop.cc:367
Ptr< MacTxMiddle > m_txMiddle
the MacTxMiddle
Definition: txop.h:324
Time GetBackoffStart(void) const
Return the time when the backoff procedure started.
Definition: txop.cc:216
virtual ~Txop()
Definition: txop.cc:105
virtual uint32_t GetMaxCw(void) const
Return the maximum contention window size.
Definition: txop.cc:268
uint32_t m_cwMax
the maximum contention window
Definition: txop.h:329
virtual void NotifyWakeUp(void)
When wake up operation occurs, channel access will be restarted.
Definition: txop.cc:398
void StartBackoffNow(uint32_t nSlots)
Definition: txop.cc:231
virtual void NotifyOff(void)
When off operation occurs, the queue gets cleaned up.
Definition: txop.cc:391
void SetMinCw(uint32_t minCw)
Set the minimum contention window size.
Definition: txop.cc:161
uint32_t GetBackoffSlots(void) const
Return the current number of backoff slots.
Definition: txop.cc:210
void SetChannelAccessManager(const Ptr< ChannelAccessManager > manager)
Set ChannelAccessManager this Txop is associated to.
Definition: txop.cc:122
virtual void Queue(Ptr< Packet > packet, const WifiMacHeader &hdr)
Definition: txop.cc:294
virtual void GenerateBackoff(void)
Generate a new backoff now.
Definition: txop.cc:376
virtual bool HasFramesToTransmit(void)
Check if the Txop has frames to transmit.
Definition: txop.cc:286
virtual uint32_t GetMinCw(void) const
Return the minimum contention window size.
Definition: txop.cc:262
virtual uint8_t GetAifsn(void) const
Return the number of slots that make up an AIFS.
Definition: txop.cc:274
void DoDispose(void) override
Destructor implementation.
Definition: txop.cc:111
virtual void StartAccessIfNeeded(void)
Request access from Txop if needed.
Definition: txop.cc:317
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
Implements the IEEE 802.11 MAC header.
This queue implements the timeout procedure described in (Section 9.19.2.6 "Retransmit procedures" pa...
#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
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
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:1309
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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
@ AC_BE_NQOS
Non-QoS.
Definition: qos-utils.h:81
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:522
@ WIFI_MAC_DROP_FAILED_ENQUEUE
Definition: wifi-mac.h:67
@ WIFI_MAC_DROP_EXPIRED_LIFETIME
Definition: wifi-mac.h:68
mac
Definition: third.py:99