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