A Discrete-Event Network Simulator
API
mac-rx-middle.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/sequence-number.h"
23 #include "ns3/packet.h"
24 #include "mac-rx-middle.h"
25 #include "wifi-mac-queue-item.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("MacRxMiddle");
30 
36 {
37 private:
41  typedef std::list<Ptr<const Packet> > Fragments;
45  typedef std::list<Ptr<const Packet> >::const_iterator FragmentsCI;
46 
50 
51 
52 public:
54  {
55  /* this is a magic value necessary. */
56  m_lastSequenceControl = 0xffff;
57  m_defragmenting = false;
58  }
60  {
61  m_fragments.clear ();
62  }
69  bool IsDeFragmenting (void) const
70  {
71  return m_defragmenting;
72  }
80  {
82  m_defragmenting = true;
83  m_fragments.push_back (packet);
84  }
96  {
98  m_fragments.push_back (packet);
99  m_defragmenting = false;
100  Ptr<Packet> full = Create<Packet> ();
101  for (FragmentsCI i = m_fragments.begin (); i != m_fragments.end (); i++)
102  {
103  full->AddAtEnd (*i);
104  }
105  m_fragments.erase (m_fragments.begin (), m_fragments.end ());
106  return full;
107  }
115  {
117  m_fragments.push_back (packet);
118  }
128  bool IsNextFragment (uint16_t sequenceControl) const
129  {
130  if ((sequenceControl >> 4) == (m_lastSequenceControl >> 4)
131  && (sequenceControl & 0x0f) == ((m_lastSequenceControl & 0x0f) + 1))
132  {
133  return true;
134  }
135  else
136  {
137  return false;
138  }
139  }
145  uint16_t GetLastSequenceControl (void) const
146  {
147  return m_lastSequenceControl;
148  }
154  void SetSequenceControl (uint16_t sequenceControl)
155  {
156  m_lastSequenceControl = sequenceControl;
157  }
158 };
159 
160 
162 {
164 }
165 
167 {
169  for (OriginatorsI i = m_originatorStatus.begin ();
170  i != m_originatorStatus.end (); i++)
171  {
172  delete (*i).second;
173  }
174  m_originatorStatus.erase (m_originatorStatus.begin (),
175  m_originatorStatus.end ());
176  for (QosOriginatorsI i = m_qosOriginatorStatus.begin ();
177  i != m_qosOriginatorStatus.end (); i++)
178  {
179  delete (*i).second;
180  }
182  m_qosOriginatorStatus.end ());
183 }
184 
185 void
187 {
189  m_callback = callback;
190 }
191 
194 {
195  NS_LOG_FUNCTION (hdr);
196  OriginatorRxStatus *originator;
197  Mac48Address source = hdr->GetAddr2 ();
198  if (hdr->IsQosData ()
199  && !hdr->GetAddr2 ().IsGroup ())
200  {
201  /* only for QoS data non-broadcast frames */
202  originator = m_qosOriginatorStatus[std::make_pair (source, hdr->GetQosTid ())];
203  if (originator == 0)
204  {
205  originator = new OriginatorRxStatus ();
206  m_qosOriginatorStatus[std::make_pair (source, hdr->GetQosTid ())] = originator;
207  }
208  }
209  else
210  {
211  /* - management frames
212  * - QoS data broadcast frames
213  * - non-QoS data frames
214  * see section 7.1.3.4.1
215  */
216  originator = m_originatorStatus[source];
217  if (originator == 0)
218  {
219  originator = new OriginatorRxStatus ();
220  m_originatorStatus[source] = originator;
221  }
222  }
223  return originator;
224 }
225 
226 bool
228  OriginatorRxStatus *originator) const
229 {
230  NS_LOG_FUNCTION (hdr << originator);
231  if (hdr->IsRetry ()
232  && originator->GetLastSequenceControl () == hdr->GetSequenceControl ())
233  {
234  return true;
235  }
236  return false;
237 }
238 
241  OriginatorRxStatus *originator)
242 {
243  NS_LOG_FUNCTION (packet << hdr << originator);
244  if (originator->IsDeFragmenting ())
245  {
246  if (hdr->IsMoreFragments ())
247  {
248  if (originator->IsNextFragment (hdr->GetSequenceControl ()))
249  {
250  NS_LOG_DEBUG ("accumulate fragment seq=" << hdr->GetSequenceNumber () <<
251  ", frag=" << +hdr->GetFragmentNumber () <<
252  ", size=" << packet->GetSize ());
253  originator->AccumulateFragment (packet);
254  originator->SetSequenceControl (hdr->GetSequenceControl ());
255  }
256  else
257  {
258  NS_LOG_DEBUG ("non-ordered fragment");
259  }
260  return 0;
261  }
262  else
263  {
264  if (originator->IsNextFragment (hdr->GetSequenceControl ()))
265  {
266  NS_LOG_DEBUG ("accumulate last fragment seq=" << hdr->GetSequenceNumber () <<
267  ", frag=" << +hdr->GetFragmentNumber () <<
268  ", size=" << hdr->GetSize ());
269  Ptr<Packet> p = originator->AccumulateLastFragment (packet);
270  originator->SetSequenceControl (hdr->GetSequenceControl ());
271  return p;
272  }
273  else
274  {
275  NS_LOG_DEBUG ("non-ordered fragment");
276  return 0;
277  }
278  }
279  }
280  else
281  {
282  if (hdr->IsMoreFragments ())
283  {
284  NS_LOG_DEBUG ("accumulate first fragment seq=" << hdr->GetSequenceNumber () <<
285  ", frag=" << +hdr->GetFragmentNumber () <<
286  ", size=" << packet->GetSize ());
287  originator->AccumulateFirstFragment (packet);
288  originator->SetSequenceControl (hdr->GetSequenceControl ());
289  return 0;
290  }
291  else
292  {
293  return packet;
294  }
295  }
296 }
297 
298 void
300 {
301  NS_LOG_FUNCTION (*mpdu);
302  const WifiMacHeader* hdr = &mpdu->GetHeader ();
303  NS_ASSERT (hdr->IsData () || hdr->IsMgt ());
304 
305  OriginatorRxStatus *originator = Lookup (hdr);
316  {
317  NS_LOG_DEBUG ("Sequence numbers have looped back. last recorded=" << originator->GetLastSequenceControl () <<
318  " currently seen=" << hdr->GetSequenceControl ());
319  }
320  //filter duplicates.
321  if (IsDuplicate (hdr, originator))
322  {
323  NS_LOG_DEBUG ("duplicate from=" << hdr->GetAddr2 () <<
324  ", seq=" << hdr->GetSequenceNumber () <<
325  ", frag=" << +hdr->GetFragmentNumber ());
326  return;
327  }
328  Ptr<const Packet> aggregate = HandleFragments (mpdu->GetPacket (), hdr, originator);
329  if (aggregate == 0)
330  {
331  return;
332  }
333  NS_LOG_DEBUG ("forwarding data from=" << hdr->GetAddr2 () <<
334  ", seq=" << hdr->GetSequenceNumber () <<
335  ", frag=" << +hdr->GetFragmentNumber ());
336  if (!hdr->GetAddr1 ().IsGroup ())
337  {
338  originator->SetSequenceControl (hdr->GetSequenceControl ());
339  }
340  if (aggregate == mpdu->GetPacket ())
341  {
342  m_callback (mpdu);
343  }
344  else
345  {
346  // We could do this in all cases, but passing the received mpdu in case of
347  // A-MSDUs saves us the time to deaggregate the A-MSDU in MSDUs (which are
348  // kept separate in the received mpdu) and allows us to pass the originally
349  // transmitted packets (i.e., with the same UID) to the receiver.
350  m_callback (Create<WifiMacQueueItem> (aggregate, *hdr));
351  }
352 }
353 
354 } //namespace ns3
ns3::OriginatorRxStatus::SetSequenceControl
void SetSequenceControl(uint16_t sequenceControl)
Set the last sequence control we received.
Definition: mac-rx-middle.cc:154
ns3::OriginatorRxStatus::GetLastSequenceControl
uint16_t GetLastSequenceControl(void) const
Return the last sequence control we received.
Definition: mac-rx-middle.cc:145
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::OriginatorRxStatus::AccumulateLastFragment
Ptr< Packet > AccumulateLastFragment(Ptr< const Packet > packet)
We have received a last fragment of the fragmented packets (indicated by the no more fragment field).
Definition: mac-rx-middle.cc:95
ns3::MacRxMiddle::IsDuplicate
bool IsDuplicate(const WifiMacHeader *hdr, OriginatorRxStatus *originator) const
Check if we have already received the packet from the sender before (by looking at the sequence contr...
Definition: mac-rx-middle.cc:227
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3::WifiMacQueueItem::GetPacket
Ptr< const Packet > GetPacket(void) const
Get the packet stored in this item.
Definition: wifi-mac-queue-item.cc:57
ns3::OriginatorRxStatus::~OriginatorRxStatus
~OriginatorRxStatus()
Definition: mac-rx-middle.cc:59
ns3::OriginatorRxStatus::m_defragmenting
bool m_defragmenting
flag to indicate whether we are defragmenting
Definition: mac-rx-middle.cc:47
ns3::WifiMacHeader::GetSize
uint32_t GetSize(void) const
Return the size of the WifiMacHeader in octets.
Definition: wifi-mac-header.cc:933
ns3::Mac48Address::IsGroup
bool IsGroup(void) const
Definition: mac48-address.cc:164
ns3::WifiMacHeader::IsData
bool IsData(void) const
Return true if the Type is DATA.
Definition: wifi-mac-header.cc:558
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::Callback< void, Ptr< WifiMacQueueItem > >
ns3::WifiMacHeader::IsRetry
bool IsRetry(void) const
Return if the Retry bit is set.
Definition: wifi-mac-header.cc:789
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MacRxMiddle::QosOriginatorsI
std::map< std::pair< Mac48Address, uint8_t >, OriginatorRxStatus *, std::less< std::pair< Mac48Address, uint8_t > > >::iterator QosOriginatorsI
typedef for an iterator for QosOriginators
Definition: mac-rx-middle.h:124
ns3::OriginatorRxStatus::Fragments
std::list< Ptr< const Packet > > Fragments
typedef for a list of fragments (i.e.
Definition: mac-rx-middle.cc:41
ns3::Mac48Address
an EUI-48 address
Definition: mac48-address.h:44
ns3::MacRxMiddle::OriginatorsI
std::map< Mac48Address, OriginatorRxStatus *, std::less< Mac48Address > >::iterator OriginatorsI
typedef for an iterator for Originators
Definition: mac-rx-middle.h:120
wifi-mac-queue-item.h
ns3::WifiMacHeader::GetAddr1
Mac48Address GetAddr1(void) const
Return the address in the Address 1 field.
Definition: wifi-mac-header.cc:424
ns3::OriginatorRxStatus::FragmentsCI
std::list< Ptr< const Packet > >::const_iterator FragmentsCI
typedef for a const iterator for Fragments
Definition: mac-rx-middle.cc:45
ns3::WifiMacHeader::GetSequenceControl
uint16_t GetSequenceControl(void) const
Return the raw Sequence Control field.
Definition: wifi-mac-header.cc:771
ns3::WifiMacQueueItem::GetHeader
const WifiMacHeader & GetHeader(void) const
Get the header stored in this item.
Definition: wifi-mac-queue-item.cc:63
ns3::MacRxMiddle::m_originatorStatus
Originators m_originatorStatus
originator status
Definition: mac-rx-middle.h:126
ns3::WifiMacHeader
Implements the IEEE 802.11 MAC header.
Definition: wifi-mac-header.h:85
ns3::Ptr< const Packet >
ns3::MacRxMiddle::SetForwardCallback
void SetForwardCallback(ForwardUpCallback callback)
Set a callback to forward the packet up.
Definition: mac-rx-middle.cc:186
ns3::OriginatorRxStatus::OriginatorRxStatus
OriginatorRxStatus()
Definition: mac-rx-middle.cc:53
ns3::OriginatorRxStatus::IsDeFragmenting
bool IsDeFragmenting(void) const
Check if we are de-fragmenting packets.
Definition: mac-rx-middle.cc:69
ns3::MacRxMiddle::HandleFragments
Ptr< const Packet > HandleFragments(Ptr< const Packet > packet, const WifiMacHeader *hdr, OriginatorRxStatus *originator)
Check if the received packet is a fragment and handle it appropriately.
Definition: mac-rx-middle.cc:240
ns3::WifiMacHeader::IsQosData
bool IsQosData(void) const
Return true if the Type is DATA and Subtype is one of the possible values for QoS Data.
Definition: wifi-mac-header.cc:565
mac-rx-middle.h
ns3::WifiMacHeader::GetSequenceNumber
uint16_t GetSequenceNumber(void) const
Return the sequence number of the header.
Definition: wifi-mac-header.cc:777
ns3::WifiMacHeader::GetFragmentNumber
uint8_t GetFragmentNumber(void) const
Return the fragment number of the header.
Definition: wifi-mac-header.cc:783
ns3::WifiMacHeader::IsMgt
bool IsMgt(void) const
Return true if the Type is Management.
Definition: wifi-mac-header.cc:577
ns3::OriginatorRxStatus::m_lastSequenceControl
uint16_t m_lastSequenceControl
last sequence control
Definition: mac-rx-middle.cc:48
ns3::WifiMacHeader::GetQosTid
uint8_t GetQosTid(void) const
Return the Traffic ID of a QoS header.
Definition: wifi-mac-header.cc:862
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::OriginatorRxStatus::IsNextFragment
bool IsNextFragment(uint16_t sequenceControl) const
Check if the sequence control (i.e.
Definition: mac-rx-middle.cc:128
ns3::MacRxMiddle::~MacRxMiddle
~MacRxMiddle()
Definition: mac-rx-middle.cc:166
ns3::Packet::AddAtEnd
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::MacRxMiddle::m_callback
ForwardUpCallback m_callback
forward up callback
Definition: mac-rx-middle.h:128
ns3::OriginatorRxStatus::AccumulateFirstFragment
void AccumulateFirstFragment(Ptr< const Packet > packet)
We have received a first fragmented packet.
Definition: mac-rx-middle.cc:79
ns3::OriginatorRxStatus::m_fragments
Fragments m_fragments
fragments
Definition: mac-rx-middle.cc:49
ns3::OriginatorRxStatus
A class to keep track of the packet originator status.
Definition: mac-rx-middle.cc:36
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::WifiMacHeader::IsMoreFragments
bool IsMoreFragments(void) const
Return if the More Fragment bit is set.
Definition: wifi-mac-header.cc:795
ns3::MacRxMiddle::m_qosOriginatorStatus
QosOriginators m_qosOriginatorStatus
QOS originator status.
Definition: mac-rx-middle.h:127
ns3::MacRxMiddle::MacRxMiddle
MacRxMiddle()
Definition: mac-rx-middle.cc:161
ns3::MacRxMiddle::Lookup
OriginatorRxStatus * Lookup(const WifiMacHeader *hdr)
Look up for OriginatorRxStatus associated with the sender address (by looking at ADDR2 field in the h...
Definition: mac-rx-middle.cc:193
ns3::WifiMacHeader::GetAddr2
Mac48Address GetAddr2(void) const
Return the address in the Address 2 field.
Definition: wifi-mac-header.cc:430
ns3::SequenceNumber
Generic "sequence number" class.
Definition: sequence-number.h:60
ns3::OriginatorRxStatus::AccumulateFragment
void AccumulateFragment(Ptr< const Packet > packet)
We received a fragmented packet (not first and not last).
Definition: mac-rx-middle.cc:114
ns3::MacRxMiddle::Receive
void Receive(Ptr< WifiMacQueueItem > mpdu)
Receive a packet.
Definition: mac-rx-middle.cc:299