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