1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008,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  * Authors: Kirill Andreev <andreev@iitp.ru>
19  *          Aleksey Kovalenko <kovalenko@iitp.ru>
20  */
21 
22 #ifndef DOT11S_PEER_MAN_H
23 #define DOT11S_PEER_MAN_H
24 
25 #include "ns3/mac48-address.h"
26 #include "ns3/net-device.h"
27 #include "ns3/event-id.h"
28 #include "ns3/nstime.h"
29 #include "ns3/traced-value.h"
30 #include "ie-dot11s-beacon-timing.h"
31 #include "ie-dot11s-peer-management.h"
32 #include "peer-link.h"
33 
34 #include <map>
35 namespace ns3 {
36 class MeshPointDevice;
37 class UniformRandomVariable;
38 namespace dot11s {
39 class PeerManagementProtocolMac;
40 class PeerLink;
41 class IeMeshId;
42 class IePeerManagement;
43 class IeConfiguration;
44 /**
45  * \ingroup dot11s
46  *
47  * \brief 802.11s Peer Management Protocol model
48  */
49 class PeerManagementProtocol : public Object
50 {
51 public:
52   PeerManagementProtocol ();
53   ~PeerManagementProtocol ();
54   /**
55    * \brief Get the type ID.
56    * \return the object TypeId
57    */
58   static TypeId GetTypeId ();
59   void DoDispose ();
60   /**
61    * \brief Install PMP on given mesh point.
62    * \param mp the MeshPointDevice to install onto
63    * \return true if successful
64    *
65    * Installing protocol causes installation of its interface MAC plugins.
66    *
67    * Also MP aggregates all installed protocols, PMP protocol can be accessed
68    * via MeshPointDevice::GetObject<PeerManagementProtocol>();
69    */
70   bool Install (Ptr<MeshPointDevice> mp);
71   /**
72    * \brief Methods that handle beacon sending/receiving procedure.
73    *
74    * \name This methods interact with MAC_layer plug-in
75    */
76   ///@{
77   /**
78    * \brief When we are sending a beacon - we fill beacon timing
79    * element
80    * \return IeBeaconTiming is a beacon timing element that should be present in beacon
81    * \param interface is a interface sending a beacon
82    */
83   Ptr<IeBeaconTiming> GetBeaconTimingElement (uint32_t interface);
84   /**
85    * \brief To initiate peer link we must notify about received beacon
86    * \param interface the interface where a beacon was received from
87    * \param peerAddress address of station, which sent a beacon
88    * \param beaconInterval beacon interval (needed by beacon loss counter)
89    * \param beaconTiming beacon timing element (needed by BCA)
90    */
91   void ReceiveBeacon (uint32_t interface, Mac48Address peerAddress, Time beaconInterval, Ptr<IeBeaconTiming> beaconTiming);
92   ///@}
93 
94   /**
95    * \name Methods that handle Peer link management frames interaction:
96    */
97   ///@{
98   /**
99    * Deliver Peer link management information to the protocol-part
100    * \param interface is a interface ID of a given MAC (interfaceID rather than MAC address, because many interfaces may have the same MAC)
101    * \param peerAddress is address of peer
102    * \param peerMeshPointAddress is address of peer mesh point device (equal to peer address when only one interface)
103    * \param aid is association ID, which peer has assigned to us
104    * \param peerManagementElement is peer link management element
105    * \param meshConfig is mesh configuration element taken from the peer management frame
106    */
107   void ReceivePeerLinkFrame (
108     uint32_t interface,
109     Mac48Address peerAddress,
110     Mac48Address peerMeshPointAddress,
111     uint16_t aid,
112     IePeerManagement peerManagementElement,
113     IeConfiguration meshConfig
114     );
115   /**
116    * \brief Cancels peer link due to broken configuration (Mesh ID or Supported
117    * rates)
118    * \param interface interface of the link to cancel
119    * \param peerAddress peer address of the link to cancel
120    */
121   void ConfigurationMismatch (uint32_t interface, Mac48Address peerAddress);
122   /**
123    * \brief Cancels peer link due to successive transmission failures
124    * \param interface interface of the link to cancel
125    * \param peerAddress peer address of the link to cancel
126    */
127   void TransmissionFailure (uint32_t interface, const Mac48Address peerAddress);
128   /**
129    * \brief resets transmission failure statistics
130    * \param interface interface of the link to reset
131    * \param peerAddress peer address of the link to reset
132    */
133   void TransmissionSuccess (uint32_t interface, const Mac48Address peerAddress);
134   /**
135    * \brief Checks if there is established link
136    * \param interface interface of the link to check
137    * \param peerAddress peer address of the link to check
138    * \return true if the link is active
139    */
140   bool IsActiveLink (uint32_t interface, Mac48Address peerAddress);
141   ///@}
142 
143   /// \name Interface to other protocols (MLME)
144   ///@{
145   /**
146    *  Set peer link status change callback
147    *  \param cb the callback
148    */
149   void SetPeerLinkStatusCallback (Callback<void, Mac48Address, Mac48Address, uint32_t, bool> cb);
150   /**
151    * Find active peer link by my interface and peer interface MAC
152    * \param interface interface of the link to find
153    * \param peerAddress peer address of the link to find
154    * \return The peer link (null if not found)
155    */
156   Ptr<PeerLink> FindPeerLink (uint32_t interface, Mac48Address peerAddress);
157   /**
158    * Get list of all active peer links
159    * \return a list of all the active peer links
160    */
161   std::vector < Ptr<PeerLink> > GetPeerLinks () const;
162   /**
163    * Get list of active peers of my given interface
164    * \param interface the interface
165    * \return a list of all the active peer links on an interface
166    */
167   std::vector<Mac48Address> GetPeers (uint32_t interface) const;
168   /**
169    * Get mesh point address. \todo this used by plugins only. Now MAC plugins can ask MP address directly from main MAC
170    *
171    * \return Mac48Address
172    */
173   Mac48Address GetAddress ();
174   /**
175    * Get number of links
176    * \returns the number of links
177    */
178   uint8_t GetNumberOfLinks ();
179   /**
180    * Set mesh ID to a string value
181    * \param s the mesh ID string value
182    */
183   void SetMeshId (std::string s);
184   /**
185    * Get mesh ID information element
186    * \returns the mesh ID information element
187    */
188   Ptr<IeMeshId> GetMeshId () const;
189   /**
190    * Enable or disable beacon collision avoidance
191    * \param enable true to enable beacon collision avoidance
192    */
193   void SetBeaconCollisionAvoidance (bool enable);
194   /**
195    * Get beacon collision avoidance
196    * \returns the beacon collision avoidance flag
197    */
198   bool GetBeaconCollisionAvoidance () const;
199   /**
200    * Notify about beacon send event, needed to schedule BCA
201    * \param interface the interface to use
202    * \param beaconInterval the beacon interval
203    */
204   void NotifyBeaconSent (uint32_t interface, Time beaconInterval);
205   ///@}
206 
207   /**
208    * \brief Report statistics
209    * \param os the output stream
210    */
211   void Report (std::ostream & os) const;
212   /// Reset statistics function
213   void ResetStats ();
214   /**
215    * Assign a fixed random variable stream number to the random variables
216    * used by this model.  Return the number of streams (possibly zero) that
217    * have been assigned.
218    *
219    * \param stream first stream index to use
220    * \return the number of stream indices assigned by this model
221    */
222   int64_t AssignStreams (int64_t stream);
223 
224   /**
225    * TracedCallback signature for link open/close events.
226    *
227    * \param [in] src MAC address of source interface.
228    * \param [in] dst MAC address of destination interface.
229    */
230   typedef void (* LinkOpenCloseTracedCallback)
231     (Mac48Address src, const Mac48Address dst);
232 
233 
234 private:
235   virtual void DoInitialize ();
236 
237   // Private structures
238   /// Keeps information about beacon of peer station: beacon interval, association ID, last time we have received a beacon
239   struct BeaconInfo
240   {
241     uint16_t aid; ///< Assoc ID
242     Time referenceTbtt; ///< When one of my station's beacons was put into a beacon queue;
243     Time beaconInterval; ///< Beacon interval of my station;
244   };
245   /// We keep a vector of pointers to PeerLink class. This vector
246   /// keeps all peer links at a given interface.
247   typedef std::vector<Ptr<PeerLink> > PeerLinksOnInterface;
248   /// This map keeps all peer links.
249   typedef std::map<uint32_t, PeerLinksOnInterface>  PeerLinksMap;
250   /// This map keeps relationship between peer address and its beacon information
251   typedef std::map<Mac48Address, BeaconInfo>  BeaconsOnInterface;
252   ///\brief This map keeps beacon information on all interfaces
253   typedef std::map<uint32_t, BeaconsOnInterface> BeaconInfoMap;
254   ///\brief this vector keeps pointers to MAC-plugins
255   typedef std::map<uint32_t, Ptr<PeerManagementProtocolMac> > PeerManagementProtocolMacMap;
256 
257 private:
258   /**
259    * assignment operator
260    *
261    * \param peer the value to assign
262    * \returns the assigned value
263    */
264   PeerManagementProtocol& operator= (const PeerManagementProtocol & peer);
265   /// type conversion operator
266   PeerManagementProtocol (const PeerManagementProtocol &);
267 
268   /**
269    * Initiate link function
270    *
271    * \param interface the interface to use
272    * \param peerAddress the peer address
273    * \param peerMeshPointAddress the peer mesh point address
274    * \returns the peer link
275    */
276   Ptr<PeerLink> InitiateLink (
277     uint32_t interface,
278     Mac48Address peerAddress,
279     Mac48Address peerMeshPointAddress
280     );
281   /**
282    * \brief External peer-chooser
283    * \param interface the interface to use
284    * \param peerAddress the peer address
285    * \returns true is should send an open
286    */
287   bool ShouldSendOpen (uint32_t interface, Mac48Address peerAddress);
288   /**
289    * \brief External peer-chooser
290    * \param interface the interface to use
291    * \param peerAddress the peer address
292    * \param reasonCode reason code
293    * \returns true is should send an open
294    */
295   bool ShouldAcceptOpen (uint32_t interface, Mac48Address peerAddress, PmpReasonCode & reasonCode);
296   /**
297    * \brief Indicates changes in peer links
298    * \param interface the interface
299    * \param peerAddress the peer address
300    * \param peerMeshPointAddres the peer mesh point address
301    * \param ostate old state
302    * \param nstate new state
303    */
304   void PeerLinkStatus (uint32_t interface, Mac48Address peerAddress, Mac48Address peerMeshPointAddres, PeerLink::PeerState ostate, PeerLink::PeerState nstate);
305   /**
306    * \brief BCA
307    * \param interface interface
308    */
309   void CheckBeaconCollisions (uint32_t interface);
310   /**
311    * Shift own beacon function
312    * \param interface interface
313    */
314   void ShiftOwnBeacon (uint32_t interface);
315   /**
316    * \brief Time<-->TU converters:
317    * \param x TU
318    * \return Time
319    */
320   Time TuToTime (int x);
321   /**
322    * \brief Time<-->TU converters:
323    * \param x Time
324    * \return TU
325    */
326   int TimeToTu (Time x);
327 
328   /**
329    * Aux. method to register open links
330    * \param peerMp peer mesh point address
331    * \param peerIface peer address
332    * \param myIface my address
333    * \param interface interface
334    */
335   void NotifyLinkOpen (Mac48Address peerMp, Mac48Address peerIface, Mac48Address myIface, uint32_t interface);
336   /**
337    * Aux. method to register closed links
338    * \param peerMp peer mesh point address
339    * \param peerIface peer address
340    * \param myIface my address
341    * \param interface interface
342    */
343   void NotifyLinkClose (Mac48Address peerMp, Mac48Address peerIface, Mac48Address myIface, uint32_t interface);
344 private:
345   PeerManagementProtocolMacMap m_plugins; ///< plugins
346   Mac48Address m_address; ///< address
347   Ptr<IeMeshId> m_meshId; ///< mesh ID
348 
349   uint16_t m_lastAssocId; ///< last associated ID
350   uint16_t m_lastLocalLinkId; ///< last local link ID
351   uint8_t m_maxNumberOfPeerLinks; ///< maimum number of peer links
352   /// Flag which enables BCA
353   bool m_enableBca;
354   /// Beacon can be shifted at [-m_maxBeaconShift; +m_maxBeaconShift] TUs
355   uint16_t m_maxBeaconShift;
356   /// Last beacon at each interface
357   std::map<uint32_t, Time> m_lastBeacon;
358   /// Beacon interval at each interface
359   std::map<uint32_t, Time> m_beaconInterval;
360 
361   /**
362    * \name Peer Links
363    */
364   PeerLinksMap m_peerLinks;
365   /**
366    * \brief Callback to notify about peer link changes:
367    * Mac48Address is peer address of mesh point,
368    * Mac48Address is peer address of interface,
369    * uint32_t - interface ID,
370    * bool is status - true when new link has appeared, false - when link was closed,
371    */
372   Callback <void, Mac48Address, Mac48Address, uint32_t, bool> m_peerStatusCallback;
373 
374   /// Simple link open/close trace source type. Addresses are: src interface, dst interface
375   typedef TracedCallback <Mac48Address, Mac48Address> LinkEventCallback;
376   /// LinkOpen trace source
377   LinkEventCallback m_linkOpenTraceSrc;
378   /// LinkClose trace source
379   LinkEventCallback m_linkCloseTraceSrc;
380 
381   /// Statistics structure
382   struct Statistics {
383     uint16_t linksTotal; ///< total links
384     uint16_t linksOpened; ///< opened links
385     uint16_t linksClosed; ///< links closed
386 
387     /**
388      * Constructor
389      *
390      * \param t
391      */
392     Statistics (uint16_t t = 0);
393     /**
394      * Print function
395      * \param os the output stream to print to
396      */
397     void Print (std::ostream & os) const;
398   };
399   struct Statistics m_stats; ///< statistics
400 
401   /// Add randomness to beacon shift
402   Ptr<UniformRandomVariable> m_beaconShift;
403 };
404 
405 } // namespace dot11s
406 } // namespace ns3
407 #endif
408