1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #ifndef UAN_MAC_RC_GW_H
22 #define UAN_MAC_RC_GW_H
23 
24 #include "uan-mac.h"
25 #include "ns3/mac8-address.h"
26 
27 #include "ns3/nstime.h"
28 #include "ns3/traced-callback.h"
29 
30 
31 
32 
33 #include <set>
34 #include <map>
35 
36 namespace ns3 {
37 
38 class UanTxMode;
39 
40 /**
41  * \ingroup uan
42  *
43  * Gateway side of RC-MAC.
44  *
45  * This MAC protocol assumes a network topology where all traffic
46  * is destined for a set of GW nodes which are connected via
47  * some out of band (RF?) means.  UanMacRcGw is the protocol
48  * which runs on the gateway nodes.
49  *
50  * This particular implementation
51  * assumes that there is only a single gateway.
52  *
53  * For more information on class operation email
54  * lentracy@u.washington.edu
55  * (This work is, as of yet, unpublished)
56  */
57 class UanMacRcGw : public UanMac
58 {
59 public:
60   UanMacRcGw ();           //!< Constructor
61   virtual ~UanMacRcGw ();  //!< Dummy destructor, see DoDispose.
62 
63   /**
64    * Register this type.
65    * \return The type ID.
66    */
67   static TypeId GetTypeId (void);
68 
69   // Inherited methods
70   virtual bool Enqueue (Ptr<Packet> pkt, uint16_t protocolNumber, const Address &dest);
71   virtual void SetForwardUpCb (Callback<void, Ptr<Packet>, uint16_t, const Mac8Address&> cb);
72   virtual void AttachPhy (Ptr<UanPhy> phy);
73   virtual void Clear (void);
74   int64_t AssignStreams (int64_t stream);
75 
76   /**
77    * TracedCallback signature for
78    *
79    * \param [in] now, The current simulation time.
80    * \param [in] delay Minimum path delay of first RTS.
81    * \param [in] numRts Number of pending RTS.
82    * \param [in] totalBytes Length of RTS header.
83    * \param [in] secs Effective window size for RcCtsGlobal message, or
84    *             time to next cycle, both in seconds.
85    * \param [in] ctlRate CTL rate in Bps.
86    * \param [in] actualX Current retry rate.
87    */
88   typedef void (* CycleCallback)
89     (Time now, Time delay, uint32_t numRts, uint32_t totalBytes,
90      double secs, uint32_t ctlRate, double actualX);
91 
92 
93 private:
94   /** Gateway state. */
95   enum State {
96     IDLE,     //!< Initial idle state.
97     INCYCLE,  //!< Cycling through nodes.
98     CTSING    //!< Sending CTS.
99   };
100   State m_state;  //!< Gateway processing state.
101 
102   /**
103    * \ingroup uan
104    * Reservation request.
105    */
106   struct Request
107   {
108     uint8_t numFrames;  //!< Number of frames.
109     uint8_t frameNo;    //!< Current frame number.
110     uint8_t retryNo;    //!< Retry number.
111     uint16_t length;    //!< Request header length.
112     Time rxTime;        //!< Time request received.
113   };
114 
115   /**
116    * \ingroup uan
117    * Packet ACK data.
118    */
119   struct AckData
120   {
121     uint8_t frameNo;    //!< Frame number being ACK'ed.
122     std::set<uint8_t> rxFrames;  //!< Received frames.
123     uint8_t expFrames;  //!< Expected number of frames.
124   };
125   /** Forwarding up callback. */
126   Callback<void, Ptr<Packet>, uint16_t, const Mac8Address&> m_forwardUpCb;
127 
128   Ptr<UanPhy> m_phy;            //!< PHY layer attached to this MAC.
129   Time m_maxDelta;              //!< Maximum propagation delay between gateway and non-gateway nodes .
130   Time m_sifs;                  //!< Spacing between frames to account for timing error and processing delay.
131   uint32_t m_maxRes;            //!< Maximum number of reservations to accept per cycle.
132   uint32_t m_numRates;          //!< Number of rates per Phy layer.
133   uint32_t m_rtsSize;           //!< Size of UanHeaderCommon and UanHeaderRcRts.
134   uint32_t m_ctsSizeN;          //!< Size of UanHeaderRcCts.
135   uint32_t m_ctsSizeG;          //!< Size of UanHeaderCommon and UanHeaderRcCtsGlobal.
136   uint32_t m_ackSize;           //!< Size of UanHeaderCommon and UanHeaderRcAck.
137   uint16_t m_currentRetryRate;  //!< Retry rate number for current cycle.
138   uint32_t m_currentRateNum;    //!< Rate number corresponding to data rate of current cycle.
139   uint32_t m_numNodes;          //!< Number of non-gateway nodes in this gateway's neighborhood.
140   uint32_t m_totalRate;         //!< Total available channel rate in bps (for a single channel, without splitting reservation channel).
141   uint32_t m_rateStep;          //!< Increments available for rate assignment in bps.
142   uint32_t m_frameSize;         //!< Size of data frames in bytes.
143 
144   double m_minRetryRate;        //!< Smallest allowed RTS retry rate.
145   double m_retryStep;           //!< Retry rate increment.
146 
147   /** Propagation delay to each node. */
148   std::map<Mac8Address, Time> m_propDelay;
149 
150   /** AckData for each node. */
151   std::map<Mac8Address, AckData> m_ackData;
152 
153   /** Request for each node. */
154   std::map<Mac8Address, Request> m_requests;
155   /** Queued request times. */
156   std::set<std::pair<Time, Mac8Address> > m_sortedRes;
157 
158   /** Flag when we've been cleared. */
159   bool m_cleared;
160 
161   /** A packet was destined for and received at this MAC layer. */
162   TracedCallback<Ptr<const Packet>, UanTxMode > m_rxLogger;
163 
164   /**
165    * A packet was destined for and received at this MAC layer.
166    *
167    * \pname{Start time}
168    * \pname{min propagation delay}
169    * \pname{reservations}
170    * \pname{frames}
171    * \pname{bytes}
172    * \pname{window size}
173    * \pname{CTS rate}
174    * \pname{retry rate}
175    */
176   TracedCallback<Time, Time, uint32_t, uint32_t, double, uint32_t, double> m_cycleLogger;
177 
178   /**
179    * PHY receive ok callback.
180    *
181    * \param pkt The Packet to receive
182    * \param sinr The SINR on the channel
183    * \param mode The transmission mode
184    */
185   void ReceivePacket (Ptr<Packet> pkt, double sinr, UanTxMode mode);
186 
187   /** Cycle through pending requests. */
188   void StartCycle (void);
189   /** End cycle by scheduling pending ACKs. */
190   void EndCycle (void);
191   /**
192    * Send packet on PHY.
193    *
194    * \param pkt The Packet.
195    * \param rate The UanTxMode number, m_currentRateNum.
196    */
197   void SendPacket (Ptr<Packet> pkt, uint32_t rate);
198   /** Set state to INCYCLE. */
199   void CycleStarted (void);
200   /**
201    * PHY receive error callback.
202    *
203    * \param pkt The failed packet.
204    * \param sinr The SINR value on the channel.
205    */
206   void ReceiveError (Ptr<Packet> pkt, double sinr);
207 
208   // Stuff for computing exp throughput
209   /**
210    * Compute alpha parameter.
211    *
212    * \param totalFrames Total number of frames in m_requests.
213    * \param totalBytes Total number of bytes in m_requests.
214    * \param n Number of nodes.
215    * \param a m_maxRes, or optimal A value.
216    * \param deltaK Propagation delay.
217    * \return Alpha parameter.
218    */
219    double ComputeAlpha (uint32_t totalFrames, uint32_t totalBytes, uint32_t n, uint32_t a, double deltaK);
220   /**
221    * Get the expected propagation delay to each node.
222    *
223    * \return Vector of expected propagation delays.
224    */
225   std::vector<double>  GetExpPdk (void);
226   /**
227    * Throughput for \pname{a} reservations with framesize \pname{ld},
228    * given expected delays exppdk.
229    *
230    * \param a Number of reservations.
231    * \param ld Frame size.
232    * \param exppdk Expected delays, given by GetExpPdk.
233    * \return Expected throughput.
234    */
235   double ComputeExpS (uint32_t a, uint32_t ld, std::vector<double> exppdk);
236   /**
237    * Throughput for \pname{a} reservations with framesize \pname{ld}.
238    *
239    * \param a Number of reservations.
240    * \param ld Frame size.
241    * \return Expected throughput.
242    */
243   double ComputeExpS (uint32_t a, uint32_t ld);
244   /**
245    * Index to the k'th expected delay among n nodes.
246    *
247    * \param n Number of nodes.
248    * \param k Target index.
249    * \return The expected index.
250    */
251   uint32_t CompExpMinIndex (uint32_t n, uint32_t k);
252   /**
253    * Numeric function.
254    *
255    * \param a Number of reservations.
256    * \param n number of nodes.
257    * \param k K'th node.
258    * \return Value.
259    */
260   double ComputePiK (uint32_t a, uint32_t n, uint32_t k);
261   /**
262    * Numeric function.
263    *
264    * \param n Number of nodes.
265    * \param a Number of reservations.
266    * \param ldlh Sum of common header length and frame size.
267    * \param deltaK Result of GetExpPdk
268    * \return value.
269    */
270   double ComputeExpBOverA (uint32_t n, uint32_t a, uint32_t ldlh, std::vector<double> deltaK);
271   /**
272    * Binomial coefficient.
273    *
274    * \param n Pool size.
275    * \param k Selection size.
276    * \return Binomial coefficient n choose k.
277    */
278   uint64_t NchooseK (uint32_t n, uint32_t k);
279   /**
280    * Compute the optimum maximum number of reservations to accept per cycle.
281    *
282    * \return Optimum number.
283    */
284   uint32_t FindOptA (void);
285 protected:
286   virtual void DoDispose ();
287 
288 };  // class UanMacRcGw
289 
290 } // namespace ns3
291 
292 #endif /* UAN_MAC_RC_GW_H */
293