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