blob: cfb412454227171f4357317a2ee222b5713a34f2 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider7072e162017-09-16 13:43:00 -07002/*
Alexander Afanasyev28181ee2020-06-03 13:58:47 -04003 * Copyright (c) 2016-2020, Regents of the University of California,
Davide Pesavento92998fe2017-01-18 21:04:52 -05004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
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 Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Weiwei Liue4765012016-06-01 00:10:29 -070026 * @author Davide Pesavento
27 * @author Weiwei Liu
Klaus Schneider7072e162017-09-16 13:43:00 -070028 * @author Klaus Schneider
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +000029 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010030 */
31
Andrea Tosatto672b9a72016-01-05 16:18:20 +010032#include "consumer.hpp"
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070033#include "discover-version.hpp"
Klaus Schneider9e5122b2019-03-19 17:03:25 -070034#include "pipeline-interests-aimd.hpp"
35#include "pipeline-interests-cubic.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070036#include "pipeline-interests-fixed.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070037#include "statistics-collector.hpp"
Junxiao Shif8606492017-07-23 03:44:34 +000038#include "core/version.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039
Weiwei Liu245d7912016-07-28 00:04:25 -070040#include <fstream>
Junxiao Shif8606492017-07-23 03:44:34 +000041#include <ndn-cxx/security/validator-null.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042
43namespace ndn {
44namespace chunks {
45
46static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040047main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048{
49 std::string programName(argv[0]);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010050
Davide Pesavento97a33b22019-10-17 22:10:47 -040051 Options options;
52 std::string uri, pipelineType("cubic"), cwndPath, rttPath;
Davide Pesavento0da1f442019-07-26 17:38:13 -040053 time::milliseconds::rep minRto(200), maxRto(60000);
Davide Pesavento97a33b22019-10-17 22:10:47 -040054 double rtoAlpha(0.125), rtoBeta(0.25);
55 int rtoK(8);
Weiwei Liu245d7912016-07-28 00:04:25 -070056
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 namespace po = boost::program_options;
Weiwei Liu245d7912016-07-28 00:04:25 -070058 po::options_description basicDesc("Basic Options");
59 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 ("help,h", "print this help message and exit")
Klaus Schneider7072e162017-09-16 13:43:00 -070061 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
Davide Pesaventoba560662019-06-26 22:45:44 -040062 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Davide Pesavento296b3852019-10-20 16:00:16 -040063 ("fresh,f", po::bool_switch(&options.mustBeFresh),
64 "only return fresh content (set MustBeFresh on all outgoing Interests)")
Davide Pesavento0da1f442019-07-26 17:38:13 -040065 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
68 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
Davide Pesavento296b3852019-10-20 16:00:16 -040069 ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
70 "skip version discovery, even if the supplied name does not end with a version component")
Davide Pesaventof6991e12018-01-08 20:58:50 -050071 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
72 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 ("version,V", "print program version and exit")
74 ;
75
Weiwei Liu245d7912016-07-28 00:04:25 -070076 po::options_description fixedPipeDesc("Fixed pipeline options");
77 fixedPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040078 ("pipeline-size,s", po::value<size_t>(&options.maxPipelineSize)->default_value(options.maxPipelineSize),
Weiwei Liu245d7912016-07-28 00:04:25 -070079 "size of the Interest pipeline")
80 ;
81
Klaus Schneider9e5122b2019-03-19 17:03:25 -070082 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070083 adaptivePipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040084 ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
Davide Pesavento296b3852019-10-20 16:00:16 -040085 "do not reduce the window after receiving a congestion mark")
Davide Pesavento97a33b22019-10-17 22:10:47 -040086 ("disable-cwa", po::bool_switch(&options.disableCwa),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070087 "disable Conservative Window Adaptation, i.e., reduce the window on "
88 "each timeout or congestion mark instead of at most once per RTT")
Davide Pesavento97a33b22019-10-17 22:10:47 -040089 ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070090 "after a timeout or congestion mark, reset the window "
91 "to the initial value instead of resetting to ssthresh")
Davide Pesavento97a33b22019-10-17 22:10:47 -040092 ("init-cwnd", po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070093 "initial congestion window in segments")
Davide Pesavento97a33b22019-10-17 22:10:47 -040094 ("init-ssthresh", po::value<double>(&options.initSsthresh),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070095 "initial slow start threshold in segments (defaults to infinity)")
Davide Pesavento97a33b22019-10-17 22:10:47 -040096 ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
schneiderklausd8197df2019-03-16 11:31:40 -070097 "additive-increase step")
Davide Pesavento97a33b22019-10-17 22:10:47 -040098 ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070099 "multiplicative decrease factor (AIMD)")
100 ("rto-alpha", po::value<double>(&rtoAlpha)->default_value(rtoAlpha),
Davide Pesaventoba560662019-06-26 22:45:44 -0400101 "alpha value for RTO calculation")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700102 ("rto-beta", po::value<double>(&rtoBeta)->default_value(rtoBeta),
Davide Pesaventoba560662019-06-26 22:45:44 -0400103 "beta value for RTO calculation")
Davide Pesavento97a33b22019-10-17 22:10:47 -0400104 ("rto-k", po::value<int>(&rtoK)->default_value(rtoK),
Davide Pesaventoba560662019-06-26 22:45:44 -0400105 "k value for RTO calculation")
Davide Pesavento0da1f442019-07-26 17:38:13 -0400106 ("min-rto", po::value<time::milliseconds::rep>(&minRto)->default_value(minRto),
107 "minimum RTO value, in milliseconds")
108 ("max-rto", po::value<time::milliseconds::rep>(&maxRto)->default_value(maxRto),
109 "maximum RTO value, in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700110 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
111 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
112 ;
113
114 po::options_description cubicPipeDesc("CUBIC pipeline options");
115 cubicPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -0400116 ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
117 ("fast-conv", po::bool_switch(&options.enableFastConv), "enable fast convergence")
Weiwei Liu245d7912016-07-28 00:04:25 -0700118 ;
119
120 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700121 visibleDesc.add(basicDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700122 .add(fixedPipeDesc)
123 .add(adaptivePipeDesc)
124 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700125
126 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127 hiddenDesc.add_options()
128 ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
129
Davide Pesavento6752d942019-07-26 16:26:32 -0400130 po::options_description optDesc;
131 optDesc.add(visibleDesc).add(hiddenDesc);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700132
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100133 po::positional_options_description p;
134 p.add("ndn-name", -1);
135
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136 po::variables_map vm;
137 try {
138 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
139 po::notify(vm);
140 }
141 catch (const po::error& e) {
142 std::cerr << "ERROR: " << e.what() << std::endl;
143 return 2;
144 }
145 catch (const boost::bad_any_cast& e) {
146 std::cerr << "ERROR: " << e.what() << std::endl;
147 return 2;
148 }
149
150 if (vm.count("help") > 0) {
151 std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
152 std::cout << visibleDesc;
153 return 0;
154 }
155
156 if (vm.count("version") > 0) {
157 std::cout << "ndncatchunks " << tools::VERSION << std::endl;
158 return 0;
159 }
160
161 if (vm.count("ndn-name") == 0) {
162 std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
163 std::cerr << visibleDesc;
164 return 2;
165 }
166
Davide Pesavento97a33b22019-10-17 22:10:47 -0400167 if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168 std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
169 return 2;
170 }
171
172 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
173 std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
174 return 2;
175 }
176
Davide Pesavento0da1f442019-07-26 17:38:13 -0400177 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
178 if (options.interestLifetime < 0_ms) {
Klaus Schneider7072e162017-09-16 13:43:00 -0700179 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
180 return 2;
181 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100182
Davide Pesaventof6991e12018-01-08 20:58:50 -0500183 if (options.isQuiet && options.isVerbose) {
184 std::cerr << "ERROR: cannot be quiet and verbose at the same time" << std::endl;
185 return 2;
186 }
187
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100188 try {
189 Face face;
Davide Pesavento97a33b22019-10-17 22:10:47 -0400190 auto discover = make_unique<DiscoverVersion>(face, Name(uri), options);
Weiwei Liue4765012016-06-01 00:10:29 -0700191 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700192 unique_ptr<StatisticsCollector> statsCollector;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400193 unique_ptr<RttEstimatorWithStats> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700194 std::ofstream statsFileCwnd;
195 std::ofstream statsFileRtt;
196
Weiwei Liue4765012016-06-01 00:10:29 -0700197 if (pipelineType == "fixed") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400198 pipeline = make_unique<PipelineInterestsFixed>(face, options);
Weiwei Liue4765012016-06-01 00:10:29 -0700199 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700200 else if (pipelineType == "aimd" || pipelineType == "cubic") {
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400201 auto optionsRttEst = make_shared<RttEstimatorWithStats::Options>();
202 optionsRttEst->alpha = rtoAlpha;
203 optionsRttEst->beta = rtoBeta;
Davide Pesavento97a33b22019-10-17 22:10:47 -0400204 optionsRttEst->k = rtoK;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400205 optionsRttEst->initialRto = 1_s;
206 optionsRttEst->minRto = time::milliseconds(minRto);
207 optionsRttEst->maxRto = time::milliseconds(maxRto);
208 optionsRttEst->rtoBackoffMultiplier = 2;
Davide Pesavento70576402019-06-07 16:42:21 -0400209 if (options.isVerbose) {
Davide Pesaventoba560662019-06-26 22:45:44 -0400210 using namespace ndn::time;
Davide Pesavento70576402019-06-07 16:42:21 -0400211 std::cerr << "RTT estimator parameters:\n"
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400212 << "\tAlpha = " << optionsRttEst->alpha << "\n"
213 << "\tBeta = " << optionsRttEst->beta << "\n"
214 << "\tK = " << optionsRttEst->k << "\n"
215 << "\tInitial RTO = " << duration_cast<milliseconds>(optionsRttEst->initialRto) << "\n"
216 << "\tMin RTO = " << duration_cast<milliseconds>(optionsRttEst->minRto) << "\n"
217 << "\tMax RTO = " << duration_cast<milliseconds>(optionsRttEst->maxRto) << "\n"
218 << "\tBackoff multiplier = " << optionsRttEst->rtoBackoffMultiplier << "\n";
Davide Pesavento70576402019-06-07 16:42:21 -0400219 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400220 rttEstimator = make_unique<RttEstimatorWithStats>(std::move(optionsRttEst));
Davide Pesavento70576402019-06-07 16:42:21 -0400221
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700222 unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
223 if (pipelineType == "aimd") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400224 adaptivePipeline = make_unique<PipelineInterestsAimd>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700225 }
226 else {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400227 adaptivePipeline = make_unique<PipelineInterestsCubic>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700228 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700229
230 if (!cwndPath.empty() || !rttPath.empty()) {
231 if (!cwndPath.empty()) {
232 statsFileCwnd.open(cwndPath);
233 if (statsFileCwnd.fail()) {
234 std::cerr << "ERROR: failed to open " << cwndPath << std::endl;
235 return 4;
236 }
237 }
238 if (!rttPath.empty()) {
239 statsFileRtt.open(rttPath);
240 if (statsFileRtt.fail()) {
241 std::cerr << "ERROR: failed to open " << rttPath << std::endl;
242 return 4;
243 }
244 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400245 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700246 }
247
schneiderklausd8197df2019-03-16 11:31:40 -0700248 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700249 }
Weiwei Liue4765012016-06-01 00:10:29 -0700250 else {
251 std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
252 return 2;
253 }
254
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400255 Consumer consumer(security::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100256 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700257 BOOST_ASSERT(pipeline != nullptr);
258 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700259 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100260 }
261 catch (const Consumer::ApplicationNackError& e) {
262 std::cerr << "ERROR: " << e.what() << std::endl;
263 return 3;
264 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500265 catch (const Consumer::DataValidationError& e) {
266 std::cerr << "ERROR: " << e.what() << std::endl;
267 return 5;
268 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100269 catch (const std::exception& e) {
270 std::cerr << "ERROR: " << e.what() << std::endl;
271 return 1;
272 }
273
274 return 0;
275}
276
277} // namespace chunks
278} // namespace ndn
279
280int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400281main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100282{
283 return ndn::chunks::main(argc, argv);
284}