Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2018, Regents of the University of California, |
| 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 8 | * |
| 9 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 10 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 11 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 15 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 18 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 19 | * <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 22 | * |
| 23 | * @author Shuo Yang |
| 24 | * @author Weiwei Liu |
| 25 | * @author Chavoosh Ghasemi |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "segment-fetcher.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 29 | #include "../name-component.hpp" |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 30 | #include "../encoding/buffer-stream.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 31 | #include "../lp/nack.hpp" |
| 32 | #include "../lp/nack-header.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 33 | |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 34 | #include <boost/asio/io_service.hpp> |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 35 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 36 | #include <cmath> |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 38 | namespace ndn { |
| 39 | namespace util { |
| 40 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 41 | constexpr double SegmentFetcher::MIN_SSTHRESH; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 42 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 43 | void |
| 44 | SegmentFetcher::Options::validate() |
| 45 | { |
| 46 | if (maxTimeout < 1_ms) { |
| 47 | BOOST_THROW_EXCEPTION(std::invalid_argument("maxTimeout must be greater than or equal to 1 millisecond")); |
| 48 | } |
| 49 | |
| 50 | if (initCwnd < 1.0) { |
| 51 | BOOST_THROW_EXCEPTION(std::invalid_argument("initCwnd must be greater than or equal to 1")); |
| 52 | } |
| 53 | |
| 54 | if (aiStep < 0.0) { |
| 55 | BOOST_THROW_EXCEPTION(std::invalid_argument("aiStep must be greater than or equal to 0")); |
| 56 | } |
| 57 | |
| 58 | if (mdCoef < 0.0 || mdCoef > 1.0) { |
| 59 | BOOST_THROW_EXCEPTION(std::invalid_argument("mdCoef must be in range [0, 1]")); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | SegmentFetcher::SegmentFetcher(Face& face, |
| 64 | security::v2::Validator& validator, |
| 65 | const SegmentFetcher::Options& options) |
| 66 | : m_options(options) |
| 67 | , m_face(face) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 68 | , m_scheduler(m_face.getIoService()) |
| 69 | , m_validator(validator) |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 70 | , m_rttEstimator(options.rttOptions) |
| 71 | , m_timeLastSegmentReceived(time::steady_clock::now()) |
| 72 | , m_nextSegmentNum(0) |
| 73 | , m_cwnd(options.initCwnd) |
| 74 | , m_ssthresh(options.initSsthresh) |
| 75 | , m_nSegmentsInFlight(0) |
| 76 | , m_nSegments(0) |
| 77 | , m_highInterest(0) |
| 78 | , m_highData(0) |
| 79 | , m_recPoint(0) |
| 80 | , m_nReceived(0) |
| 81 | , m_nBytesReceived(0) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 82 | { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 83 | m_options.validate(); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 86 | shared_ptr<SegmentFetcher> |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 87 | SegmentFetcher::start(Face& face, |
| 88 | const Interest& baseInterest, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 89 | security::v2::Validator& validator, |
| 90 | const SegmentFetcher::Options& options) |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 91 | { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 92 | shared_ptr<SegmentFetcher> fetcher(new SegmentFetcher(face, validator, options)); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 93 | fetcher->m_this = fetcher; |
| 94 | fetcher->fetchFirstSegment(baseInterest, false); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 95 | return fetcher; |
| 96 | } |
| 97 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 98 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 99 | SegmentFetcher::stop() |
| 100 | { |
| 101 | if (!m_this) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | for (const auto& pendingSegment : m_pendingSegments) { |
| 106 | m_face.removePendingInterest(pendingSegment.second.id); |
| 107 | if (pendingSegment.second.timeoutEvent) { |
| 108 | m_scheduler.cancelEvent(pendingSegment.second.timeoutEvent); |
| 109 | } |
| 110 | } |
| 111 | m_face.getIoService().post([self = std::move(m_this)] {}); |
| 112 | } |
| 113 | |
| 114 | bool |
| 115 | SegmentFetcher::shouldStop(const weak_ptr<SegmentFetcher>& weakSelf) |
| 116 | { |
| 117 | auto self = weakSelf.lock(); |
| 118 | return self == nullptr || self->m_this == nullptr; |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | SegmentFetcher::fetchFirstSegment(const Interest& baseInterest, bool isRetransmission) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 123 | { |
| 124 | Interest interest(baseInterest); |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame] | 125 | interest.setCanBePrefix(true); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 126 | interest.setMustBeFresh(true); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 127 | interest.setInterestLifetime(m_options.interestLifetime); |
| 128 | if (isRetransmission) { |
| 129 | interest.refreshNonce(); |
| 130 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 131 | |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 132 | weak_ptr<SegmentFetcher> weakSelf = m_this; |
| 133 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 134 | m_nSegmentsInFlight++; |
| 135 | auto pendingInterest = m_face.expressInterest(interest, |
| 136 | bind(&SegmentFetcher::afterSegmentReceivedCb, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 137 | this, _1, _2, weakSelf), |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 138 | bind(&SegmentFetcher::afterNackReceivedCb, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 139 | this, _1, _2, weakSelf), |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 140 | nullptr); |
| 141 | auto timeoutEvent = |
| 142 | m_scheduler.scheduleEvent(m_options.useConstantInterestTimeout ? m_options.maxTimeout : getEstimatedRto(), |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 143 | bind(&SegmentFetcher::afterTimeoutCb, this, interest, weakSelf)); |
| 144 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 145 | if (isRetransmission) { |
| 146 | updateRetransmittedSegment(0, pendingInterest, timeoutEvent); |
| 147 | } |
| 148 | else { |
| 149 | BOOST_ASSERT(m_pendingSegments.count(0) == 0); |
| 150 | m_pendingSegments.emplace(0, PendingSegment{SegmentState::FirstInterest, time::steady_clock::now(), |
| 151 | pendingInterest, timeoutEvent}); |
| 152 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 156 | SegmentFetcher::fetchSegmentsInWindow(const Interest& origInterest) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 157 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 158 | weak_ptr<SegmentFetcher> weakSelf = m_this; |
| 159 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 160 | if (checkAllSegmentsReceived()) { |
| 161 | // All segments have been retrieved |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 162 | return finalizeFetch(); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int64_t availableWindowSize = static_cast<int64_t>(m_cwnd) - m_nSegmentsInFlight; |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 166 | std::vector<std::pair<uint64_t, bool>> segmentsToRequest; // The boolean indicates whether a retx or not |
| 167 | |
| 168 | while (availableWindowSize > 0) { |
| 169 | if (!m_retxQueue.empty()) { |
| 170 | auto pendingSegmentIt = m_pendingSegments.find(m_retxQueue.front()); |
| 171 | m_retxQueue.pop(); |
| 172 | if (pendingSegmentIt == m_pendingSegments.end()) { |
| 173 | // Skip re-requesting this segment, since it was received after RTO timeout |
| 174 | continue; |
| 175 | } |
| 176 | BOOST_ASSERT(pendingSegmentIt->second.state == SegmentState::InRetxQueue); |
| 177 | segmentsToRequest.emplace_back(pendingSegmentIt->first, true); |
| 178 | } |
| 179 | else if (m_nSegments == 0 || m_nextSegmentNum < static_cast<uint64_t>(m_nSegments)) { |
| 180 | if (m_receivedSegments.count(m_nextSegmentNum) > 0) { |
| 181 | // Don't request a segment a second time if received in response to first "discovery" Interest |
| 182 | m_nextSegmentNum++; |
| 183 | continue; |
| 184 | } |
| 185 | segmentsToRequest.emplace_back(m_nextSegmentNum++, false); |
| 186 | } |
| 187 | else { |
| 188 | break; |
| 189 | } |
| 190 | availableWindowSize--; |
| 191 | } |
| 192 | |
| 193 | for (const auto& segment : segmentsToRequest) { |
| 194 | Interest interest(origInterest); // to preserve Interest elements |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 195 | interest.setName(Name(m_versionedDataName).appendSegment(segment.first)); |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame] | 196 | interest.setCanBePrefix(false); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 197 | interest.setMustBeFresh(false); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 198 | interest.setInterestLifetime(m_options.interestLifetime); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 199 | interest.refreshNonce(); |
| 200 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 201 | m_nSegmentsInFlight++; |
| 202 | auto pendingInterest = m_face.expressInterest(interest, |
| 203 | bind(&SegmentFetcher::afterSegmentReceivedCb, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 204 | this, _1, _2, weakSelf), |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 205 | bind(&SegmentFetcher::afterNackReceivedCb, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 206 | this, _1, _2, weakSelf), |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 207 | nullptr); |
| 208 | auto timeoutEvent = |
| 209 | m_scheduler.scheduleEvent(m_options.useConstantInterestTimeout ? m_options.maxTimeout : getEstimatedRto(), |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 210 | bind(&SegmentFetcher::afterTimeoutCb, this, interest, weakSelf)); |
| 211 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 212 | if (segment.second) { // Retransmission |
| 213 | updateRetransmittedSegment(segment.first, pendingInterest, timeoutEvent); |
| 214 | } |
| 215 | else { // First request for segment |
| 216 | BOOST_ASSERT(m_pendingSegments.count(segment.first) == 0); |
| 217 | m_pendingSegments.emplace(segment.first, PendingSegment{SegmentState::FirstInterest, |
| 218 | time::steady_clock::now(), |
| 219 | pendingInterest, timeoutEvent}); |
| 220 | m_highInterest = segment.first; |
| 221 | } |
| 222 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 226 | SegmentFetcher::afterSegmentReceivedCb(const Interest& origInterest, const Data& data, |
| 227 | const weak_ptr<SegmentFetcher>& weakSelf) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 228 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 229 | if (shouldStop(weakSelf)) |
| 230 | return; |
| 231 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 232 | afterSegmentReceived(data); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 233 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 234 | BOOST_ASSERT(m_nSegmentsInFlight > 0); |
| 235 | m_nSegmentsInFlight--; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 236 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 237 | name::Component currentSegmentComponent = data.getName().get(-1); |
| 238 | if (!currentSegmentComponent.isSegment()) { |
| 239 | return signalError(DATA_HAS_NO_SEGMENT, "Data Name has no segment number"); |
| 240 | } |
| 241 | |
| 242 | uint64_t currentSegment = currentSegmentComponent.toSegment(); |
| 243 | |
| 244 | // The first received Interest could have any segment ID |
| 245 | std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt; |
| 246 | if (m_receivedSegments.size() > 0) { |
| 247 | pendingSegmentIt = m_pendingSegments.find(currentSegment); |
| 248 | } |
| 249 | else { |
| 250 | pendingSegmentIt = m_pendingSegments.begin(); |
| 251 | } |
| 252 | |
| 253 | // Cancel timeout event |
| 254 | m_scheduler.cancelEvent(pendingSegmentIt->second.timeoutEvent); |
| 255 | pendingSegmentIt->second.timeoutEvent = nullptr; |
| 256 | |
| 257 | m_validator.validate(data, |
| 258 | bind(&SegmentFetcher::afterValidationSuccess, this, _1, origInterest, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 259 | pendingSegmentIt, weakSelf), |
| 260 | bind(&SegmentFetcher::afterValidationFailure, this, _1, _2, weakSelf)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 261 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 262 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 263 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 264 | SegmentFetcher::afterValidationSuccess(const Data& data, const Interest& origInterest, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 265 | std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 266 | const weak_ptr<SegmentFetcher>& weakSelf) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 267 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 268 | if (shouldStop(weakSelf)) |
| 269 | return; |
| 270 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 271 | // We update the last receive time here instead of in the segment received callback so that the |
| 272 | // transfer will not fail to terminate if we only received invalid Data packets. |
| 273 | m_timeLastSegmentReceived = time::steady_clock::now(); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 274 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 275 | m_nReceived++; |
| 276 | |
| 277 | // It was verified in afterSegmentReceivedCb that the last Data name component is a segment number |
| 278 | uint64_t currentSegment = data.getName().get(-1).toSegment(); |
| 279 | // Add measurement to RTO estimator (if not retransmission) |
| 280 | if (pendingSegmentIt->second.state == SegmentState::FirstInterest) { |
| 281 | m_rttEstimator.addMeasurement(m_timeLastSegmentReceived - pendingSegmentIt->second.sendTime, |
| 282 | std::max<int64_t>(m_nSegmentsInFlight + 1, 1)); |
| 283 | } |
| 284 | |
| 285 | // Remove from pending segments map |
| 286 | m_pendingSegments.erase(pendingSegmentIt); |
| 287 | |
| 288 | // Copy data in segment to temporary buffer |
| 289 | auto receivedSegmentIt = m_receivedSegments.emplace(std::piecewise_construct, |
| 290 | std::forward_as_tuple(currentSegment), |
| 291 | std::forward_as_tuple(data.getContent().value_size())); |
| 292 | std::copy(data.getContent().value_begin(), data.getContent().value_end(), |
| 293 | receivedSegmentIt.first->second.begin()); |
| 294 | m_nBytesReceived += data.getContent().value_size(); |
| 295 | afterSegmentValidated(data); |
| 296 | |
| 297 | if (data.getFinalBlock()) { |
| 298 | if (!data.getFinalBlock()->isSegment()) { |
| 299 | return signalError(FINALBLOCKID_NOT_SEGMENT, |
| 300 | "Received FinalBlockId did not contain a segment component"); |
| 301 | } |
| 302 | |
| 303 | if (data.getFinalBlock()->toSegment() + 1 != static_cast<uint64_t>(m_nSegments)) { |
| 304 | m_nSegments = data.getFinalBlock()->toSegment() + 1; |
| 305 | cancelExcessInFlightSegments(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (m_receivedSegments.size() == 1) { |
| 310 | m_versionedDataName = data.getName().getPrefix(-1); |
| 311 | if (currentSegment == 0) { |
| 312 | // We received the first segment in response, so we can increment the next segment number |
| 313 | m_nextSegmentNum++; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (m_highData < currentSegment) { |
| 318 | m_highData = currentSegment; |
| 319 | } |
| 320 | |
| 321 | if (data.getCongestionMark() > 0 && !m_options.ignoreCongMarks) { |
| 322 | windowDecrease(); |
| 323 | } |
| 324 | else { |
| 325 | windowIncrease(); |
| 326 | } |
| 327 | |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 328 | fetchSegmentsInWindow(origInterest); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void |
| 332 | SegmentFetcher::afterValidationFailure(const Data& data, |
| 333 | const security::v2::ValidationError& error, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 334 | const weak_ptr<SegmentFetcher>& weakSelf) |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 335 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 336 | if (shouldStop(weakSelf)) |
| 337 | return; |
| 338 | |
| 339 | signalError(SEGMENT_VALIDATION_FAIL, "Segment validation failed: " + boost::lexical_cast<std::string>(error)); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 342 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 343 | SegmentFetcher::afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack, |
| 344 | const weak_ptr<SegmentFetcher>& weakSelf) |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 345 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 346 | if (shouldStop(weakSelf)) |
| 347 | return; |
| 348 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 349 | afterSegmentNacked(); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 350 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 351 | BOOST_ASSERT(m_nSegmentsInFlight > 0); |
| 352 | m_nSegmentsInFlight--; |
| 353 | |
| 354 | switch (nack.getReason()) { |
| 355 | case lp::NackReason::DUPLICATE: |
| 356 | case lp::NackReason::CONGESTION: |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 357 | afterNackOrTimeout(origInterest); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 358 | break; |
| 359 | default: |
| 360 | signalError(NACK_ERROR, "Nack Error"); |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | SegmentFetcher::afterTimeoutCb(const Interest& origInterest, |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 367 | const weak_ptr<SegmentFetcher>& weakSelf) |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 368 | { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 369 | if (shouldStop(weakSelf)) |
| 370 | return; |
| 371 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 372 | afterSegmentTimedOut(); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 373 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 374 | BOOST_ASSERT(m_nSegmentsInFlight > 0); |
| 375 | m_nSegmentsInFlight--; |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 376 | afterNackOrTimeout(origInterest); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 380 | SegmentFetcher::afterNackOrTimeout(const Interest& origInterest) |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 381 | { |
| 382 | if (time::steady_clock::now() >= m_timeLastSegmentReceived + m_options.maxTimeout) { |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 383 | // Fail transfer due to exceeding the maximum timeout between the successful receipt of segments |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 384 | return signalError(INTEREST_TIMEOUT, "Timeout exceeded"); |
| 385 | } |
| 386 | |
| 387 | name::Component lastNameComponent = origInterest.getName().get(-1); |
| 388 | std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt; |
| 389 | BOOST_ASSERT(m_pendingSegments.size() > 0); |
| 390 | if (lastNameComponent.isSegment()) { |
| 391 | BOOST_ASSERT(m_pendingSegments.count(lastNameComponent.toSegment()) > 0); |
| 392 | pendingSegmentIt = m_pendingSegments.find(lastNameComponent.toSegment()); |
| 393 | } |
| 394 | else { // First Interest |
| 395 | BOOST_ASSERT(m_pendingSegments.size() > 0); |
| 396 | pendingSegmentIt = m_pendingSegments.begin(); |
| 397 | } |
| 398 | |
| 399 | // Cancel timeout event and set status to InRetxQueue |
| 400 | m_scheduler.cancelEvent(pendingSegmentIt->second.timeoutEvent); |
| 401 | pendingSegmentIt->second.timeoutEvent = nullptr; |
| 402 | pendingSegmentIt->second.state = SegmentState::InRetxQueue; |
| 403 | |
| 404 | m_rttEstimator.backoffRto(); |
| 405 | |
| 406 | if (m_receivedSegments.size() == 0) { |
| 407 | // Resend first Interest (until maximum receive timeout exceeded) |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 408 | fetchFirstSegment(origInterest, true); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 409 | } |
| 410 | else { |
| 411 | windowDecrease(); |
| 412 | m_retxQueue.push(pendingSegmentIt->first); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 413 | fetchSegmentsInWindow(origInterest); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | void |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 418 | SegmentFetcher::finalizeFetch() |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 419 | { |
| 420 | // Combine segments into final buffer |
| 421 | OBufferStream buf; |
| 422 | // We may have received more segments than exist in the object. |
| 423 | BOOST_ASSERT(m_receivedSegments.size() >= static_cast<uint64_t>(m_nSegments)); |
| 424 | |
| 425 | for (int64_t i = 0; i < m_nSegments; i++) { |
| 426 | buf.write(m_receivedSegments[i].get<const char>(), m_receivedSegments[i].size()); |
| 427 | } |
| 428 | |
| 429 | onComplete(buf.buf()); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 430 | stop(); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void |
| 434 | SegmentFetcher::windowIncrease() |
| 435 | { |
| 436 | if (m_options.useConstantCwnd) { |
| 437 | BOOST_ASSERT(m_cwnd == m_options.initCwnd); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | if (m_cwnd < m_ssthresh) { |
| 442 | m_cwnd += m_options.aiStep; // additive increase |
| 443 | } |
| 444 | else { |
| 445 | m_cwnd += m_options.aiStep / std::floor(m_cwnd); // congestion avoidance |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | SegmentFetcher::windowDecrease() |
| 451 | { |
| 452 | if (m_options.disableCwa || m_highData > m_recPoint) { |
| 453 | m_recPoint = m_highInterest; |
| 454 | |
| 455 | if (m_options.useConstantCwnd) { |
| 456 | BOOST_ASSERT(m_cwnd == m_options.initCwnd); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // Refer to RFC 5681, Section 3.1 for the rationale behind the code below |
| 461 | m_ssthresh = std::max(MIN_SSTHRESH, m_cwnd * m_options.mdCoef); // multiplicative decrease |
| 462 | m_cwnd = m_options.resetCwndToInit ? m_options.initCwnd : m_ssthresh; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void |
| 467 | SegmentFetcher::signalError(uint32_t code, const std::string& msg) |
| 468 | { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 469 | onError(code, msg); |
Ashlesh Gawande | 679dbb0 | 2018-08-21 11:43:21 -0500 | [diff] [blame^] | 470 | stop(); |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void |
| 474 | SegmentFetcher::updateRetransmittedSegment(uint64_t segmentNum, |
| 475 | const PendingInterestId* pendingInterest, |
| 476 | scheduler::EventId timeoutEvent) |
| 477 | { |
| 478 | auto pendingSegmentIt = m_pendingSegments.find(segmentNum); |
| 479 | BOOST_ASSERT(pendingSegmentIt != m_pendingSegments.end()); |
| 480 | BOOST_ASSERT(pendingSegmentIt->second.state == SegmentState::InRetxQueue); |
| 481 | pendingSegmentIt->second.state = SegmentState::Retransmitted; |
| 482 | pendingSegmentIt->second.id = pendingInterest; |
| 483 | pendingSegmentIt->second.timeoutEvent = timeoutEvent; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | SegmentFetcher::cancelExcessInFlightSegments() |
| 488 | { |
| 489 | for (auto it = m_pendingSegments.begin(); it != m_pendingSegments.end();) { |
| 490 | if (it->first >= static_cast<uint64_t>(m_nSegments)) { |
| 491 | m_face.removePendingInterest(it->second.id); |
| 492 | if (it->second.timeoutEvent) { |
| 493 | m_scheduler.cancelEvent(it->second.timeoutEvent); |
| 494 | } |
| 495 | it = m_pendingSegments.erase(it); |
| 496 | BOOST_ASSERT(m_nSegmentsInFlight > 0); |
| 497 | m_nSegmentsInFlight--; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 498 | } |
| 499 | else { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 500 | ++it; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 505 | bool |
| 506 | SegmentFetcher::checkAllSegmentsReceived() |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 507 | { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 508 | bool haveReceivedAllSegments = false; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 509 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 510 | if (m_nSegments != 0 && m_nReceived >= m_nSegments) { |
| 511 | haveReceivedAllSegments = true; |
| 512 | // Verify that all segments in window have been received. If not, send Interests for missing segments. |
| 513 | for (uint64_t i = 0; i < static_cast<uint64_t>(m_nSegments); i++) { |
| 514 | if (m_receivedSegments.count(i) == 0) { |
| 515 | m_retxQueue.push(i); |
| 516 | haveReceivedAllSegments = false; |
| 517 | } |
| 518 | } |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 521 | return haveReceivedAllSegments; |
| 522 | } |
| 523 | |
| 524 | time::milliseconds |
| 525 | SegmentFetcher::getEstimatedRto() |
| 526 | { |
| 527 | // We don't want an Interest timeout greater than the maximum allowed timeout between the |
| 528 | // succesful receipt of segments |
| 529 | return std::min(m_options.maxTimeout, |
| 530 | time::duration_cast<time::milliseconds>(m_rttEstimator.getEstimatedRto())); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | } // namespace util |
| 534 | } // namespace ndn |