| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
// This is a controversial thing, so temporarily blocking |
| 13 |
//#define SRT_ENABLE_SYSTEMBUFFER_TRACE |
| 14 |
|
| 15 |
#include "platform_sys.h" |
| 16 |
|
| 17 |
|
| 18 |
#ifdef SRT_ENABLE_SYSTEMBUFFER_TRACE |
| 19 |
#if defined(unix) |
| 20 |
// XXX will be nonportable |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <cmath> |
| 27 |
|
| 28 |
|
| 29 |
#include "common.h" |
| 30 |
#include "core.h" |
| 31 |
#include "queue.h" |
| 32 |
#include "packet.h" |
| 33 |
#include "congctl.h" |
| 34 |
#include "logging.h" |
| 35 |
|
| 36 |
using namespace std; |
| 37 |
using namespace srt::sync; |
| 38 |
using namespace srt_logging; |
| 39 |
|
| 40 |
namespace srt { |
| 41 |
|
| 42 |
SrtCongestionControlBase::SrtCongestionControlBase(CUDT* parent) |
| 43 |
{ |
| 44 |
m_parent = parent; |
| 45 |
m_dMaxCWndSize = m_parent->flowWindowSize(); |
| 46 |
// RcvRate (deliveryRate()), RTT and Bandwidth can be read directly from CUDT when needed. |
| 47 |
m_dCWndSize = 1000; |
| 48 |
m_dPktSndPeriod = 1; |
| 49 |
} |
| 50 |
|
| 51 |
void SrtCongestion::Check() |
| 52 |
{ |
| 53 |
if (!congctl) |
| 54 |
throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0); |
| 55 |
} |
| 56 |
|
| 57 |
// Useful macro to shorthand passing a method as argument |
| 58 |
// Requires "Me" name by which a class refers to itself |
| 59 |
#define SSLOT(method) EventSlot(this, &Me:: method) |
| 60 |
|
| 61 |
class LiveCC: public SrtCongestionControlBase |
| 62 |
{ |
| 63 |
srt::sync::atomic<int64_t> m_llSndMaxBW; //Max bandwidth (bytes/sec) |
| 64 |
srt::sync::atomic<size_t> m_zSndAvgPayloadSize; //Average Payload Size of packets to xmit |
| 65 |
size_t m_zMaxPayloadSize; |
| 66 |
size_t m_zHeaderSize; |
| 67 |
|
| 68 |
// NAKREPORT stuff. |
| 69 |
int m_iMinNakInterval_us; // Minimum NAK Report Period (usec) |
| 70 |
int m_iNakReportAccel; // NAK Report Period (RTT) accelerator |
| 71 |
|
| 72 |
typedef LiveCC Me; // required for SSLOT macro |
| 73 |
|
| 74 |
public: |
| 75 |
|
| 76 |
LiveCC(CUDT* parent) |
| 77 |
: SrtCongestionControlBase(parent) |
| 78 |
{ |
| 79 |
m_llSndMaxBW = BW_INFINITE; // 1 Gbbps in Bytes/sec BW_INFINITE |
| 80 |
m_zMaxPayloadSize = parent->OPT_PayloadSize(); |
| 81 |
if (m_zMaxPayloadSize == 0) |
| 82 |
m_zMaxPayloadSize = parent->maxPayloadSize(); |
| 83 |
m_zSndAvgPayloadSize = m_zMaxPayloadSize; |
| 84 |
|
| 85 |
m_zHeaderSize = parent->m_config.iMSS - parent->maxPayloadSize(); |
| 86 |
|
| 87 |
m_iMinNakInterval_us = 20000; //Minimum NAK Report Period (usec) |
| 88 |
m_iNakReportAccel = 2; //Default NAK Report Period (RTT) accelerator (send periodic NAK every RTT/2) |
| 89 |
|
| 90 |
HLOGC(cclog.Debug, log << "Creating LiveCC: bw=" << m_llSndMaxBW << " avgplsize=" << m_zSndAvgPayloadSize); |
| 91 |
|
| 92 |
updatePktSndPeriod(); |
| 93 |
|
| 94 |
|
| 95 |
// NOTE: TEV_SEND gets dispatched from Sending thread, all others |
| 96 |
// from receiving thread. |
| 97 |
parent->ConnectSignal(TEV_SEND, SSLOT(updatePayloadSize)); |
| 98 |
|
| 99 |
// |
| 100 |
// Adjust the max SndPeriod onACK and onTimeout. |
| 101 |
// |
| 102 |
parent->ConnectSignal(TEV_CHECKTIMER, SSLOT(onRTO)); |
| 103 |
parent->ConnectSignal(TEV_ACK, SSLOT(onAck)); |
| 104 |
} |
| 105 |
|
| 106 |
bool checkTransArgs(SrtCongestion::TransAPI api, SrtCongestion::TransDir dir, const char* , size_t size, int , bool ) ATR_OVERRIDE |
| 107 |
{ |
| 108 |
if (api != SrtCongestion::STA_MESSAGE) |
| 109 |
{ |
| 110 |
LOGC(cclog.Error, log << "LiveCC: invalid API use. Only sendmsg/recvmsg allowed."); |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
if (dir == SrtCongestion::STAD_SEND) |
| 115 |
{ |
| 116 |
// For sending, check if the size of data doesn't exceed the maximum live packet size. |
| 117 |
if (size > m_zMaxPayloadSize) |
| 118 |
{ |
| 119 |
LOGC(cclog.Error, log << "LiveCC: payload size: " << size << " exceeds maximum allowed " << m_zMaxPayloadSize); |
| 120 |
return false; |
| 121 |
} |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
// For receiving, check if the buffer has enough space to keep the payload. |
| 126 |
if (size < m_zMaxPayloadSize) |
| 127 |
{ |
| 128 |
LOGC(cclog.Error, log << "LiveCC: buffer size: " << size << " is too small for the maximum possible " << m_zMaxPayloadSize); |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
// XXX You can decide here if the not-fully-packed packet should require immediate ACK or not. |
| 137 |
// bool needsQuickACK(const CPacket& pkt) ATR_OVERRIDE |
| 138 |
|
| 139 |
virtual int64_t sndBandwidth() ATR_OVERRIDE { return m_llSndMaxBW; } |
| 140 |
|
| 141 |
private: |
| 142 |
// SLOTS: |
| 143 |
|
| 144 |
// TEV_SEND -> CPacket*. |
| 145 |
void updatePayloadSize(ETransmissionEvent, EventVariant var) |
| 146 |
{ |
| 147 |
const CPacket& packet = *var.get<EventVariant::PACKET>(); |
| 148 |
|
| 149 |
// XXX NOTE: TEV_SEND is sent from CSndQueue::worker thread, which is |
| 150 |
// different to threads running any other events (TEV_CHECKTIMER and TEV_ACK). |
| 151 |
// The m_zSndAvgPayloadSize field is however left unguarded because |
| 152 |
// there's no other modifier of this field. |
| 153 |
// Worst case scenario, the procedure running in CRcvQueue::worker |
| 154 |
// thread will pick up a "slightly outdated" average value from this |
| 155 |
// field - this is insignificant. |
| 156 |
m_zSndAvgPayloadSize = avg_iir<128, size_t>(m_zSndAvgPayloadSize, packet.getLength()); |
| 157 |
HLOGC(cclog.Debug, log << "LiveCC: avg payload size updated: " << m_zSndAvgPayloadSize); |
| 158 |
} |
| 159 |
|
| 160 |
/// @brief On RTO event update an inter-packet send interval. |
| 161 |
/// @param arg EventVariant::STAGE to distinguish between INIT and actual RTO. |
| 162 |
void onRTO(ETransmissionEvent , EventVariant var) |
| 163 |
{ |
| 164 |
if (var.get<EventVariant::STAGE>() != TEV_CHT_INIT ) |
| 165 |
updatePktSndPeriod(); |
| 166 |
} |
| 167 |
|
| 168 |
/// @brief Handle an incoming ACK event. |
| 169 |
/// Mainly updates a send interval between packets relying on the maximum BW limit. |
| 170 |
void onAck(ETransmissionEvent, EventVariant ) |
| 171 |
{ |
| 172 |
updatePktSndPeriod(); |
| 173 |
} |
| 174 |
|
| 175 |
/// @brief Updates a send interval between packets relying on the maximum BW limit. |
| 176 |
void updatePktSndPeriod() |
| 177 |
{ |
| 178 |
// packet = payload + header |
| 179 |
const double pktsize = (double) m_zSndAvgPayloadSize.load() + m_zHeaderSize; |
| 180 |
m_dPktSndPeriod = 1000 * 1000.0 * (pktsize / m_llSndMaxBW); |
| 181 |
HLOGC(cclog.Debug, log << "LiveCC: sending period updated: " << m_dPktSndPeriod |
| 182 |
<< " by avg pktsize=" << m_zSndAvgPayloadSize |
| 183 |
<< ", bw=" << m_llSndMaxBW); |
| 184 |
} |
| 185 |
|
| 186 |
void setMaxBW(int64_t maxbw) |
| 187 |
{ |
| 188 |
m_llSndMaxBW = maxbw > 0 ? maxbw : BW_INFINITE; |
| 189 |
updatePktSndPeriod(); |
| 190 |
|
| 191 |
/* |
| 192 |
* UDT default flow control should not trigger under normal SRT operation |
| 193 |
* UDT stops sending if the number of packets in transit (not acknowledged) |
| 194 |
* is larger than the congestion window. |
| 195 |
* Up to SRT 1.0.6, this value was set at 1000 pkts, which may be insufficient |
| 196 |
* for satellite links with ~1000 msec RTT and high bit rate. |
| 197 |
*/ |
| 198 |
// XXX Consider making this a socket option. |
| 199 |
m_dCWndSize = m_dMaxCWndSize; |
| 200 |
} |
| 201 |
|
| 202 |
void updateBandwidth(int64_t maxbw, int64_t bw) ATR_OVERRIDE |
| 203 |
{ |
| 204 |
// bw is the bandwidth calculated with regard to the |
| 205 |
// SRTO_INPUTBW and SRTO_OHEADBW parameters. The maxbw |
| 206 |
// value simply represents the SRTO_MAXBW setting. |
| 207 |
if (maxbw) |
| 208 |
{ |
| 209 |
setMaxBW(maxbw); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
if (bw == 0) |
| 214 |
{ |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
setMaxBW(bw); |
| 219 |
} |
| 220 |
|
| 221 |
SrtCongestion::RexmitMethod rexmitMethod() ATR_OVERRIDE |
| 222 |
{ |
| 223 |
return SrtCongestion::SRM_FASTREXMIT; |
| 224 |
} |
| 225 |
|
| 226 |
int64_t updateNAKInterval(int64_t nakint_us, int /*rcv_speed*/, size_t /*loss_length*/) ATR_OVERRIDE |
| 227 |
{ |
| 228 |
/* |
| 229 |
* duB: |
| 230 |
* The RTT accounts for the time for the last NAK to reach sender and start resending lost pkts. |
| 231 |
* The rcv_speed add the time to resend all the pkts in the loss list. |
| 232 |
* |
| 233 |
* For realtime Transport Stream content, pkts/sec is not a good indication of time to transmit |
| 234 |
* since packets are not filled to m_iMSS and packet size average is lower than (7*188) |
| 235 |
* for low bit rates. |
| 236 |
* If NAK report is lost, another cycle (RTT) is required which is bad for low latency so we |
| 237 |
* accelerate the NAK Reports frequency, at the cost of possible duplicate resend. |
| 238 |
* Finally, the UDT4 native minimum NAK interval (m_ullMinNakInt_tk) is 300 ms which is too high |
| 239 |
* (~10 i30 video frames) to maintain low latency. |
| 240 |
*/ |
| 241 |
|
| 242 |
// Note: this value will still be reshaped to defined minimum, |
| 243 |
// as per minNAKInterval. |
| 244 |
return nakint_us / m_iNakReportAccel; |
| 245 |
} |
| 246 |
|
| 247 |
int64_t minNAKInterval() ATR_OVERRIDE |
| 248 |
{ |
| 249 |
return m_iMinNakInterval_us; |
| 250 |
} |
| 251 |
|
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
class FileCC : public SrtCongestionControlBase |
| 256 |
{ |
| 257 |
typedef FileCC Me; // Required by SSLOT macro |
| 258 |
|
| 259 |
// Fields from CUDTCC |
| 260 |
int m_iRCInterval; // UDT Rate control interval |
| 261 |
steady_clock::time_point m_LastRCTime; // last rate increase time |
| 262 |
bool m_bSlowStart; // if in slow start phase |
| 263 |
int32_t m_iLastAck; // last ACKed seq no |
| 264 |
bool m_bLoss; // if loss happened since last rate increase |
| 265 |
int32_t m_iLastDecSeq; // max pkt seq no sent out when last decrease happened |
| 266 |
double m_dLastDecPeriod; // value of pktsndperiod when last decrease happened |
| 267 |
int m_iNAKCount; // NAK counter |
| 268 |
int m_iDecRandom; // random threshold on decrease by number of loss events |
| 269 |
int m_iAvgNAKNum; // average number of NAKs per congestion |
| 270 |
int m_iDecCount; // number of decreases in a congestion epoch |
| 271 |
|
| 272 |
int64_t m_maxSR; |
| 273 |
|
| 274 |
public: |
| 275 |
|
| 276 |
FileCC(CUDT* parent) |
| 277 |
: SrtCongestionControlBase(parent) |
| 278 |
, m_iRCInterval(CUDT::COMM_SYN_INTERVAL_US) |
| 279 |
, m_LastRCTime(steady_clock::now()) |
| 280 |
, m_bSlowStart(true) |
| 281 |
, m_iLastAck(parent->sndSeqNo()) |
| 282 |
, m_bLoss(false) |
| 283 |
, m_iLastDecSeq(CSeqNo::decseq(m_iLastAck)) |
| 284 |
, m_dLastDecPeriod(1) |
| 285 |
, m_iNAKCount(0) |
| 286 |
, m_iDecRandom(1) |
| 287 |
, m_iAvgNAKNum(0) |
| 288 |
, m_iDecCount(0) |
| 289 |
, m_maxSR(0) |
| 290 |
{ |
| 291 |
// Note that this function is called at the moment of |
| 292 |
// calling m_Smoother.configure(this). It is placed more less |
| 293 |
// at the same position as the series-of-parameter-setting-then-init |
| 294 |
// in the original UDT code. So, old CUDTCC::init() can be moved |
| 295 |
// to constructor. |
| 296 |
|
| 297 |
// SmotherBase |
| 298 |
m_dCWndSize = 16; |
| 299 |
m_dPktSndPeriod = 1; |
| 300 |
|
| 301 |
parent->ConnectSignal(TEV_ACK, SSLOT(onACK)); |
| 302 |
parent->ConnectSignal(TEV_LOSSREPORT, SSLOT(onLossReport)); |
| 303 |
parent->ConnectSignal(TEV_CHECKTIMER, SSLOT(onRTO)); |
| 304 |
|
| 305 |
HLOGC(cclog.Debug, log << "Creating FileCC"); |
| 306 |
} |
| 307 |
|
| 308 |
bool checkTransArgs(SrtCongestion::TransAPI, SrtCongestion::TransDir, const char*, size_t, int, bool) ATR_OVERRIDE |
| 309 |
{ |
| 310 |
// XXX |
| 311 |
// The FileCC has currently no restrictions, although it should be |
| 312 |
// rather required that the "message" mode or "buffer" mode be used on both sides the same. |
| 313 |
// This must be somehow checked separately. |
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
/// Tells if an early ACK is needed (before the next Full ACK happening every 10ms). |
| 318 |
/// In FileCC, treat non-full-payload as an end-of-message (stream) |
| 319 |
/// and request ACK to be sent immediately. |
| 320 |
bool needsQuickACK(const CPacket& pkt) ATR_OVERRIDE |
| 321 |
{ |
| 322 |
if (pkt.getLength() < m_parent->maxPayloadSize()) |
| 323 |
{ |
| 324 |
// This is not a regular fixed size packet... |
| 325 |
// an irregular sized packet usually indicates the end of a message, so send an ACK immediately |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
void updateBandwidth(int64_t maxbw, int64_t) ATR_OVERRIDE |
| 333 |
{ |
| 334 |
if (maxbw != 0) |
| 335 |
{ |
| 336 |
m_maxSR = maxbw; |
| 337 |
HLOGC(cclog.Debug, log << "FileCC: updated BW: " << m_maxSR); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
private: |
| 342 |
/// Handle icoming ACK event. |
| 343 |
/// In slow start stage increase CWND. Leave slow start once maximum CWND is reached. |
| 344 |
/// In congestion avoidance stage adjust inter packet send interval value to achieve maximum rate. |
| 345 |
void onACK(ETransmissionEvent, EventVariant arg) |
| 346 |
{ |
| 347 |
const int ack = arg.get<EventVariant::ACK>(); |
| 348 |
|
| 349 |
const steady_clock::time_point currtime = steady_clock::now(); |
| 350 |
if (count_microseconds(currtime - m_LastRCTime) < m_iRCInterval) |
| 351 |
return; |
| 352 |
|
| 353 |
m_LastRCTime = currtime; |
| 354 |
|
| 355 |
if (m_bSlowStart) |
| 356 |
{ |
| 357 |
m_dCWndSize += CSeqNo::seqlen(m_iLastAck, ack); |
| 358 |
m_iLastAck = ack; |
| 359 |
|
| 360 |
if (m_dCWndSize > m_dMaxCWndSize) |
| 361 |
{ |
| 362 |
m_bSlowStart = false; |
| 363 |
if (m_parent->deliveryRate() > 0) |
| 364 |
{ |
| 365 |
m_dPktSndPeriod = 1000000.0 / m_parent->deliveryRate(); |
| 366 |
HLOGC(cclog.Debug, log << "FileCC: UPD (slowstart:ENDED) wndsize=" |
| 367 |
<< m_dCWndSize << "/" << m_dMaxCWndSize |
| 368 |
<< " sndperiod=" << m_dPktSndPeriod << "us = 1M/(" |
| 369 |
<< m_parent->deliveryRate() << " pkts/s)"); |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
m_dPktSndPeriod = m_dCWndSize / (m_parent->SRTT() + m_iRCInterval); |
| 374 |
HLOGC(cclog.Debug, log << "FileCC: UPD (slowstart:ENDED) wndsize=" |
| 375 |
<< m_dCWndSize << "/" << m_dMaxCWndSize |
| 376 |
<< " sndperiod=" << m_dPktSndPeriod << "us = wndsize/(RTT+RCIV) RTT=" |
| 377 |
<< m_parent->SRTT() << " RCIV=" << m_iRCInterval); |
| 378 |
} |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
HLOGC(cclog.Debug, log << "FileCC: UPD (slowstart:KEPT) wndsize=" |
| 383 |
<< m_dCWndSize << "/" << m_dMaxCWndSize |
| 384 |
<< " sndperiod=" << m_dPktSndPeriod << "us"); |
| 385 |
} |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
m_dCWndSize = m_parent->deliveryRate() / 1000000.0 * (m_parent->SRTT() + m_iRCInterval) + 16; |
| 390 |
HLOGC(cclog.Debug, log << "FileCC: UPD (speed mode) wndsize=" |
| 391 |
<< m_dCWndSize << "/" << m_dMaxCWndSize << " RTT = " << m_parent->SRTT() |
| 392 |
<< " sndperiod=" << m_dPktSndPeriod << "us. deliverRate = " |
| 393 |
<< m_parent->deliveryRate() << " pkts/s)"); |
| 394 |
} |
| 395 |
|
| 396 |
if (!m_bSlowStart) |
| 397 |
{ |
| 398 |
if (m_bLoss) |
| 399 |
{ |
| 400 |
m_bLoss = false; |
| 401 |
} |
| 402 |
// During Slow Start, no rate increase |
| 403 |
else |
| 404 |
{ |
| 405 |
double inc = 0; |
| 406 |
const int loss_bw = static_cast<int>(2 * (1000000 / m_dLastDecPeriod)); // 2 times last loss point |
| 407 |
const int bw_pktps = min(loss_bw, m_parent->bandwidth()); |
| 408 |
|
| 409 |
int64_t B = (int64_t)(bw_pktps - 1000000.0 / m_dPktSndPeriod); |
| 410 |
if ((m_dPktSndPeriod > m_dLastDecPeriod) && ((bw_pktps / 9) < B)) |
| 411 |
B = bw_pktps / 9; |
| 412 |
if (B <= 0) |
| 413 |
inc = 1.0 / m_parent->MSS(); |
| 414 |
else |
| 415 |
{ |
| 416 |
// inc = max(10 ^ ceil(log10( B * MSS * 8 ) * Beta / MSS, 1/MSS) |
| 417 |
// Beta = 1.5 * 10^(-6) |
| 418 |
|
| 419 |
inc = pow(10.0, ceil(log10(B * m_parent->MSS() * 8.0))) * 0.0000015 / m_parent->MSS(); |
| 420 |
inc = max(inc, 1.0 / m_parent->MSS()); |
| 421 |
} |
| 422 |
|
| 423 |
HLOGC(cclog.Debug, log << "FileCC: UPD (slowstart:OFF) loss_bw=" << loss_bw |
| 424 |
<< " bandwidth=" << m_parent->bandwidth() << " inc=" << inc |
| 425 |
<< " m_dPktSndPeriod=" << m_dPktSndPeriod |
| 426 |
<< "->" << (m_dPktSndPeriod * m_iRCInterval) / (m_dPktSndPeriod * inc + m_iRCInterval)); |
| 427 |
|
| 428 |
m_dPktSndPeriod = (m_dPktSndPeriod * m_iRCInterval) / (m_dPktSndPeriod * inc + m_iRCInterval); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
#if ENABLE_HEAVY_LOGGING |
| 433 |
// Try to do reverse-calculation for m_dPktSndPeriod, as per minSP below |
| 434 |
// sndperiod = mega / (maxbw / MSS) |
| 435 |
// 1/sndperiod = (maxbw/MSS) / mega |
| 436 |
// mega/sndperiod = maxbw/MSS |
| 437 |
// maxbw = (MSS*mega)/sndperiod |
| 438 |
uint64_t usedbw = (m_parent->MSS() * 1000000.0) / m_dPktSndPeriod; |
| 439 |
|
| 440 |
#if defined(unix) && defined (SRT_ENABLE_SYSTEMBUFFER_TRACE) |
| 441 |
// Check the outgoing system queue level |
| 442 |
int udp_buffer_size = m_parent->sndQueue()->sockoptQuery(SOL_SOCKET, SO_SNDBUF); |
| 443 |
int udp_buffer_level = m_parent->sndQueue()->ioctlQuery(TIOCOUTQ); |
| 444 |
int udp_buffer_free = udp_buffer_size - udp_buffer_level; |
| 445 |
#else |
| 446 |
int udp_buffer_free = -1; |
| 447 |
#endif |
| 448 |
|
| 449 |
HLOGC(cclog.Debug, log << "FileCC: UPD (slowstart:" |
| 450 |
<< (m_bSlowStart ? "ON" : "OFF") << ") wndsize=" << m_dCWndSize |
| 451 |
<< " sndperiod=" << m_dPktSndPeriod << "us BANDWIDTH USED:" << usedbw << " (limit: " << m_maxSR << ")" |
| 452 |
" SYSTEM BUFFER LEFT: " << udp_buffer_free); |
| 453 |
#endif |
| 454 |
|
| 455 |
//set maximum transfer rate |
| 456 |
if (m_maxSR) |
| 457 |
{ |
| 458 |
double minSP = 1000000.0 / (double(m_maxSR) / m_parent->MSS()); |
| 459 |
if (m_dPktSndPeriod < minSP) |
| 460 |
{ |
| 461 |
m_dPktSndPeriod = minSP; |
| 462 |
HLOGC(cclog.Debug, log << "FileCC: BW limited to " << m_maxSR |
| 463 |
<< " - SLOWDOWN sndperiod=" << m_dPktSndPeriod << "us"); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
/// When a lossreport has been received, it might be due to having |
| 470 |
/// reached the available bandwidth limit. Slowdown to avoid further losses. |
| 471 |
/// Leave the slow start stage if it was active. |
| 472 |
void onLossReport(ETransmissionEvent, EventVariant arg) |
| 473 |
{ |
| 474 |
const int32_t* losslist = arg.get_ptr(); |
| 475 |
size_t losslist_size = arg.get_len(); |
| 476 |
|
| 477 |
// Sanity check. Should be impossible that TEV_LOSSREPORT event |
| 478 |
// is called with a nonempty loss list. |
| 479 |
if (losslist_size == 0) |
| 480 |
{ |
| 481 |
LOGC(cclog.Error, log << "IPE: FileCC: empty loss list!"); |
| 482 |
return; |
| 483 |
} |
| 484 |
|
| 485 |
//Slow Start stopped, if it hasn't yet |
| 486 |
if (m_bSlowStart) |
| 487 |
{ |
| 488 |
m_bSlowStart = false; |
| 489 |
if (m_parent->deliveryRate() > 0) |
| 490 |
{ |
| 491 |
m_dPktSndPeriod = 1000000.0 / m_parent->deliveryRate(); |
| 492 |
HLOGC(cclog.Debug, log << "FileCC: LOSS, SLOWSTART:OFF, sndperiod=" << m_dPktSndPeriod << "us AS mega/rate (rate=" |
| 493 |
<< m_parent->deliveryRate() << ")"); |
| 494 |
} |
| 495 |
else |
| 496 |
{ |
| 497 |
m_dPktSndPeriod = m_dCWndSize / (m_parent->SRTT() + m_iRCInterval); |
| 498 |
HLOGC(cclog.Debug, log << "FileCC: LOSS, SLOWSTART:OFF, sndperiod=" << m_dPktSndPeriod << "us AS wndsize/(RTT+RCIV) (RTT=" |
| 499 |
<< m_parent->SRTT() << " RCIV=" << m_iRCInterval << ")"); |
| 500 |
} |
| 501 |
|
| 502 |
} |
| 503 |
|
| 504 |
m_bLoss = true; |
| 505 |
|
| 506 |
// TODO: const int pktsInFlight = CSeqNo::seqoff(m_iLastAck, m_parent->sndSeqNo()); |
| 507 |
const int pktsInFlight = static_cast<int>(m_parent->SRTT() / m_dPktSndPeriod); |
| 508 |
const int numPktsLost = m_parent->sndLossLength(); |
| 509 |
const int lost_pcent_x10 = pktsInFlight > 0 ? (numPktsLost * 1000) / pktsInFlight : 0; |
| 510 |
|
| 511 |
HLOGC(cclog.Debug, log << "FileCC: LOSS: " |
| 512 |
<< "sent=" << CSeqNo::seqlen(m_iLastAck, m_parent->sndSeqNo()) << ", inFlight=" << pktsInFlight |
| 513 |
<< ", lost=" << numPktsLost << " (" |
| 514 |
<< lost_pcent_x10 / 10 << "." << lost_pcent_x10 % 10 << "%)"); |
| 515 |
if (lost_pcent_x10 < 20) // 2.0% |
| 516 |
{ |
| 517 |
HLOGC(cclog.Debug, log << "FileCC: LOSS: m_dLastDecPeriod=" << m_dLastDecPeriod << "->" << m_dPktSndPeriod); |
| 518 |
m_dLastDecPeriod = m_dPktSndPeriod; |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
// In contradiction to UDT, TEV_LOSSREPORT will be reported also when |
| 523 |
// the lossreport is being sent again, periodically, as a result of |
| 524 |
// NAKREPORT feature. You should make sure that NAKREPORT is off when |
| 525 |
// using FileCC, so relying on SRTO_TRANSTYPE rather than |
| 526 |
// just SRTO_CONGESTION is recommended. |
| 527 |
int32_t lossbegin = SEQNO_VALUE::unwrap(losslist[0]); |
| 528 |
|
| 529 |
if (CSeqNo::seqcmp(lossbegin, m_iLastDecSeq) > 0) |
| 530 |
{ |
| 531 |
m_dLastDecPeriod = m_dPktSndPeriod; |
| 532 |
m_dPktSndPeriod = ceil(m_dPktSndPeriod * 1.03); |
| 533 |
|
| 534 |
const double loss_share_factor = 0.03; |
| 535 |
m_iAvgNAKNum = (int)ceil(m_iAvgNAKNum * (1 - loss_share_factor) + m_iNAKCount * loss_share_factor); |
| 536 |
m_iNAKCount = 1; |
| 537 |
m_iDecCount = 1; |
| 538 |
|
| 539 |
m_iLastDecSeq = m_parent->sndSeqNo(); |
| 540 |
|
| 541 |
m_iDecRandom = m_iAvgNAKNum > 1 ? genRandomInt(1, m_iAvgNAKNum) : 1; |
| 542 |
SRT_ASSERT(m_iDecRandom >= 1); |
| 543 |
HLOGC(cclog.Debug, log << "FileCC: LOSS:NEW lseqno=" << lossbegin |
| 544 |
<< ", lastsentseqno=" << m_iLastDecSeq |
| 545 |
<< ", seqdiff=" << CSeqNo::seqoff(m_iLastDecSeq, lossbegin) |
| 546 |
<< ", rand=" << m_iDecRandom |
| 547 |
<< " avg NAK:" << m_iAvgNAKNum |
| 548 |
<< ", sndperiod=" << m_dPktSndPeriod << "us"); |
| 549 |
} |
| 550 |
else if ((m_iDecCount++ < 5) && (0 == (++m_iNAKCount % m_iDecRandom))) |
| 551 |
{ |
| 552 |
// 0.875^5 = 0.51, rate should not be decreased by more than half within a congestion period |
| 553 |
m_dPktSndPeriod = ceil(m_dPktSndPeriod * 1.03); |
| 554 |
m_iLastDecSeq = m_parent->sndSeqNo(); |
| 555 |
HLOGC(cclog.Debug, log << "FileCC: LOSS:PERIOD lseqno=" << lossbegin |
| 556 |
<< ", lastsentseqno=" << m_iLastDecSeq |
| 557 |
<< ", seqdiff=" << CSeqNo::seqoff(m_iLastDecSeq, lossbegin) |
| 558 |
<< ", deccnt=" << m_iDecCount |
| 559 |
<< ", decrnd=" << m_iDecRandom |
| 560 |
<< ", sndperiod=" << m_dPktSndPeriod << "us"); |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
HLOGC(cclog.Debug, log << "FileCC: LOSS:STILL lseqno=" << lossbegin |
| 565 |
<< ", lastsentseqno=" << m_iLastDecSeq |
| 566 |
<< ", seqdiff=" << CSeqNo::seqoff(m_iLastDecSeq, lossbegin) |
| 567 |
<< ", deccnt=" << m_iDecCount |
| 568 |
<< ", decrnd=" << m_iDecRandom |
| 569 |
<< ", sndperiod=" << m_dPktSndPeriod << "us"); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
/// @brief On retransmission timeout leave slow start stage if it was active. |
| 574 |
/// @param arg EventVariant::STAGE to distinguish between INIT and actual RTO. |
| 575 |
void onRTO(ETransmissionEvent, EventVariant arg) |
| 576 |
{ |
| 577 |
ECheckTimerStage stg = arg.get<EventVariant::STAGE>(); |
| 578 |
|
| 579 |
// TEV_INIT is in the beginning of checkTimers(), used |
| 580 |
// only to synchronize back the values (which is done in updateCC |
| 581 |
// after emitting the signal). |
| 582 |
if (stg == TEV_CHT_INIT) |
| 583 |
return; |
| 584 |
|
| 585 |
if (m_bSlowStart) |
| 586 |
{ |
| 587 |
m_bSlowStart = false; |
| 588 |
if (m_parent->deliveryRate() > 0) |
| 589 |
{ |
| 590 |
m_dPktSndPeriod = 1000000.0 / m_parent->deliveryRate(); |
| 591 |
HLOGC(cclog.Debug, log << "FileCC: CHKTIMER, SLOWSTART:OFF, sndperiod=" << m_dPktSndPeriod << "us AS mega/rate (rate=" |
| 592 |
<< m_parent->deliveryRate() << ")"); |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
m_dPktSndPeriod = m_dCWndSize / (m_parent->SRTT() + m_iRCInterval); |
| 597 |
HLOGC(cclog.Debug, log << "FileCC: CHKTIMER, SLOWSTART:OFF, sndperiod=" << m_dPktSndPeriod << "us AS wndsize/(RTT+RCIV) (wndsize=" |
| 598 |
<< m_dCWndSize << " RTT=" << m_parent->SRTT() << " RCIV=" << m_iRCInterval << ")"); |
| 599 |
} |
| 600 |
} |
| 601 |
else |
| 602 |
{ |
| 603 |
// XXX This code is a copy of legacy CUDTCC::onTimeout() body. |
| 604 |
// This part was commented out there already. |
| 605 |
/* |
| 606 |
m_dLastDecPeriod = m_dPktSndPeriod; |
| 607 |
m_dPktSndPeriod = ceil(m_dPktSndPeriod * 2); |
| 608 |
m_iLastDecSeq = m_iLastAck; |
| 609 |
*/ |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
SrtCongestion::RexmitMethod rexmitMethod() ATR_OVERRIDE |
| 614 |
{ |
| 615 |
return SrtCongestion::SRM_LATEREXMIT; |
| 616 |
} |
| 617 |
}; |
| 618 |
|
| 619 |
|
| 620 |
#undef SSLOT |
| 621 |
|
| 622 |
template <class Target> |
| 623 |
struct Creator |
| 624 |
{ |
| 625 |
static SrtCongestionControlBase* Create(CUDT* parent) { return new Target(parent); } |
| 626 |
}; |
| 627 |
|
| 628 |
SrtCongestion::NamePtr SrtCongestion::congctls[N_CONTROLLERS] = |
| 629 |
{ |
| 630 |
{"live", Creator<LiveCC>::Create }, |
| 631 |
{"file", Creator<FileCC>::Create } |
| 632 |
}; |
| 633 |
|
| 634 |
|
| 635 |
bool SrtCongestion::configure(CUDT* parent) |
| 636 |
{ |
| 637 |
if (selector == N_CONTROLLERS) |
| 638 |
return false; |
| 639 |
|
| 640 |
// Found a congctl, so call the creation function |
| 641 |
congctl = (*congctls[selector].second)(parent); |
| 642 |
|
| 643 |
// The congctl should have pinned in all events |
| 644 |
// that are of its interest. It's stated that |
| 645 |
// it's ready after creation. |
| 646 |
return !!congctl; |
| 647 |
} |
| 648 |
|
| 649 |
void SrtCongestion::dispose() |
| 650 |
{ |
| 651 |
if (congctl) |
| 652 |
{ |
| 653 |
delete congctl; |
| 654 |
congctl = 0; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
SrtCongestion::~SrtCongestion() |
| 659 |
{ |
| 660 |
dispose(); |
| 661 |
} |
| 662 |
|
| 663 |
} // namespace srt |