Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 2 | /** |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
| 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 |
| 25 | */ |
| 26 | |
| 27 | #ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_AIMD_HPP |
| 28 | #define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_AIMD_HPP |
| 29 | |
| 30 | #include "options.hpp" |
| 31 | #include "aimd-rtt-estimator.hpp" |
| 32 | #include "pipeline-interests.hpp" |
| 33 | |
| 34 | #include <queue> |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 35 | #include <unordered_map> |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace chunks { |
| 39 | namespace aimd { |
| 40 | |
Davide Pesavento | 92998fe | 2017-01-18 21:04:52 -0500 | [diff] [blame] | 41 | class PipelineInterestsAimdOptions : public Options |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 42 | { |
Davide Pesavento | 92998fe | 2017-01-18 21:04:52 -0500 | [diff] [blame] | 43 | public: |
| 44 | explicit |
| 45 | PipelineInterestsAimdOptions(const Options& options = Options()) |
| 46 | : Options(options) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | public: |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 51 | double initCwnd = 1.0; ///< initial congestion window size |
Davide Pesavento | 92998fe | 2017-01-18 21:04:52 -0500 | [diff] [blame] | 52 | double initSsthresh = std::numeric_limits<double>::max(); ///< initial slow start threshold |
| 53 | double aiStep = 1.0; ///< additive increase step (in segments) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 54 | double mdCoef = 0.5; ///< multiplicative decrease coefficient |
Davide Pesavento | 92998fe | 2017-01-18 21:04:52 -0500 | [diff] [blame] | 55 | time::milliseconds rtoCheckInterval{10}; ///< interval for checking retransmission timer |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 56 | bool disableCwa = false; ///< disable Conservative Window Adaptation |
| 57 | bool resetCwndToInit = false; ///< reduce cwnd to initCwnd when loss event occurs |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * @brief indicates the state of the segment |
| 62 | */ |
| 63 | enum class SegmentState { |
| 64 | FirstTimeSent, ///< segment has been sent for the first time |
| 65 | InRetxQueue, ///< segment is in retransmission queue |
| 66 | Retransmitted, ///< segment has been retransmitted |
| 67 | RetxReceived, ///< segment has been received after retransmission |
| 68 | }; |
| 69 | |
| 70 | std::ostream& |
| 71 | operator<<(std::ostream& os, SegmentState state); |
| 72 | |
| 73 | /** |
| 74 | * @brief Wraps up information that's necessary for segment transmission |
| 75 | */ |
| 76 | struct SegmentInfo |
| 77 | { |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 78 | const PendingInterestId* interestId; ///< pending interest ID returned by ndn::Face::expressInterest |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 79 | time::steady_clock::TimePoint timeSent; |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 80 | Milliseconds rto; |
| 81 | SegmentState state; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * @brief Service for retrieving Data via an Interest pipeline |
| 86 | * |
| 87 | * Retrieves all segmented Data under the specified prefix by maintaining a dynamic AIMD |
| 88 | * congestion window combined with a Conservative Loss Adaptation algorithm. For details, |
| 89 | * please refer to the description in section "Interest pipeline types in ndncatchunks" of |
| 90 | * tools/chunks/README.md |
| 91 | * |
| 92 | * Provides retrieved Data on arrival with no ordering guarantees. Data is delivered to the |
| 93 | * PipelineInterests' user via callback immediately upon arrival. |
| 94 | */ |
| 95 | class PipelineInterestsAimd : public PipelineInterests |
| 96 | { |
| 97 | public: |
| 98 | typedef PipelineInterestsAimdOptions Options; |
| 99 | |
| 100 | public: |
| 101 | /** |
| 102 | * @brief create a PipelineInterestsAimd service |
| 103 | * |
| 104 | * Configures the pipelining service without specifying the retrieval namespace. After this |
| 105 | * configuration the method run must be called to start the Pipeline. |
| 106 | */ |
| 107 | PipelineInterestsAimd(Face& face, RttEstimator& rttEstimator, |
| 108 | const Options& options = Options()); |
| 109 | |
| 110 | ~PipelineInterestsAimd() final; |
| 111 | |
| 112 | /** |
| 113 | * @brief Signals when cwnd changes |
| 114 | * |
| 115 | * The callback function should be: void(Milliseconds age, double cwnd) where age is the |
| 116 | * duration since pipeline starts, and cwnd is the new congestion window size (in segments). |
| 117 | */ |
| 118 | signal::Signal<PipelineInterestsAimd, Milliseconds, double> afterCwndChange; |
| 119 | |
| 120 | private: |
| 121 | /** |
| 122 | * @brief fetch all the segments between 0 and lastSegment of the specified prefix |
| 123 | * |
| 124 | * Starts the pipeline with an AIMD algorithm to control the window size. The pipeline will fetch |
| 125 | * every segment until the last segment is successfully received or an error occurs. |
| 126 | * The segment with segment number equal to m_excludedSegmentNo will not be fetched. |
| 127 | */ |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 128 | void |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 129 | doRun() final; |
| 130 | |
| 131 | /** |
| 132 | * @brief stop all fetch operations |
| 133 | */ |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 134 | void |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 135 | doCancel() final; |
| 136 | |
| 137 | /** |
| 138 | * @brief check RTO for all sent-but-not-acked segments. |
| 139 | */ |
| 140 | void |
| 141 | checkRto(); |
| 142 | |
| 143 | /** |
| 144 | * @param segNo the segment # of the to-be-sent Interest |
| 145 | * @param isRetransmission true if this is a retransmission |
| 146 | */ |
| 147 | void |
| 148 | sendInterest(uint64_t segNo, bool isRetransmission); |
| 149 | |
| 150 | void |
| 151 | schedulePackets(); |
| 152 | |
| 153 | void |
| 154 | handleData(const Interest& interest, const Data& data); |
| 155 | |
| 156 | void |
| 157 | handleNack(const Interest& interest, const lp::Nack& nack); |
| 158 | |
| 159 | void |
| 160 | handleLifetimeExpiration(const Interest& interest); |
| 161 | |
| 162 | void |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 163 | recordTimeout(); |
| 164 | |
| 165 | void |
| 166 | enqueueForRetransmission(uint64_t segNo); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 167 | |
| 168 | void |
| 169 | handleFail(uint64_t segNo, const std::string& reason); |
| 170 | |
| 171 | /** |
| 172 | * @brief increase congestion window size based on AIMD scheme |
| 173 | */ |
| 174 | void |
| 175 | increaseWindow(); |
| 176 | |
| 177 | /** |
| 178 | * @brief decrease congestion window size based on AIMD scheme |
| 179 | */ |
| 180 | void |
| 181 | decreaseWindow(); |
| 182 | |
| 183 | /** \return next segment number to retrieve |
| 184 | * \post m_nextSegmentNo == return-value + 1 |
| 185 | */ |
| 186 | uint64_t |
| 187 | getNextSegmentNo(); |
| 188 | |
| 189 | void |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 190 | cancelInFlightSegmentsGreaterThan(uint64_t segNo); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 191 | |
| 192 | void |
| 193 | printSummary() const; |
| 194 | |
| 195 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 196 | const Options m_options; |
| 197 | RttEstimator& m_rttEstimator; |
| 198 | Scheduler m_scheduler; |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 199 | scheduler::ScopedEventId m_checkRtoEvent; |
| 200 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 201 | uint64_t m_nextSegmentNo; |
| 202 | size_t m_receivedSize; |
| 203 | |
| 204 | uint64_t m_highData; ///< the highest segment number of the Data packet the consumer has received so far |
| 205 | uint64_t m_highInterest; ///< the highest segment number of the Interests the consumer has sent so far |
Davide Pesavento | cd65c2c | 2017-01-15 16:10:38 -0500 | [diff] [blame] | 206 | uint64_t m_recPoint; ///< the value of m_highInterest when a packet loss event occurred, |
| 207 | ///< it remains fixed until the next packet loss event happens |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 208 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 209 | int64_t m_nInFlight; ///< # of segments in flight |
| 210 | int64_t m_nReceived; ///< # of segments received |
| 211 | int64_t m_nLossEvents; ///< # of loss events occurred |
| 212 | int64_t m_nRetransmitted; ///< # of segments retransmitted |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 213 | |
| 214 | double m_cwnd; ///< current congestion window size (in segments) |
| 215 | double m_ssthresh; ///< current slow start threshold |
| 216 | |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 217 | std::unordered_map<uint64_t, SegmentInfo> m_segmentInfo; ///< keeps all the internal information |
| 218 | ///< on sent but not acked segments |
| 219 | std::unordered_map<uint64_t, int> m_retxCount; ///< maps segment number to its retransmission count; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 220 | ///< if the count reaches to the maximum number of |
| 221 | ///< timeout/nack retries, the pipeline will be aborted |
Davide Pesavento | 958896e | 2017-01-19 00:52:04 -0500 | [diff] [blame] | 222 | std::queue<uint64_t> m_retxQueue; |
| 223 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 224 | bool m_hasFailure; |
| 225 | uint64_t m_failedSegNo; |
| 226 | std::string m_failureReason; |
| 227 | }; |
| 228 | |
| 229 | std::ostream& |
| 230 | operator<<(std::ostream& os, const PipelineInterestsAimdOptions& options); |
| 231 | |
| 232 | } // namespace aimd |
| 233 | |
| 234 | using aimd::PipelineInterestsAimd; |
| 235 | |
| 236 | } // namespace chunks |
| 237 | } // namespace ndn |
| 238 | |
| 239 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_AIMD_HPP |