src/devices/wifi/minstrel-wifi-manager.h
changeset 4703 e1259e2fdaad
child 6065 0f012e7d9128
equal deleted inserted replaced
4702:e33a677e8864 4703:e1259e2fdaad
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /* 
       
     3  * Copyright (c) 2009 Duy Nguyen 
       
     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: Duy Nguyen <duy@soe.ucsc.edu> 
       
    19  */
       
    20 
       
    21 
       
    22 
       
    23 #ifndef MINSTREL_WIFI_MANAGER_H
       
    24 #define MINSTREL_WIFI_MANAGER_H
       
    25 
       
    26 #include "wifi-remote-station-manager.h"
       
    27 #include "wifi-mode.h"
       
    28 #include "ns3/nstime.h"
       
    29 #include <vector>
       
    30 
       
    31 
       
    32 
       
    33 /**
       
    34  * \author Duy Nguyen
       
    35  * \brief Implementation of Minstrel Rate Control Algorithm 
       
    36  *
       
    37  * Porting Minstrel from Madwifi and Linux Kernel 
       
    38  * http://linuxwireless.org/en/developers/Documentation/mac80211/RateControl/minstrel
       
    39  */
       
    40 
       
    41 
       
    42 namespace ns3 {
       
    43 
       
    44 
       
    45 /**
       
    46  * A struct to contain all information related to a data rate 
       
    47  */
       
    48 struct RateInfo{
       
    49 
       
    50   /**
       
    51    * Perfect transmission time calculation, or frame calculation
       
    52    * Given a bit rate and a packet length n bytes 
       
    53    */
       
    54   Time perfectTxTime;		
       
    55 
       
    56 
       
    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)
       
    62 
       
    63   /**
       
    64    * EWMA calculation
       
    65    * ewma_prob =[prob *(100 - ewma_level) + (ewma_prob_old * ewma_level)]/100 
       
    66    */
       
    67   uint32_t ewmaProb;
       
    68 
       
    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
       
    74 };
       
    75 
       
    76 /**
       
    77  * Data structure for a Minstrel Rate table 
       
    78  * A vector of a struct RateInfo 
       
    79  */
       
    80 typedef std::vector<struct RateInfo> MinstrelRate;
       
    81 
       
    82 /**
       
    83  * Data structure for a Sample Rate table
       
    84  * A vector of a vector uint32_t 
       
    85  */
       
    86 typedef std::vector<std::vector<uint32_t> > SampleRate;
       
    87 
       
    88 
       
    89 class MinstrelWifiManager : public WifiRemoteStationManager
       
    90 {
       
    91 
       
    92 public:
       
    93   static TypeId GetTypeId (void);
       
    94   MinstrelWifiManager ();
       
    95   virtual ~MinstrelWifiManager();
       
    96 
       
    97   virtual void SetupPhy (Ptr<WifiPhy> phy);
       
    98 
       
    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);
       
   102 
       
   103 private:
       
   104   friend class MinstrelWifiRemoteStation;
       
   105   virtual class WifiRemoteStation *CreateStation (void);
       
   106 
       
   107   typedef std::vector<std::pair<Time,WifiMode> > TxTime;
       
   108 
       
   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
       
   116   
       
   117 };
       
   118 
       
   119 class MinstrelWifiRemoteStation : public WifiRemoteStation
       
   120 {
       
   121 public:
       
   122   MinstrelWifiRemoteStation (Ptr<MinstrelWifiManager> stations);
       
   123  
       
   124   virtual ~MinstrelWifiRemoteStation ();
       
   125 
       
   126 protected:
       
   127 
       
   128   /** 
       
   129    * when packet is successfully received
       
   130    * see wifi-remote-station-manager.h for more documentation
       
   131    */
       
   132   virtual void DoReportRxOk (double rxSnr, WifiMode txMode);
       
   133 
       
   134   /// when RTS timeout expires
       
   135   virtual void DoReportRtsFailed (void);
       
   136 
       
   137 
       
   138   /**
       
   139    *
       
   140    * Retry Chain table is implemented here
       
   141    *
       
   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
       
   149    *
       
   150    * Note: For clarity, multiple blocks of if's and else's are used
       
   151    * After a failing 7 times, DoReportFinalDataFailed will be called
       
   152    */
       
   153   virtual void DoReportDataFailed (void);
       
   154 
       
   155   /// when receive a CTS, associated with an RTS
       
   156   virtual void DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr);
       
   157 
       
   158   /// when an ACK, associated with a data pkt, is received
       
   159   virtual void DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr);
       
   160 
       
   161   /// after calling ReportRtsFailed if NeedRtsRetransmission returns false
       
   162   virtual void DoReportFinalRtsFailed (void);
       
   163 
       
   164   /// after calling ReportDataFailed if NeedDataRetransmission returns false
       
   165   virtual void DoReportFinalDataFailed (void);
       
   166 
       
   167 
       
   168 private:
       
   169   virtual Ptr<WifiRemoteStationManager> GetManager (void) const;
       
   170 
       
   171   /** 
       
   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
       
   175    */
       
   176   virtual WifiMode DoGetDataMode (uint32_t size);
       
   177 
       
   178   /// returns the transmission mode for sending RTS packet
       
   179   virtual WifiMode DoGetRtsMode (void);
       
   180 
       
   181   /// update the number of retries and reset accordingly
       
   182   void UpdateRetry (void);
       
   183 
       
   184   /// getting the next sample from Sample Table
       
   185   uint32_t GetNextSample (void);
       
   186 
       
   187   /// find a rate to use from Minstrel Table
       
   188   uint32_t FindRate (void);
       
   189 
       
   190   /// updating the Minstrel Table every 1/10 seconds
       
   191   void UpdateStats (void);
       
   192 
       
   193   /// initialize Minstrel Table
       
   194   void RateInit (void);
       
   195 
       
   196   /// initialize Sample Table
       
   197   void InitSampleTable (void);
       
   198 
       
   199   /// printing Sample Table
       
   200   void PrintSampleTable (void);
       
   201 
       
   202   /// printing Minstrel Table
       
   203   void PrintTable (void);
       
   204 
       
   205   /**
       
   206    * \param packet lenghth 
       
   207    * \param current WifiMode
       
   208    * \returns calcuated transmit duration 
       
   209    */
       
   210   Time CalcRatePacket (uint32_t, WifiMode);
       
   211 
       
   212   void CheckInit(void);  ///< check for initializations
       
   213 
       
   214   Ptr<MinstrelWifiManager> m_stations;
       
   215 
       
   216   Time m_nextStatsUpdate;  ///< 10 times every second
       
   217 
       
   218   MinstrelRate m_minstrelTable;  ///< minstrel table	
       
   219 
       
   220   SampleRate m_sampleTable;  ///< sample table
       
   221 
       
   222   /**
       
   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.
       
   227    */
       
   228   uint32_t m_col, m_index;							
       
   229 
       
   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
       
   233 
       
   234   int m_packetCount;  ///< total number of packets as of now
       
   235   int m_sampleCount;  ///< how many packets we have sample so far
       
   236 
       
   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
       
   241 
       
   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
       
   247 
       
   248   bool m_initialized;  ///< for initializing tables
       
   249 
       
   250 
       
   251 }; 
       
   252 
       
   253 }// namespace ns3
       
   254 
       
   255 #endif /* MINSTREL_WIFI_MANAGER_H */