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