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