1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2009 Duy Nguyen
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;
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.
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
18 * Author: Duy Nguyen <duy@soe.ucsc.edu>
23 #ifndef MINSTREL_WIFI_MANAGER_H
24 #define MINSTREL_WIFI_MANAGER_H
26 #include "wifi-remote-station-manager.h"
27 #include "wifi-mode.h"
28 #include "ns3/nstime.h"
35 * \brief Implementation of Minstrel Rate Control Algorithm
37 * Porting Minstrel from Madwifi and Linux Kernel
38 * http://linuxwireless.org/en/developers/Documentation/mac80211/RateControl/minstrel
46 * A struct to contain all information related to a data rate
51 * Perfect transmission time calculation, or frame calculation
52 * Given a bit rate and a packet length n bytes
57 uint32_t retryCount; ///< retry limit
58 uint32_t adjustedRetryCount; ///< adjust the retry limit for this rate
59 uint32_t numRateAttempt; ///< how many number of attempts so far
60 uint32_t numRateSuccess; ///< number of successful pkts
61 uint32_t prob; ///< (# pkts success )/(# total pkts)
65 * ewma_prob =[prob *(100 - ewma_level) + (ewma_prob_old * ewma_level)]/100
69 uint32_t prevNumRateAttempt; ///< from last rate
70 uint32_t prevNumRateSuccess; ///< from last rate
71 uint64_t successHist; ///< aggregate of all successes
72 uint64_t attemptHist; ///< aggregate of all attempts
73 uint32_t throughput; ///< throughput of a rate
77 * Data structure for a Minstrel Rate table
78 * A vector of a struct RateInfo
80 typedef std::vector<struct RateInfo> MinstrelRate;
83 * Data structure for a Sample Rate table
84 * A vector of a vector uint32_t
86 typedef std::vector<std::vector<uint32_t> > SampleRate;
89 class MinstrelWifiManager : public WifiRemoteStationManager
93 static TypeId GetTypeId (void);
94 MinstrelWifiManager ();
95 virtual ~MinstrelWifiManager();
97 virtual void SetupPhy (Ptr<WifiPhy> phy);
99 /// for estimating the TxTime of a packet with a given mode
100 Time GetCalcTxTime (WifiMode mode) const;
101 void AddCalcTxTime (WifiMode mode, Time t);
104 friend class MinstrelWifiRemoteStation;
105 virtual class WifiRemoteStation *CreateStation (void);
107 typedef std::vector<std::pair<Time,WifiMode> > TxTime;
109 TxTime m_calcTxTime; ///< to hold all the calculated TxTime for all modes
110 Time m_updateStats; ///< how frequent do we calculate the stats(1/10 seconds)
111 double m_lookAroundRate; ///< the % to try other rates than our current rate
112 double m_ewmaLevel; ///< exponential weighted moving average
113 uint32_t m_segmentSize; ///< largest allowable segment size
114 uint32_t m_sampleCol; ///< number of sample columns
115 uint32_t m_pktLen; ///< packet length used for calculate mode TxTime
119 class MinstrelWifiRemoteStation : public WifiRemoteStation
122 MinstrelWifiRemoteStation (Ptr<MinstrelWifiManager> stations);
124 virtual ~MinstrelWifiRemoteStation ();
129 * when packet is successfully received
130 * see wifi-remote-station-manager.h for more documentation
132 virtual void DoReportRxOk (double rxSnr, WifiMode txMode);
134 /// when RTS timeout expires
135 virtual void DoReportRtsFailed (void);
140 * Retry Chain table is implemented here
142 * Try | LOOKAROUND RATE | NORMAL RATE
143 * | random < best | random > best |
144 * --------------------------------------------------------------
145 * 1 | Best throughput | Random rate | Best throughput
146 * 2 | Random rate | Best throughput | Next best throughput
147 * 3 | Best probability | Best probability | Best probability
148 * 4 | Lowest Baserate | Lowest baserate | Lowest baserate
150 * Note: For clarity, multiple blocks of if's and else's are used
151 * After a failing 7 times, DoReportFinalDataFailed will be called
153 virtual void DoReportDataFailed (void);
155 /// when receive a CTS, associated with an RTS
156 virtual void DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr);
158 /// when an ACK, associated with a data pkt, is received
159 virtual void DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr);
161 /// after calling ReportRtsFailed if NeedRtsRetransmission returns false
162 virtual void DoReportFinalRtsFailed (void);
164 /// after calling ReportDataFailed if NeedDataRetransmission returns false
165 virtual void DoReportFinalDataFailed (void);
169 virtual Ptr<WifiRemoteStationManager> GetManager (void) const;
172 * returns the transmission mode for sending this packet
173 * this function gets called when node is getting ready to send DATA
174 * see wifi-remote-station-manager.h for more documentation
176 virtual WifiMode DoGetDataMode (uint32_t size);
178 /// returns the transmission mode for sending RTS packet
179 virtual WifiMode DoGetRtsMode (void);
181 /// update the number of retries and reset accordingly
182 void UpdateRetry (void);
184 /// getting the next sample from Sample Table
185 uint32_t GetNextSample (void);
187 /// find a rate to use from Minstrel Table
188 uint32_t FindRate (void);
190 /// updating the Minstrel Table every 1/10 seconds
191 void UpdateStats (void);
193 /// initialize Minstrel Table
194 void RateInit (void);
196 /// initialize Sample Table
197 void InitSampleTable (void);
199 /// printing Sample Table
200 void PrintSampleTable (void);
202 /// printing Minstrel Table
203 void PrintTable (void);
206 * \param packet lenghth
207 * \param current WifiMode
208 * \returns calcuated transmit duration
210 Time CalcRatePacket (uint32_t, WifiMode);
212 void CheckInit(void); ///< check for initializations
214 Ptr<MinstrelWifiManager> m_stations;
216 Time m_nextStatsUpdate; ///< 10 times every second
218 MinstrelRate m_minstrelTable; ///< minstrel table
220 SampleRate m_sampleTable; ///< sample table
223 * To keep track of the current position in the our random sample table
224 * going row by row from 1st column until the 10th column(Minstrel defines 10)
225 * then we wrap back to the row 1 col 1.
226 * note: there are many other ways to do this.
228 uint32_t m_col, m_index;
230 uint32_t m_maxTpRate; ///< the current throughput rate
231 uint32_t m_maxTpRate2; ///< second highest throughput rate
232 uint32_t m_maxProbRate; ///< rate with highest prob of success
234 int m_packetCount; ///< total number of packets as of now
235 int m_sampleCount; ///< how many packets we have sample so far
237 bool m_isSampling; ///< a flag to indicate we are currently sampling
238 uint32_t m_sampleRate; ///< current sample rate
239 bool m_sampleRateSlower; ///< a flag to indicate sample rate is slower
240 uint32_t m_currentRate; ///< current rate we are using
242 uint32_t m_shortRetry; ///< short retries such as control packts
243 uint32_t m_longRetry; ///< long retries such as data packets
244 uint32_t m_retry; ///< total retries short + long
245 uint32_t m_err; ///< retry errors
246 uint32_t m_txrate; ///< current transmit rate
248 bool m_initialized; ///< for initializing tables
255 #endif /* MINSTREL_WIFI_MANAGER_H */