Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 2 | /* |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, Regents of the University of California, |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Shuo Yang |
| 24 | * @author Weiwei Liu |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 25 | * @author Chavoosh Ghasemi |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "pipeline-interests-aimd.hpp" |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 29 | #include "data-fetcher.hpp" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 30 | |
| 31 | #include <cmath> |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 32 | #include <iomanip> |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace chunks { |
| 36 | namespace aimd { |
| 37 | |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 38 | constexpr double PipelineInterestsAimd::MIN_SSTHRESH; |
| 39 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 40 | PipelineInterestsAimd::PipelineInterestsAimd(Face& face, RttEstimator& rttEstimator, |
| 41 | const Options& options) |
| 42 | : PipelineInterests(face) |
| 43 | , m_options(options) |
| 44 | , m_rttEstimator(rttEstimator) |
| 45 | , m_scheduler(m_face.getIoService()) |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 46 | , m_checkRtoEvent(m_scheduler) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 47 | , m_highData(0) |
| 48 | , m_highInterest(0) |
| 49 | , m_recPoint(0) |
| 50 | , m_nInFlight(0) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 51 | , m_nLossEvents(0) |
| 52 | , m_nRetransmitted(0) |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 53 | , m_nCongMarks(0) |
Chavoosh Ghasemi | 75309ae | 2018-03-26 14:46:24 -0400 | [diff] [blame^] | 54 | , m_nSent(0) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 55 | , m_cwnd(m_options.initCwnd) |
| 56 | , m_ssthresh(m_options.initSsthresh) |
| 57 | , m_hasFailure(false) |
| 58 | , m_failedSegNo(0) |
| 59 | { |
| 60 | if (m_options.isVerbose) { |
| 61 | std::cerr << m_options; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | PipelineInterestsAimd::~PipelineInterestsAimd() |
| 66 | { |
| 67 | cancel(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | PipelineInterestsAimd::doRun() |
| 72 | { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 73 | // schedule the event to check retransmission timer |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 74 | m_checkRtoEvent = m_scheduler.scheduleEvent(m_options.rtoCheckInterval, [this] { checkRto(); }); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 76 | schedulePackets(); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
| 80 | PipelineInterestsAimd::doCancel() |
| 81 | { |
| 82 | for (const auto& entry : m_segmentInfo) { |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 83 | m_face.removePendingInterest(entry.second.interestId); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 84 | } |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 85 | m_checkRtoEvent.cancel(); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 86 | m_segmentInfo.clear(); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
| 90 | PipelineInterestsAimd::checkRto() |
| 91 | { |
| 92 | if (isStopping()) |
| 93 | return; |
| 94 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 95 | bool hasTimeout = false; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 96 | |
| 97 | for (auto& entry : m_segmentInfo) { |
| 98 | SegmentInfo& segInfo = entry.second; |
| 99 | if (segInfo.state != SegmentState::InRetxQueue && // do not check segments currently in the retx queue |
| 100 | segInfo.state != SegmentState::RetxReceived) { // or already-received retransmitted segments |
| 101 | Milliseconds timeElapsed = time::steady_clock::now() - segInfo.timeSent; |
| 102 | if (timeElapsed.count() > segInfo.rto.count()) { // timer expired? |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 103 | hasTimeout = true; |
| 104 | enqueueForRetransmission(entry.first); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 109 | if (hasTimeout) { |
| 110 | recordTimeout(); |
| 111 | schedulePackets(); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // schedule the next check after predefined interval |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 115 | m_checkRtoEvent = m_scheduler.scheduleEvent(m_options.rtoCheckInterval, [this] { checkRto(); }); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | PipelineInterestsAimd::sendInterest(uint64_t segNo, bool isRetransmission) |
| 120 | { |
| 121 | if (isStopping()) |
| 122 | return; |
| 123 | |
| 124 | if (m_hasFinalBlockId && segNo > m_lastSegmentNo && !isRetransmission) |
| 125 | return; |
| 126 | |
| 127 | if (!isRetransmission && m_hasFailure) |
| 128 | return; |
| 129 | |
| 130 | if (m_options.isVerbose) { |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 131 | std::cerr << (isRetransmission ? "Retransmitting" : "Requesting") |
| 132 | << " segment #" << segNo << std::endl; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (isRetransmission) { |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 136 | // keep track of retx count for this segment |
| 137 | auto ret = m_retxCount.emplace(segNo, 1); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 138 | if (ret.second == false) { // not the first retransmission |
| 139 | m_retxCount[segNo] += 1; |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 140 | if (m_options.maxRetriesOnTimeoutOrNack != DataFetcher::MAX_RETRIES_INFINITE && |
| 141 | m_retxCount[segNo] > m_options.maxRetriesOnTimeoutOrNack) { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 142 | return handleFail(segNo, "Reached the maximum number of retries (" + |
| 143 | to_string(m_options.maxRetriesOnTimeoutOrNack) + |
| 144 | ") while retrieving segment #" + to_string(segNo)); |
| 145 | } |
| 146 | |
| 147 | if (m_options.isVerbose) { |
| 148 | std::cerr << "# of retries for segment #" << segNo |
| 149 | << " is " << m_retxCount[segNo] << std::endl; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | m_face.removePendingInterest(m_segmentInfo[segNo].interestId); |
| 154 | } |
| 155 | |
| 156 | Interest interest(Name(m_prefix).appendSegment(segNo)); |
| 157 | interest.setInterestLifetime(m_options.interestLifetime); |
| 158 | interest.setMustBeFresh(m_options.mustBeFresh); |
| 159 | interest.setMaxSuffixComponents(1); |
| 160 | |
| 161 | auto interestId = m_face.expressInterest(interest, |
| 162 | bind(&PipelineInterestsAimd::handleData, this, _1, _2), |
| 163 | bind(&PipelineInterestsAimd::handleNack, this, _1, _2), |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 164 | bind(&PipelineInterestsAimd::handleLifetimeExpiration, this, _1)); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 165 | m_nInFlight++; |
Chavoosh Ghasemi | 75309ae | 2018-03-26 14:46:24 -0400 | [diff] [blame^] | 166 | m_nSent++; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 167 | |
| 168 | if (isRetransmission) { |
| 169 | SegmentInfo& segInfo = m_segmentInfo[segNo]; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 170 | segInfo.timeSent = time::steady_clock::now(); |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 171 | segInfo.rto = m_rttEstimator.getEstimatedRto(); |
| 172 | segInfo.state = SegmentState::Retransmitted; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 173 | m_nRetransmitted++; |
| 174 | } |
| 175 | else { |
| 176 | m_highInterest = segNo; |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 177 | m_segmentInfo[segNo] = {interestId, |
| 178 | time::steady_clock::now(), |
| 179 | m_rttEstimator.getEstimatedRto(), |
| 180 | SegmentState::FirstTimeSent}; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | PipelineInterestsAimd::schedulePackets() |
| 186 | { |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 187 | BOOST_ASSERT(m_nInFlight >= 0); |
| 188 | auto availableWindowSize = static_cast<int64_t>(m_cwnd) - m_nInFlight; |
| 189 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 190 | while (availableWindowSize > 0) { |
| 191 | if (!m_retxQueue.empty()) { // do retransmission first |
| 192 | uint64_t retxSegNo = m_retxQueue.front(); |
| 193 | m_retxQueue.pop(); |
| 194 | |
| 195 | auto it = m_segmentInfo.find(retxSegNo); |
| 196 | if (it == m_segmentInfo.end()) { |
| 197 | continue; |
| 198 | } |
| 199 | // the segment is still in the map, it means that it needs to be retransmitted |
| 200 | sendInterest(retxSegNo, true); |
| 201 | } |
| 202 | else { // send next segment |
| 203 | sendInterest(getNextSegmentNo(), false); |
| 204 | } |
| 205 | availableWindowSize--; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | PipelineInterestsAimd::handleData(const Interest& interest, const Data& data) |
| 211 | { |
| 212 | if (isStopping()) |
| 213 | return; |
| 214 | |
| 215 | // Data name will not have extra components because MaxSuffixComponents is set to 1 |
| 216 | BOOST_ASSERT(data.getName().equals(interest.getName())); |
| 217 | |
| 218 | if (!m_hasFinalBlockId && !data.getFinalBlockId().empty()) { |
| 219 | m_lastSegmentNo = data.getFinalBlockId().toSegment(); |
| 220 | m_hasFinalBlockId = true; |
| 221 | cancelInFlightSegmentsGreaterThan(m_lastSegmentNo); |
| 222 | if (m_hasFailure && m_lastSegmentNo >= m_failedSegNo) { |
| 223 | // previously failed segment is part of the content |
| 224 | return onFailure(m_failureReason); |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 225 | } |
| 226 | else { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 227 | m_hasFailure = false; |
| 228 | } |
| 229 | } |
| 230 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 231 | uint64_t recvSegNo = getSegmentFromPacket(data); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 232 | SegmentInfo& segInfo = m_segmentInfo[recvSegNo]; |
| 233 | if (segInfo.state == SegmentState::RetxReceived) { |
| 234 | m_segmentInfo.erase(recvSegNo); |
| 235 | return; // ignore already-received segment |
| 236 | } |
| 237 | |
| 238 | Milliseconds rtt = time::steady_clock::now() - segInfo.timeSent; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 239 | if (m_options.isVerbose) { |
| 240 | std::cerr << "Received segment #" << recvSegNo |
| 241 | << ", rtt=" << rtt.count() << "ms" |
| 242 | << ", rto=" << segInfo.rto.count() << "ms" << std::endl; |
| 243 | } |
| 244 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 245 | if (m_highData < recvSegNo) { |
| 246 | m_highData = recvSegNo; |
| 247 | } |
| 248 | |
| 249 | // for segments in retx queue, we must not decrement m_nInFlight |
| 250 | // because it was already decremented when the segment timed out |
| 251 | if (segInfo.state != SegmentState::InRetxQueue) { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 252 | m_nInFlight--; |
| 253 | } |
| 254 | |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 255 | // upon finding congestion mark, decrease the window size |
| 256 | // without retransmitting any packet |
| 257 | if (data.getCongestionMark() > 0) { |
| 258 | m_nCongMarks++; |
| 259 | if (!m_options.ignoreCongMarks) { |
| 260 | if (m_options.disableCwa || m_highData > m_recPoint) { |
| 261 | m_recPoint = m_highInterest; // react to only one congestion event (timeout or congestion mark) |
| 262 | // per RTT (conservative window adaptation) |
| 263 | decreaseWindow(); |
| 264 | |
| 265 | if (m_options.isVerbose) { |
| 266 | std::cerr << "Received congestion mark, value = " << data.getCongestionMark() |
| 267 | << ", new cwnd = " << m_cwnd << std::endl; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | else { |
| 272 | increaseWindow(); |
| 273 | } |
| 274 | } |
| 275 | else { |
| 276 | increaseWindow(); |
| 277 | } |
| 278 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 279 | onData(data); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 280 | |
| 281 | if (segInfo.state == SegmentState::FirstTimeSent || |
| 282 | segInfo.state == SegmentState::InRetxQueue) { // do not sample RTT for retransmitted segments |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 283 | auto nExpectedSamples = std::max<int64_t>((m_nInFlight + 1) >> 1, 1); |
| 284 | BOOST_ASSERT(nExpectedSamples > 0); |
| 285 | m_rttEstimator.addMeasurement(recvSegNo, rtt, static_cast<size_t>(nExpectedSamples)); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 286 | m_segmentInfo.erase(recvSegNo); // remove the entry associated with the received segment |
| 287 | } |
| 288 | else { // retransmission |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 289 | BOOST_ASSERT(segInfo.state == SegmentState::Retransmitted); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 290 | segInfo.state = SegmentState::RetxReceived; |
| 291 | } |
| 292 | |
| 293 | BOOST_ASSERT(m_nReceived > 0); |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 294 | if (m_hasFinalBlockId && |
| 295 | static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo) { // all segments have been received |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 296 | cancel(); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 297 | if (!m_options.isQuiet) { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 298 | printSummary(); |
| 299 | } |
| 300 | } |
| 301 | else { |
| 302 | schedulePackets(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | PipelineInterestsAimd::handleNack(const Interest& interest, const lp::Nack& nack) |
| 308 | { |
| 309 | if (isStopping()) |
| 310 | return; |
| 311 | |
| 312 | if (m_options.isVerbose) |
| 313 | std::cerr << "Received Nack with reason " << nack.getReason() |
| 314 | << " for Interest " << interest << std::endl; |
| 315 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 316 | uint64_t segNo = getSegmentFromPacket(interest); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 317 | |
| 318 | switch (nack.getReason()) { |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 319 | case lp::NackReason::DUPLICATE: |
| 320 | // ignore duplicates |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 321 | break; |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 322 | case lp::NackReason::CONGESTION: |
| 323 | // treated the same as timeout for now |
| 324 | enqueueForRetransmission(segNo); |
| 325 | recordTimeout(); |
| 326 | schedulePackets(); |
| 327 | break; |
| 328 | default: |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 329 | handleFail(segNo, "Could not retrieve data for " + interest.getName().toUri() + |
| 330 | ", reason: " + boost::lexical_cast<std::string>(nack.getReason())); |
| 331 | break; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | PipelineInterestsAimd::handleLifetimeExpiration(const Interest& interest) |
| 337 | { |
| 338 | if (isStopping()) |
| 339 | return; |
| 340 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 341 | enqueueForRetransmission(getSegmentFromPacket(interest)); |
| 342 | recordTimeout(); |
| 343 | schedulePackets(); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 347 | PipelineInterestsAimd::recordTimeout() |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 348 | { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 349 | if (m_options.disableCwa || m_highData > m_recPoint) { |
| 350 | // react to only one timeout per RTT (conservative window adaptation) |
| 351 | m_recPoint = m_highInterest; |
| 352 | |
| 353 | decreaseWindow(); |
| 354 | m_rttEstimator.backoffRto(); |
| 355 | m_nLossEvents++; |
| 356 | |
| 357 | if (m_options.isVerbose) { |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 358 | std::cerr << "Packet loss event, new cwnd = " << m_cwnd |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 359 | << ", ssthresh = " << m_ssthresh << std::endl; |
| 360 | } |
| 361 | } |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 362 | } |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 363 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 364 | void |
| 365 | PipelineInterestsAimd::enqueueForRetransmission(uint64_t segNo) |
| 366 | { |
| 367 | BOOST_ASSERT(m_nInFlight > 0); |
| 368 | m_nInFlight--; |
| 369 | m_retxQueue.push(segNo); |
| 370 | m_segmentInfo.at(segNo).state = SegmentState::InRetxQueue; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void |
| 374 | PipelineInterestsAimd::handleFail(uint64_t segNo, const std::string& reason) |
| 375 | { |
| 376 | if (isStopping()) |
| 377 | return; |
| 378 | |
| 379 | // if the failed segment is definitely part of the content, raise a fatal error |
| 380 | if (m_hasFinalBlockId && segNo <= m_lastSegmentNo) |
| 381 | return onFailure(reason); |
| 382 | |
| 383 | if (!m_hasFinalBlockId) { |
| 384 | m_segmentInfo.erase(segNo); |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 385 | m_nInFlight--; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 386 | |
| 387 | if (m_segmentInfo.empty()) { |
| 388 | onFailure("Fetching terminated but no final segment number has been found"); |
| 389 | } |
| 390 | else { |
| 391 | cancelInFlightSegmentsGreaterThan(segNo); |
| 392 | m_hasFailure = true; |
| 393 | m_failedSegNo = segNo; |
| 394 | m_failureReason = reason; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | PipelineInterestsAimd::increaseWindow() |
| 401 | { |
| 402 | if (m_cwnd < m_ssthresh) { |
| 403 | m_cwnd += m_options.aiStep; // additive increase |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 404 | } |
| 405 | else { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 406 | m_cwnd += m_options.aiStep / std::floor(m_cwnd); // congestion avoidance |
| 407 | } |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 408 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 409 | afterCwndChange(time::steady_clock::now() - getStartTime(), m_cwnd); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void |
| 413 | PipelineInterestsAimd::decreaseWindow() |
| 414 | { |
| 415 | // please refer to RFC 5681, Section 3.1 for the rationale behind it |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 416 | m_ssthresh = std::max(MIN_SSTHRESH, m_cwnd * m_options.mdCoef); // multiplicative decrease |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 417 | m_cwnd = m_options.resetCwndToInit ? m_options.initCwnd : m_ssthresh; |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 418 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 419 | afterCwndChange(time::steady_clock::now() - getStartTime(), m_cwnd); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 422 | void |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 423 | PipelineInterestsAimd::cancelInFlightSegmentsGreaterThan(uint64_t segNo) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 424 | { |
| 425 | for (auto it = m_segmentInfo.begin(); it != m_segmentInfo.end();) { |
| 426 | // cancel fetching all segments that follow |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 427 | if (it->first > segNo) { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 428 | m_face.removePendingInterest(it->second.interestId); |
| 429 | it = m_segmentInfo.erase(it); |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 430 | m_nInFlight--; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 431 | } |
| 432 | else { |
| 433 | ++it; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | PipelineInterestsAimd::printSummary() const |
| 440 | { |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 441 | PipelineInterests::printSummary(); |
Chavoosh Ghasemi | 75309ae | 2018-03-26 14:46:24 -0400 | [diff] [blame^] | 442 | std::cerr << "Total # of lost/retransmitted segments: " << m_nRetransmitted |
| 443 | << " (caused " << m_nLossEvents << " window decreases)\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 444 | << "Packet loss rate: " |
Chavoosh Ghasemi | 75309ae | 2018-03-26 14:46:24 -0400 | [diff] [blame^] | 445 | << (static_cast<double>(m_nRetransmitted) / static_cast<double>(m_nSent)) * 100 << "%\n" |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 446 | << "Total # of received congestion marks: " << m_nCongMarks << "\n" |
Chavoosh Ghasemi | 75309ae | 2018-03-26 14:46:24 -0400 | [diff] [blame^] | 447 | << "RTT "; |
| 448 | |
| 449 | if (m_rttEstimator.getMinRtt() == std::numeric_limits<double>::max() || |
| 450 | m_rttEstimator.getMaxRtt() == std::numeric_limits<double>::min()) { |
| 451 | std::cerr << "stats unavailable\n"; |
| 452 | } |
| 453 | else { |
| 454 | std::cerr << "min/avg/max = " << std::fixed << std::setprecision(3) |
| 455 | << m_rttEstimator.getMinRtt() << "/" |
| 456 | << m_rttEstimator.getAvgRtt() << "/" |
| 457 | << m_rttEstimator.getMaxRtt() << " ms\n"; |
| 458 | } |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | std::ostream& |
| 462 | operator<<(std::ostream& os, SegmentState state) |
| 463 | { |
| 464 | switch (state) { |
| 465 | case SegmentState::FirstTimeSent: |
| 466 | os << "FirstTimeSent"; |
| 467 | break; |
| 468 | case SegmentState::InRetxQueue: |
| 469 | os << "InRetxQueue"; |
| 470 | break; |
| 471 | case SegmentState::Retransmitted: |
| 472 | os << "Retransmitted"; |
| 473 | break; |
| 474 | case SegmentState::RetxReceived: |
| 475 | os << "RetxReceived"; |
| 476 | break; |
| 477 | } |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 478 | return os; |
| 479 | } |
| 480 | |
| 481 | std::ostream& |
| 482 | operator<<(std::ostream& os, const PipelineInterestsAimdOptions& options) |
| 483 | { |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 484 | os << "AIMD pipeline parameters:\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 485 | << "\tInitial congestion window size = " << options.initCwnd << "\n" |
| 486 | << "\tInitial slow start threshold = " << options.initSsthresh << "\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 487 | << "\tAdditive increase step = " << options.aiStep << "\n" |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 488 | << "\tMultiplicative decrease factor = " << options.mdCoef << "\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 489 | << "\tRTO check interval = " << options.rtoCheckInterval << "\n" |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 490 | << "\tMax retries on timeout or Nack = " << (options.maxRetriesOnTimeoutOrNack == DataFetcher::MAX_RETRIES_INFINITE ? |
| 491 | "infinite" : to_string(options.maxRetriesOnTimeoutOrNack)) << "\n" |
Chavoosh Ghasemi | 641f593 | 2017-11-06 22:45:11 +0000 | [diff] [blame] | 492 | << "\tReaction to congestion marks " << (options.ignoreCongMarks ? "disabled" : "enabled") << "\n" |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 493 | << "\tConservative window adaptation " << (options.disableCwa ? "disabled" : "enabled") << "\n" |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 494 | << "\tResetting cwnd to " << (options.resetCwndToInit ? "initCwnd" : "ssthresh") << " upon loss event\n"; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 495 | return os; |
| 496 | } |
| 497 | |
| 498 | } // namespace aimd |
| 499 | } // namespace chunks |
| 500 | } // namespace ndn |