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