blob: 158656eb93635d4115d1292068a0d45505ba05ec [file] [log] [blame]
Davide Pesaventobf1c0692017-01-15 19:15:09 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +00002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2016-2021, Regents of the University of California,
Davide Pesaventocd65c2c2017-01-15 16:10:38 -05004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Weiwei Liu245d7912016-07-28 00:04:25 -07006 *
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 Ghasemi4d36ed52017-10-31 22:26:25 +000025 * @author Chavoosh Ghasemi
schneiderklausd8197df2019-03-16 11:31:40 -070026 * @author Klaus Schneider
Weiwei Liu245d7912016-07-28 00:04:25 -070027 */
28
schneiderklausd8197df2019-03-16 11:31:40 -070029#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_ADAPTIVE_HPP
30#define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_ADAPTIVE_HPP
Weiwei Liu245d7912016-07-28 00:04:25 -070031
Weiwei Liu245d7912016-07-28 00:04:25 -070032#include "pipeline-interests.hpp"
33
Davide Pesavento70576402019-06-07 16:42:21 -040034#include <ndn-cxx/util/rtt-estimator.hpp>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050035#include <ndn-cxx/util/signal.hpp>
Davide Pesavento70576402019-06-07 16:42:21 -040036
Weiwei Liu245d7912016-07-28 00:04:25 -070037#include <queue>
Davide Pesaventoc0702702017-08-24 22:04:00 -040038#include <unordered_map>
Weiwei Liu245d7912016-07-28 00:04:25 -070039
40namespace ndn {
41namespace chunks {
Weiwei Liu245d7912016-07-28 00:04:25 -070042
Davide Pesavento5e3773d2019-08-22 15:35:08 -040043using util::RttEstimatorWithStats;
Davide Pesavento70576402019-06-07 16:42:21 -040044
Weiwei Liu245d7912016-07-28 00:04:25 -070045/**
46 * @brief indicates the state of the segment
47 */
48enum 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 Liu245d7912016-07-28 00:04:25 -070052};
53
54std::ostream&
55operator<<(std::ostream& os, SegmentState state);
56
57/**
58 * @brief Wraps up information that's necessary for segment transmission
59 */
60struct SegmentInfo
61{
Junxiao Shi06d008c2019-02-04 08:26:59 +000062 ScopedPendingInterestHandle interestHdl;
Weiwei Liu245d7912016-07-28 00:04:25 -070063 time::steady_clock::TimePoint timeSent;
Davide Pesaventoba560662019-06-26 22:45:44 -040064 time::nanoseconds rto;
Davide Pesavento958896e2017-01-19 00:52:04 -050065 SegmentState state;
Weiwei Liu245d7912016-07-28 00:04:25 -070066};
67
68/**
69 * @brief Service for retrieving Data via an Interest pipeline
70 *
schneiderklausd8197df2019-03-16 11:31:40 -070071 * Retrieves all segmented Data under the specified prefix by maintaining a dynamic
Weiwei Liu245d7912016-07-28 00:04:25 -070072 * 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 */
schneiderklausd8197df2019-03-16 11:31:40 -070079class PipelineInterestsAdaptive : public PipelineInterests
Weiwei Liu245d7912016-07-28 00:04:25 -070080{
81public:
Weiwei Liu245d7912016-07-28 00:04:25 -070082 /**
Klaus Schneider9e5122b2019-03-19 17:03:25 -070083 * @brief Constructor.
Weiwei Liu245d7912016-07-28 00:04:25 -070084 *
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 Pesavento97a33b22019-10-17 22:10:47 -040088 PipelineInterestsAdaptive(Face& face, RttEstimatorWithStats& rttEstimator, const Options& opts);
Weiwei Liu245d7912016-07-28 00:04:25 -070089
Klaus Schneider9e5122b2019-03-19 17:03:25 -070090 ~PipelineInterestsAdaptive() override;
Weiwei Liu245d7912016-07-28 00:04:25 -070091
92 /**
Klaus Schneider9e5122b2019-03-19 17:03:25 -070093 * @brief Signals when the congestion window changes.
Weiwei Liu245d7912016-07-28 00:04:25 -070094 *
Davide Pesavento70576402019-06-07 16:42:21 -040095 * 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 Liu245d7912016-07-28 00:04:25 -070097 */
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050098 util::Signal<PipelineInterestsAdaptive, time::nanoseconds, double> afterCwndChange;
Weiwei Liu245d7912016-07-28 00:04:25 -070099
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400100 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 */
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500112 util::Signal<PipelineInterestsAdaptive, RttSample> afterRttMeasurement;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400113
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700114protected:
115 DECLARE_SIGNAL_EMIT(afterCwndChange)
116
Davide Pesavento97a33b22019-10-17 22:10:47 -0400117 void
118 printOptions() const;
119
Weiwei Liu245d7912016-07-28 00:04:25 -0700120private:
121 /**
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700122 * @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
133private:
134 /**
135 * @brief Fetch all the segments between 0 and lastSegment of the specified prefix.
Weiwei Liu245d7912016-07-28 00:04:25 -0700136 *
schneiderklausd8197df2019-03-16 11:31:40 -0700137 * 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 Liu245d7912016-07-28 00:04:25 -0700140 */
Davide Pesaventocd65c2c2017-01-15 16:10:38 -0500141 void
Weiwei Liu245d7912016-07-28 00:04:25 -0700142 doRun() final;
143
144 /**
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700145 * @brief Stop all fetch operations.
Weiwei Liu245d7912016-07-28 00:04:25 -0700146 */
Davide Pesaventocd65c2c2017-01-15 16:10:38 -0500147 void
Weiwei Liu245d7912016-07-28 00:04:25 -0700148 doCancel() final;
149
150 /**
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700151 * @brief Check RTO for all sent-but-not-acked segments.
Weiwei Liu245d7912016-07-28 00:04:25 -0700152 */
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
Davide Pesavento958896e2017-01-19 00:52:04 -0500176 recordTimeout();
177
178 void
179 enqueueForRetransmission(uint64_t segNo);
Weiwei Liu245d7912016-07-28 00:04:25 -0700180
181 void
182 handleFail(uint64_t segNo, const std::string& reason);
183
Weiwei Liu245d7912016-07-28 00:04:25 -0700184 void
Davide Pesavento958896e2017-01-19 00:52:04 -0500185 cancelInFlightSegmentsGreaterThan(uint64_t segNo);
Weiwei Liu245d7912016-07-28 00:04:25 -0700186
Chavoosh Ghasemi75309ae2018-03-26 14:46:24 -0400187PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Weiwei Liu245d7912016-07-28 00:04:25 -0700188 void
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000189 printSummary() const final;
Weiwei Liu245d7912016-07-28 00:04:25 -0700190
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700191PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +0000192 static constexpr double MIN_SSTHRESH = 2.0;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700193
194 double m_cwnd; ///< current congestion window size (in segments)
195 double m_ssthresh; ///< current slow start threshold
Klaus Schneider4a2e89d2019-10-27 10:04:39 -0700196 RttEstimatorWithStats& m_rttEstimator;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700197
198PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Weiwei Liu245d7912016-07-28 00:04:25 -0700199 Scheduler m_scheduler;
Davide Pesaventocd65c2c2017-01-15 16:10:38 -0500200 scheduler::ScopedEventId m_checkRtoEvent;
201
Weiwei Liu245d7912016-07-28 00:04:25 -0700202 uint64_t m_highData; ///< the highest segment number of the Data packet the consumer has received so far
203 uint64_t m_highInterest; ///< the highest segment number of the Interests the consumer has sent so far
Davide Pesaventocd65c2c2017-01-15 16:10:38 -0500204 uint64_t m_recPoint; ///< the value of m_highInterest when a packet loss event occurred,
205 ///< it remains fixed until the next packet loss event happens
Weiwei Liu245d7912016-07-28 00:04:25 -0700206
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500207 int64_t m_nInFlight; ///< # of segments in flight
schneiderklaus8ff3abd2019-03-12 22:15:12 -0700208 int64_t m_nLossDecr; ///< # of window decreases caused by packet loss
209 int64_t m_nMarkDecr; ///< # of window decreases caused by congestion marks
210 int64_t m_nTimeouts; ///< # of timed out segments
211 int64_t m_nSkippedRetx; ///< # of segments queued for retransmission but received before the
212 ///< retransmission occurred
213 int64_t m_nRetransmitted; ///< # of retransmitted segments
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +0000214 int64_t m_nCongMarks; ///< # of data packets with congestion mark
Chavoosh Ghasemi75309ae2018-03-26 14:46:24 -0400215 int64_t m_nSent; ///< # of interest packets sent out (including retransmissions)
Weiwei Liu245d7912016-07-28 00:04:25 -0700216
Davide Pesavento958896e2017-01-19 00:52:04 -0500217 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 Liu245d7912016-07-28 00:04:25 -0700220 ///< if the count reaches to the maximum number of
221 ///< timeout/nack retries, the pipeline will be aborted
Davide Pesavento958896e2017-01-19 00:52:04 -0500222 std::queue<uint64_t> m_retxQueue;
223
Weiwei Liu245d7912016-07-28 00:04:25 -0700224 bool m_hasFailure;
225 uint64_t m_failedSegNo;
226 std::string m_failureReason;
227};
228
Weiwei Liu245d7912016-07-28 00:04:25 -0700229} // namespace chunks
230} // namespace ndn
231
schneiderklausd8197df2019-03-16 11:31:40 -0700232#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_ADAPTIVE_HPP