src/devices/wifi/minstrel-wifi-manager.h
changeset 4703 e1259e2fdaad
child 6065 0f012e7d9128
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/devices/wifi/minstrel-wifi-manager.h	Thu Aug 13 08:45:47 2009 +0200
     1.3 @@ -0,0 +1,255 @@
     1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/* 
     1.6 + * Copyright (c) 2009 Duy Nguyen 
     1.7 + * 
     1.8 + * This program is free software; you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License version 2 as
    1.10 + * published by the Free Software Foundation;
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program; if not, write to the Free Software
    1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 + * 
    1.21 + * Author: Duy Nguyen <duy@soe.ucsc.edu> 
    1.22 + */
    1.23 +
    1.24 +
    1.25 +
    1.26 +#ifndef MINSTREL_WIFI_MANAGER_H
    1.27 +#define MINSTREL_WIFI_MANAGER_H
    1.28 +
    1.29 +#include "wifi-remote-station-manager.h"
    1.30 +#include "wifi-mode.h"
    1.31 +#include "ns3/nstime.h"
    1.32 +#include <vector>
    1.33 +
    1.34 +
    1.35 +
    1.36 +/**
    1.37 + * \author Duy Nguyen
    1.38 + * \brief Implementation of Minstrel Rate Control Algorithm 
    1.39 + *
    1.40 + * Porting Minstrel from Madwifi and Linux Kernel 
    1.41 + * http://linuxwireless.org/en/developers/Documentation/mac80211/RateControl/minstrel
    1.42 + */
    1.43 +
    1.44 +
    1.45 +namespace ns3 {
    1.46 +
    1.47 +
    1.48 +/**
    1.49 + * A struct to contain all information related to a data rate 
    1.50 + */
    1.51 +struct RateInfo{
    1.52 +
    1.53 +  /**
    1.54 +   * Perfect transmission time calculation, or frame calculation
    1.55 +   * Given a bit rate and a packet length n bytes 
    1.56 +   */
    1.57 +  Time perfectTxTime;		
    1.58 +
    1.59 +
    1.60 +  uint32_t retryCount;  ///< retry limit
    1.61 +  uint32_t adjustedRetryCount;  ///< adjust the retry limit for this rate
    1.62 +  uint32_t numRateAttempt;  ///< how many number of attempts so far
    1.63 +  uint32_t numRateSuccess;  ///< number of successful pkts 
    1.64 +  uint32_t prob;  ///< (# pkts success )/(# total pkts)
    1.65 +
    1.66 +  /**
    1.67 +   * EWMA calculation
    1.68 +   * ewma_prob =[prob *(100 - ewma_level) + (ewma_prob_old * ewma_level)]/100 
    1.69 +   */
    1.70 +  uint32_t ewmaProb;
    1.71 +
    1.72 +  uint32_t prevNumRateAttempt;  ///< from last rate
    1.73 +  uint32_t prevNumRateSuccess;  ///< from last rate
    1.74 +  uint64_t successHist;  ///< aggregate of all successes
    1.75 +  uint64_t attemptHist;  ///< aggregate of all attempts
    1.76 +  uint32_t throughput;  ///< throughput of a rate
    1.77 +};
    1.78 +
    1.79 +/**
    1.80 + * Data structure for a Minstrel Rate table 
    1.81 + * A vector of a struct RateInfo 
    1.82 + */
    1.83 +typedef std::vector<struct RateInfo> MinstrelRate;
    1.84 +
    1.85 +/**
    1.86 + * Data structure for a Sample Rate table
    1.87 + * A vector of a vector uint32_t 
    1.88 + */
    1.89 +typedef std::vector<std::vector<uint32_t> > SampleRate;
    1.90 +
    1.91 +
    1.92 +class MinstrelWifiManager : public WifiRemoteStationManager
    1.93 +{
    1.94 +
    1.95 +public:
    1.96 +  static TypeId GetTypeId (void);
    1.97 +  MinstrelWifiManager ();
    1.98 +  virtual ~MinstrelWifiManager();
    1.99 +
   1.100 +  virtual void SetupPhy (Ptr<WifiPhy> phy);
   1.101 +
   1.102 +  /// for estimating the TxTime of a packet with a given mode  
   1.103 +  Time GetCalcTxTime (WifiMode mode) const;
   1.104 +  void AddCalcTxTime (WifiMode mode, Time t);
   1.105 +
   1.106 +private:
   1.107 +  friend class MinstrelWifiRemoteStation;
   1.108 +  virtual class WifiRemoteStation *CreateStation (void);
   1.109 +
   1.110 +  typedef std::vector<std::pair<Time,WifiMode> > TxTime;
   1.111 +
   1.112 +  TxTime m_calcTxTime;  ///< to hold all the calculated TxTime for all modes
   1.113 +  Time m_updateStats;  ///< how frequent do we calculate the stats(1/10 seconds)
   1.114 +  double m_lookAroundRate;  ///< the % to try other rates than our current rate 
   1.115 +  double m_ewmaLevel;  ///< exponential weighted moving average
   1.116 +  uint32_t m_segmentSize;  ///< largest allowable segment size
   1.117 +  uint32_t m_sampleCol;  ///< number of sample columns
   1.118 +  uint32_t m_pktLen;  ///< packet length used  for calculate mode TxTime
   1.119 +  
   1.120 +};
   1.121 +
   1.122 +class MinstrelWifiRemoteStation : public WifiRemoteStation
   1.123 +{
   1.124 +public:
   1.125 +  MinstrelWifiRemoteStation (Ptr<MinstrelWifiManager> stations);
   1.126 + 
   1.127 +  virtual ~MinstrelWifiRemoteStation ();
   1.128 +
   1.129 +protected:
   1.130 +
   1.131 +  /** 
   1.132 +   * when packet is successfully received
   1.133 +   * see wifi-remote-station-manager.h for more documentation
   1.134 +   */
   1.135 +  virtual void DoReportRxOk (double rxSnr, WifiMode txMode);
   1.136 +
   1.137 +  /// when RTS timeout expires
   1.138 +  virtual void DoReportRtsFailed (void);
   1.139 +
   1.140 +
   1.141 +  /**
   1.142 +   *
   1.143 +   * Retry Chain table is implemented here
   1.144 +   *
   1.145 +   * Try |         LOOKAROUND RATE              | NORMAL RATE
   1.146 +   *     | random < best    | random > best     |
   1.147 +   * --------------------------------------------------------------
   1.148 +   *  1  | Best throughput  | Random rate       | Best throughput
   1.149 +   *  2  | Random rate      | Best throughput   | Next best throughput
   1.150 +   *  3  | Best probability | Best probability  | Best probability
   1.151 +   *  4  | Lowest Baserate  | Lowest baserate   | Lowest baserate
   1.152 +   *
   1.153 +   * Note: For clarity, multiple blocks of if's and else's are used
   1.154 +   * After a failing 7 times, DoReportFinalDataFailed will be called
   1.155 +   */
   1.156 +  virtual void DoReportDataFailed (void);
   1.157 +
   1.158 +  /// when receive a CTS, associated with an RTS
   1.159 +  virtual void DoReportRtsOk (double ctsSnr, WifiMode ctsMode, double rtsSnr);
   1.160 +
   1.161 +  /// when an ACK, associated with a data pkt, is received
   1.162 +  virtual void DoReportDataOk (double ackSnr, WifiMode ackMode, double dataSnr);
   1.163 +
   1.164 +  /// after calling ReportRtsFailed if NeedRtsRetransmission returns false
   1.165 +  virtual void DoReportFinalRtsFailed (void);
   1.166 +
   1.167 +  /// after calling ReportDataFailed if NeedDataRetransmission returns false
   1.168 +  virtual void DoReportFinalDataFailed (void);
   1.169 +
   1.170 +
   1.171 +private:
   1.172 +  virtual Ptr<WifiRemoteStationManager> GetManager (void) const;
   1.173 +
   1.174 +  /** 
   1.175 +   * returns the transmission mode for sending this packet
   1.176 +   * this function gets called when node is getting ready to send DATA
   1.177 +   * see wifi-remote-station-manager.h for more documentation
   1.178 +   */
   1.179 +  virtual WifiMode DoGetDataMode (uint32_t size);
   1.180 +
   1.181 +  /// returns the transmission mode for sending RTS packet
   1.182 +  virtual WifiMode DoGetRtsMode (void);
   1.183 +
   1.184 +  /// update the number of retries and reset accordingly
   1.185 +  void UpdateRetry (void);
   1.186 +
   1.187 +  /// getting the next sample from Sample Table
   1.188 +  uint32_t GetNextSample (void);
   1.189 +
   1.190 +  /// find a rate to use from Minstrel Table
   1.191 +  uint32_t FindRate (void);
   1.192 +
   1.193 +  /// updating the Minstrel Table every 1/10 seconds
   1.194 +  void UpdateStats (void);
   1.195 +
   1.196 +  /// initialize Minstrel Table
   1.197 +  void RateInit (void);
   1.198 +
   1.199 +  /// initialize Sample Table
   1.200 +  void InitSampleTable (void);
   1.201 +
   1.202 +  /// printing Sample Table
   1.203 +  void PrintSampleTable (void);
   1.204 +
   1.205 +  /// printing Minstrel Table
   1.206 +  void PrintTable (void);
   1.207 +
   1.208 +  /**
   1.209 +   * \param packet lenghth 
   1.210 +   * \param current WifiMode
   1.211 +   * \returns calcuated transmit duration 
   1.212 +   */
   1.213 +  Time CalcRatePacket (uint32_t, WifiMode);
   1.214 +
   1.215 +  void CheckInit(void);  ///< check for initializations
   1.216 +
   1.217 +  Ptr<MinstrelWifiManager> m_stations;
   1.218 +
   1.219 +  Time m_nextStatsUpdate;  ///< 10 times every second
   1.220 +
   1.221 +  MinstrelRate m_minstrelTable;  ///< minstrel table	
   1.222 +
   1.223 +  SampleRate m_sampleTable;  ///< sample table
   1.224 +
   1.225 +  /**
   1.226 +   * To keep track of the current position in the our random sample table
   1.227 +   * going row by row from 1st column until the 10th column(Minstrel defines 10)
   1.228 +   * then we wrap back to the row 1 col 1.
   1.229 +   * note: there are many other ways to do this.
   1.230 +   */
   1.231 +  uint32_t m_col, m_index;							
   1.232 +
   1.233 +  uint32_t m_maxTpRate;  ///< the current throughput rate 
   1.234 +  uint32_t m_maxTpRate2;  ///< second highest throughput rate
   1.235 +  uint32_t m_maxProbRate;  ///< rate with highest prob of success
   1.236 +
   1.237 +  int m_packetCount;  ///< total number of packets as of now
   1.238 +  int m_sampleCount;  ///< how many packets we have sample so far
   1.239 +
   1.240 +  bool m_isSampling;  ///< a flag to indicate we are currently sampling
   1.241 +  uint32_t m_sampleRate;  ///< current sample rate
   1.242 +  bool 	m_sampleRateSlower;  ///< a flag to indicate sample rate is slower
   1.243 +  uint32_t m_currentRate;  ///< current rate we are using
   1.244 +
   1.245 +  uint32_t m_shortRetry;  ///< short retries such as control packts
   1.246 +  uint32_t m_longRetry;  ///< long retries such as data packets
   1.247 +  uint32_t m_retry;  ///< total retries short + long
   1.248 +  uint32_t m_err;  ///< retry errors
   1.249 +  uint32_t m_txrate;  ///< current transmit rate
   1.250 +
   1.251 +  bool m_initialized;  ///< for initializing tables
   1.252 +
   1.253 +
   1.254 +}; 
   1.255 +
   1.256 +}// namespace ns3
   1.257 +
   1.258 +#endif /* MINSTREL_WIFI_MANAGER_H */