00001 /***************************************************************************** 00002 Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above 00010 copyright notice, this list of conditions and the 00011 following disclaimer. 00012 00013 * Redistributions in binary form must reproduce the 00014 above copyright notice, this list of conditions 00015 and the following disclaimer in the documentation 00016 and/or other materials provided with the distribution. 00017 00018 * Neither the name of the University of Illinois 00019 nor the names of its contributors may be used to 00020 endorse or promote products derived from this 00021 software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00024 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00025 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00026 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00028 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00029 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00031 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 *****************************************************************************/ 00035 00036 /***************************************************************************** 00037 written by 00038 Yunhong Gu, last updated 02/28/2012 00039 *****************************************************************************/ 00040 00041 00042 #ifndef __UDT_CCC_H__ 00043 #define __UDT_CCC_H__ 00044 00045 00046 #include "udt.h" 00047 #include "packet.h" 00048 00049 00050 class UDT_API CCC 00051 { 00052 friend class CUDT; 00053 00054 public: 00055 CCC(); 00056 virtual ~CCC(); 00057 00058 private: 00059 CCC(const CCC&); 00060 CCC& operator=(const CCC&) {return *this;} 00061 00062 public: 00063 00064 // Functionality: 00065 // Callback function to be called (only) at the start of a UDT connection. 00066 // note that this is different from CCC(), which is always called. 00067 // Parameters: 00068 // None. 00069 // Returned value: 00070 // None. 00071 00072 virtual void init() {} 00073 00074 // Functionality: 00075 // Callback function to be called when a UDT connection is closed. 00076 // Parameters: 00077 // None. 00078 // Returned value: 00079 // None. 00080 00081 virtual void close() {} 00082 00083 // Functionality: 00084 // Callback function to be called when an ACK packet is received. 00085 // Parameters: 00086 // 0) [in] ackno: the data sequence number acknowledged by this ACK. 00087 // Returned value: 00088 // None. 00089 00090 virtual void onACK(int32_t) {} 00091 00092 // Functionality: 00093 // Callback function to be called when a loss report is received. 00094 // Parameters: 00095 // 0) [in] losslist: list of sequence number of packets, in the format describled in packet.cpp. 00096 // 1) [in] size: length of the loss list. 00097 // Returned value: 00098 // None. 00099 00100 virtual void onLoss(const int32_t*, int) {} 00101 00102 // Functionality: 00103 // Callback function to be called when a timeout event occurs. 00104 // Parameters: 00105 // None. 00106 // Returned value: 00107 // None. 00108 00109 virtual void onTimeout() {} 00110 00111 // Functionality: 00112 // Callback function to be called when a data is sent. 00113 // Parameters: 00114 // 0) [in] seqno: the data sequence number. 00115 // 1) [in] size: the payload size. 00116 // Returned value: 00117 // None. 00118 00119 virtual void onPktSent(const CPacket*) {} 00120 00121 // Functionality: 00122 // Callback function to be called when a data is received. 00123 // Parameters: 00124 // 0) [in] seqno: the data sequence number. 00125 // 1) [in] size: the payload size. 00126 // Returned value: 00127 // None. 00128 00129 virtual void onPktReceived(const CPacket*) {} 00130 00131 // Functionality: 00132 // Callback function to Process a user defined packet. 00133 // Parameters: 00134 // 0) [in] pkt: the user defined packet. 00135 // Returned value: 00136 // None. 00137 00138 virtual void processCustomMsg(const CPacket*) {} 00139 00140 protected: 00141 00142 // Functionality: 00143 // Set periodical acknowldging and the ACK period. 00144 // Parameters: 00145 // 0) [in] msINT: the period to send an ACK. 00146 // Returned value: 00147 // None. 00148 00149 void setACKTimer(int msINT); 00150 00151 // Functionality: 00152 // Set packet-based acknowldging and the number of packets to send an ACK. 00153 // Parameters: 00154 // 0) [in] pktINT: the number of packets to send an ACK. 00155 // Returned value: 00156 // None. 00157 00158 void setACKInterval(int pktINT); 00159 00160 // Functionality: 00161 // Set RTO value. 00162 // Parameters: 00163 // 0) [in] msRTO: RTO in macroseconds. 00164 // Returned value: 00165 // None. 00166 00167 void setRTO(int usRTO); 00168 00169 // Functionality: 00170 // Send a user defined control packet. 00171 // Parameters: 00172 // 0) [in] pkt: user defined packet. 00173 // Returned value: 00174 // None. 00175 00176 void sendCustomMsg(CPacket& pkt) const; 00177 00178 // Functionality: 00179 // retrieve performance information. 00180 // Parameters: 00181 // None. 00182 // Returned value: 00183 // Pointer to a performance info structure. 00184 00185 const CPerfMon* getPerfInfo(); 00186 00187 // Functionality: 00188 // Set user defined parameters. 00189 // Parameters: 00190 // 0) [in] param: the paramters in one buffer. 00191 // 1) [in] size: the size of the buffer. 00192 // Returned value: 00193 // None. 00194 00195 void setUserParam(const char* param, int size); 00196 00197 private: 00198 void setMSS(int mss); 00199 void setMaxCWndSize(int cwnd); 00200 void setBandwidth(int bw); 00201 void setSndCurrSeqNo(int32_t seqno); 00202 void setRcvRate(int rcvrate); 00203 void setRTT(int rtt); 00204 00205 protected: 00206 const int32_t& m_iSYNInterval; // UDT constant parameter, SYN 00207 00208 double m_dPktSndPeriod; // Packet sending period, in microseconds 00209 double m_dCWndSize; // Congestion window size, in packets 00210 00211 int m_iBandwidth; // estimated bandwidth, packets per second 00212 double m_dMaxCWndSize; // maximum cwnd size, in packets 00213 00214 int m_iMSS; // Maximum Packet Size, including all packet headers 00215 int32_t m_iSndCurrSeqNo; // current maximum seq no sent out 00216 int m_iRcvRate; // packet arrive rate at receiver side, packets per second 00217 int m_iRTT; // current estimated RTT, microsecond 00218 00219 char* m_pcParam; // user defined parameter 00220 int m_iPSize; // size of m_pcParam 00221 00222 private: 00223 UDTSOCKET m_UDT; // The UDT entity that this congestion control algorithm is bound to 00224 00225 int m_iACKPeriod; // Periodical timer to send an ACK, in milliseconds 00226 int m_iACKInterval; // How many packets to send one ACK, in packets 00227 00228 bool m_bUserDefinedRTO; // if the RTO value is defined by users 00229 int m_iRTO; // RTO value, microseconds 00230 00231 CPerfMon m_PerfInfo; // protocol statistics information 00232 }; 00233 00234 class CCCVirtualFactory 00235 { 00236 public: 00237 virtual ~CCCVirtualFactory() {} 00238 00239 virtual CCC* create() = 0; 00240 virtual CCCVirtualFactory* clone() = 0; 00241 }; 00242 00243 template <class T> 00244 class CCCFactory: public CCCVirtualFactory 00245 { 00246 public: 00247 virtual ~CCCFactory() {} 00248 00249 virtual CCC* create() {return new T;} 00250 virtual CCCVirtualFactory* clone() {return new CCCFactory<T>;} 00251 }; 00252 00253 class CUDTCC: public CCC 00254 { 00255 public: 00256 CUDTCC(); 00257 00258 public: 00259 virtual void init(); 00260 virtual void onACK(int32_t); 00261 virtual void onLoss(const int32_t*, int); 00262 virtual void onTimeout(); 00263 00264 private: 00265 int m_iRCInterval; // UDT Rate control interval 00266 uint64_t m_LastRCTime; // last rate increase time 00267 bool m_bSlowStart; // if in slow start phase 00268 int32_t m_iLastAck; // last ACKed seq no 00269 bool m_bLoss; // if loss happened since last rate increase 00270 int32_t m_iLastDecSeq; // max pkt seq no sent out when last decrease happened 00271 double m_dLastDecPeriod; // value of pktsndperiod when last decrease happened 00272 int m_iNAKCount; // NAK counter 00273 int m_iDecRandom; // random threshold on decrease by number of loss events 00274 int m_iAvgNAKNum; // average number of NAKs per congestion 00275 int m_iDecCount; // number of decreases in a congestion epoch 00276 }; 00277 00278 #endif