A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
peer-management-protocol-mac.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
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: Kirill Andreev <andreev@iitp.ru>
19  */
20 
23 #include "dot11s-mac-header.h"
26 #include "peer-link-frame.h"
27 #include "ns3/mesh-wifi-interface-mac.h"
28 #include "ns3/simulator.h"
29 #include "ns3/wifi-mac-header.h"
30 #include "ns3/mesh-information-element-vector.h"
31 #include "ns3/log.h"
32 namespace ns3 {
33 namespace dot11s {
36 {
37  m_ifIndex = interface;
38  m_protocol = protocol;
39 }
40 
42 {
43 }
44 
45 void
47 {
48  m_parent = parent;
49  m_parent->TraceConnectWithoutContext ("TxErrHeader", MakeCallback (&PeerManagementProtocolMac::TxError, this));
50  m_parent->TraceConnectWithoutContext ("TxOkHeader", MakeCallback (&PeerManagementProtocolMac::TxOk, this));
51 }
52 void
54 {
55  m_protocol->TransmissionFailure (m_ifIndex, hdr.GetAddr1 ());
56 }
57 void
59 {
60  m_protocol->TransmissionSuccess (m_ifIndex, hdr.GetAddr1 ());
61 }
62 bool
64 {
65  // First of all we copy a packet, because we need to remove some
66  //headers
67  Ptr<Packet> packet = const_packet->Copy ();
68  if (header.IsBeacon ())
69  {
70  MgtBeaconHeader beacon_hdr;
71  packet->RemoveHeader (beacon_hdr);
73  packet->RemoveHeader (elements);
74  Ptr<IeBeaconTiming> beaconTiming = DynamicCast<IeBeaconTiming> (elements.FindFirst (IE11S_BEACON_TIMING));
75  Ptr<IeMeshId> meshId = DynamicCast<IeMeshId> (elements.FindFirst (IE11S_MESH_ID));
76 
77  if ((meshId != 0) && (m_protocol->GetMeshId ()->IsEqual (*meshId)))
78  {
79  m_protocol->ReceiveBeacon (m_ifIndex, header.GetAddr2 (), MicroSeconds (
80  beacon_hdr.GetBeaconIntervalUs ()), beaconTiming);
81  }
82  // Beacon shall not be dropped. May be needed to another plugins
83  return true;
84  }
85  if (header.IsAction ())
86  {
87  WifiActionHeader actionHdr;
88  packet->RemoveHeader (actionHdr);
89  WifiActionHeader::ActionValue actionValue = actionHdr.GetAction ();
90  // If can not handle - just return;
92  {
93  return m_protocol->IsActiveLink (m_ifIndex, header.GetAddr2 ());
94  }
95  m_stats.rxMgt++;
96  m_stats.rxMgtBytes += packet->GetSize ();
97  Mac48Address peerAddress = header.GetAddr2 ();
98  Mac48Address peerMpAddress = header.GetAddr3 ();
100  {
101  PeerLinkFrameStart peerFrame;
102  peerFrame.SetPlinkFrameSubtype ((uint8_t) actionValue.peerLink);
103  packet->RemoveHeader (peerFrame);
104  fields = peerFrame.GetFields ();
105  NS_ASSERT (fields.subtype == actionValue.peerLink);
106  }
107  if ((actionValue.peerLink != WifiActionHeader::PEER_LINK_CLOSE) && !(m_parent->CheckSupportedRates (
108  fields.rates)))
109  {
110  m_protocol->ConfigurationMismatch (m_ifIndex, peerAddress);
111  // Broken peer link frame - drop it
112  m_stats.brokenMgt++;
113  return false;
114  }
115  if ((actionValue.peerLink != WifiActionHeader::PEER_LINK_CONFIRM) && !fields.meshId.IsEqual (
116  *(m_protocol->GetMeshId ())))
117  {
118  m_protocol->ConfigurationMismatch (m_ifIndex, peerAddress);
119  // Broken peer link frame - drop it
120  m_stats.brokenMgt++;
121  return false;
122  }
123  Ptr<IePeerManagement> peerElement;
124  //Peer Management element is the last element in this frame - so, we can use MeshInformationElementVector
126  packet->RemoveHeader (elements);
127  peerElement = DynamicCast<IePeerManagement>(elements.FindFirst (IE11S_PEERING_MANAGEMENT));
128  NS_ASSERT (peerElement != 0);
129  //Check taht frame subtype corresponds peer link subtype
130  if (peerElement->SubtypeIsOpen ())
131  {
132  m_stats.rxOpen++;
134  }
135  if (peerElement->SubtypeIsConfirm ())
136  {
137  m_stats.rxConfirm++;
139  }
140  if (peerElement->SubtypeIsClose ())
141  {
142  m_stats.rxClose++;
144  }
145  //Deliver Peer link management frame to protocol:
146  m_protocol->ReceivePeerLinkFrame (m_ifIndex, peerAddress, peerMpAddress, fields.aid, *peerElement,
147  fields.config);
148  // if we can handle a frame - drop it
149  return false;
150  }
151  return m_protocol->IsActiveLink (m_ifIndex, header.GetAddr2 ());
152 }
153 bool
155  Mac48Address from, Mac48Address to)
156 {
157  if (header.IsAction ())
158  {
159  WifiActionHeader actionHdr;
160  packet->PeekHeader (actionHdr);
162  {
163  return true;
164  }
165  }
166  if (header.GetAddr1 ().IsGroup ())
167  {
168  return true;
169  }
170  else
171  {
172  if (m_protocol->IsActiveLink (m_ifIndex, header.GetAddr1 ()))
173  {
174  return true;
175  }
176  else
177  {
178  m_stats.dropped++;
179  return false;
180  }
181  }
182 }
183 void
185 {
186  if (m_protocol->GetBeaconCollisionAvoidance ())
187  {
188  Ptr<IeBeaconTiming> beaconTiming = m_protocol->GetBeaconTimingElement (m_ifIndex);
189  beacon.AddInformationElement (beaconTiming);
190  }
191  beacon.AddInformationElement (m_protocol->GetMeshId ());
192  m_protocol->NotifyBeaconSent (m_ifIndex, beacon.GetBeaconInterval ());
193 }
194 
195 void
197  uint16_t aid, IePeerManagement peerElement, IeConfiguration meshConfig)
198 {
199  //Create a packet:
200  meshConfig.SetNeighborCount (m_protocol->GetNumberOfLinks ());
201  Ptr<Packet> packet = Create<Packet> ();
203  elements.AddInformationElement (Ptr<IePeerManagement> (&peerElement));
204  packet->AddHeader (elements);
206  fields.rates = m_parent->GetSupportedRates ();
207  fields.capability = 0;
208  fields.meshId = *(m_protocol->GetMeshId ());
209  fields.config = meshConfig;
210  PeerLinkFrameStart plinkFrame;
211  //Create an 802.11 frame header:
212  //Send management frame to MAC:
213  WifiActionHeader actionHdr;
214  if (peerElement.SubtypeIsOpen ())
215  {
216  m_stats.txOpen++;
220  actionHdr.SetAction (WifiActionHeader::MESH_PEERING_MGT, action);
221  }
222  if (peerElement.SubtypeIsConfirm ())
223  {
224  m_stats.txConfirm++;
227  fields.aid = aid;
229  actionHdr.SetAction (WifiActionHeader::MESH_PEERING_MGT, action);
230  }
231  if (peerElement.SubtypeIsClose ())
232  {
233  m_stats.txClose++;
237  fields.reasonCode = peerElement.GetReasonCode ();
238  actionHdr.SetAction (WifiActionHeader::MESH_PEERING_MGT, action);
239  }
240  plinkFrame.SetPlinkFrameStart (fields);
241  packet->AddHeader (plinkFrame);
242  packet->AddHeader (actionHdr);
243  m_stats.txMgt++;
244  m_stats.txMgtBytes += packet->GetSize ();
245  // Wifi Mac header:
246  WifiMacHeader hdr;
247  hdr.SetAction ();
248  hdr.SetAddr1 (peerAddress);
249  hdr.SetAddr2 (m_parent->GetAddress ());
250  //Addr is not used here, we use it as our MP address
251  hdr.SetAddr3 (m_protocol->GetAddress ());
252  hdr.SetDsNotFrom ();
253  hdr.SetDsNotTo ();
254  m_parent->SendManagementFrame (packet, hdr);
255 }
256 
259 {
260  if (m_parent != 0)
261  {
262  return m_parent->GetAddress ();
263  }
264  else
265  {
266  return Mac48Address ();
267  }
268 }
269 void
271 {
272  if (shift != Seconds (0))
273  {
275  }
276  m_parent->ShiftTbtt (shift);
277 }
279  txOpen (0), txConfirm (0), txClose (0), rxOpen (0), rxConfirm (0), rxClose (0), dropped (0), brokenMgt (0),
280  txMgt (0), txMgtBytes (0), rxMgt (0), rxMgtBytes (0), beaconShift (0)
281 {
282 }
283 void
285 {
286  os << "<Statistics "
287  "txOpen=\"" << txOpen << "\"" << std::endl <<
288  "txConfirm=\"" << txConfirm << "\"" << std::endl <<
289  "txClose=\"" << txClose << "\"" << std::endl <<
290  "rxOpen=\"" << rxOpen << "\"" << std::endl <<
291  "rxConfirm=\"" << rxConfirm << "\"" << std::endl <<
292  "rxClose=\"" << rxClose << "\"" << std::endl <<
293  "dropped=\"" << dropped << "\"" << std::endl <<
294  "brokenMgt=\"" << brokenMgt << "\"" << std::endl <<
295  "txMgt=\"" << txMgt << "\"" << std::endl <<
296  "txMgtBytes=\"" << txMgtBytes << "\"" << std::endl <<
297  "rxMgt=\"" << rxMgt << "\"" << std::endl <<
298  "rxMgtBytes=\"" << rxMgtBytes << "\"" << std::endl <<
299  "beaconShift=\"" << beaconShift << "\"/>" << std::endl;
300 }
301 void
302 PeerManagementProtocolMac::Report (std::ostream & os) const
303 {
304  os << "<PeerManagementProtocolMac "
305  "address=\"" << m_parent->GetAddress () << "\">" << std::endl;
306  m_stats.Print (os);
307  os << "</PeerManagementProtocolMac>" << std::endl;
308 }
309 void
311 {
312  m_stats = Statistics ();
313 }
314 uint32_t
316 {
317  return m_parent->GetLinkMetric (peerAddress);
318 }
319 int64_t
321 {
322  return m_protocol->AssignStreams (stream);
323 }
324 
325 } // namespace dot11s
326 } // namespace ns3
327 
bool IsBeacon(void) const
Return true if the header is a Beacon header.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
void AddInformationElement(Ptr< WifiInformationElement > ie)
Add information element.
void SetAction()
Set Type/Subtype values for an action header.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void SetBeaconShift(Time shift)
BCA functionality.
See IEEE 802.11 chapter 7.3.1.11 Header format: | category: 1 | action value: 1 |.
Definition: mgt-headers.h:336
uint64_t GetBeaconIntervalUs(void) const
Return the beacon interval in microseconds unit.
Definition: mgt-headers.cc:146
bool IsAction() const
Return true if the header is an Action header.
Mac48Address GetAddr3(void) const
Return the address in the Address 3 field.
#define NS_ASSERT(condition)
Definition: assert.h:64
uint32_t GetSize(void) const
Definition: packet.h:650
void SetAction(enum CategoryValue type, ActionValue action)
Set action for this Action header.
Definition: mgt-headers.cc:482
#define IE11S_MESH_ID
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
CategoryValue GetCategory()
Return the category value.
Definition: mgt-headers.cc:513
bool AddInformationElement(Ptr< WifiInformationElement > element)
add an IE, if maxSize has exceeded, returns false
See 7.3.2.85 of draft 2.07.
void SetAddr1(Mac48Address address)
Fill the Address 1 field with the given address.
PeerManagementProtocolMac(uint32_t interface, Ptr< PeerManagementProtocol > protocol)
void SetDsNotTo(void)
Un-set the To DS bit in the Frame Control field.
void SetAddr3(Mac48Address address)
Fill the Address 3 field with the given address.
Ptr< WifiInformationElement > FindFirst(WifiInformationElementId id) const
vector of pointers to information elements is the body of IeVector
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
#define IE11S_BEACON_TIMING
bool UpdateOutcomingFrame(Ptr< Packet > packet, WifiMacHeader &header, Mac48Address from, Mac48Address to)
Update frame before it will be forwarded down.
uint32_t GetLinkMetric(Mac48Address peerAddress)
Ptr< Packet > Copy(void) const
Definition: packet.cc:122
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:277
bool IsGroup(void) const
void SetAddr2(Mac48Address address)
Fill the Address 2 field with the given address.
void SendPeerLinkManagementFrame(Mac48Address peerAddress, Mac48Address peerMpAddress, uint16_t aid, IePeerManagement peerElement, IeConfiguration meshConfig)
an EUI-48 address
Definition: mac48-address.h:41
Mac48Address GetAddress() const
debug only, used to print established links
Beacon is beacon header + list of arbitrary information elements.
Time GetBeaconInterval() const
Returns a beacon interval of wifi beacon.
#define IE11S_PEERING_MANAGEMENT
void UpdateBeacon(MeshWifiBeacon &beacon) const
Update beacon before it will be formed and sent.
typedef for union of different ActionValues
Definition: mgt-headers.h:410
enum PeerLinkMgtActionValue peerLink
Definition: mgt-headers.h:412
Describes Mesh Configuration Element see 7.3.2.86 of 802.11s draft 3.0.
void SetNeighborCount(uint8_t neighbors)
bool IsEqual(IeMeshId const &o) const
Definition: ie-dot11s-id.cc:58
bool Receive(Ptr< Packet > packet, const WifiMacHeader &header)
Process received frame.
Mac48Address GetAddr1(void) const
Return the address in the Address 1 field.
ActionValue GetAction()
Return the action value.
Definition: mgt-headers.cc:539
void SetParent(Ptr< MeshWifiInterfaceMac > parent)
Each plugin must be installed on interface to work.
void TxError(WifiMacHeader const &hdr)
Closes link when a proper number of successive transmissions have failed.
Implement the header for management frames of type beacon.
Definition: mgt-headers.h:321
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Implements the IEEE 802.11 MAC header.
Mac48Address GetAddr2(void) const
Return the address in the Address 2 field.
void SetDsNotFrom(void)
Un-set the From DS bit in the Frame Control field.