blob: faa7ed669028ed3925ca07da77ac3197a936eecd [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/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2016-2021, 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
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050043#include <boost/program_options/options_description.hpp>
44#include <boost/program_options/parsers.hpp>
45#include <boost/program_options/variables_map.hpp>
46
Andrea Tosatto672b9a72016-01-05 16:18:20 +010047namespace ndn {
48namespace chunks {
49
50static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040051main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052{
53 std::string programName(argv[0]);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054
Davide Pesavento97a33b22019-10-17 22:10:47 -040055 Options options;
56 std::string uri, pipelineType("cubic"), cwndPath, rttPath;
Davide Pesavento0da1f442019-07-26 17:38:13 -040057 time::milliseconds::rep minRto(200), maxRto(60000);
Davide Pesavento97a33b22019-10-17 22:10:47 -040058 double rtoAlpha(0.125), rtoBeta(0.25);
59 int rtoK(8);
Weiwei Liu245d7912016-07-28 00:04:25 -070060
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 namespace po = boost::program_options;
Weiwei Liu245d7912016-07-28 00:04:25 -070062 po::options_description basicDesc("Basic Options");
63 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064 ("help,h", "print this help message and exit")
Klaus Schneider7072e162017-09-16 13:43:00 -070065 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
Davide Pesaventoba560662019-06-26 22:45:44 -040066 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Davide Pesavento296b3852019-10-20 16:00:16 -040067 ("fresh,f", po::bool_switch(&options.mustBeFresh),
68 "only return fresh content (set MustBeFresh on all outgoing Interests)")
Davide Pesavento0da1f442019-07-26 17:38:13 -040069 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
72 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
Davide Pesavento296b3852019-10-20 16:00:16 -040073 ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
74 "skip version discovery, even if the supplied name does not end with a version component")
Davide Pesaventof6991e12018-01-08 20:58:50 -050075 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
76 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077 ("version,V", "print program version and exit")
78 ;
79
Weiwei Liu245d7912016-07-28 00:04:25 -070080 po::options_description fixedPipeDesc("Fixed pipeline options");
81 fixedPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040082 ("pipeline-size,s", po::value<size_t>(&options.maxPipelineSize)->default_value(options.maxPipelineSize),
Weiwei Liu245d7912016-07-28 00:04:25 -070083 "size of the Interest pipeline")
84 ;
85
Klaus Schneider9e5122b2019-03-19 17:03:25 -070086 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070087 adaptivePipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040088 ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
Davide Pesavento296b3852019-10-20 16:00:16 -040089 "do not reduce the window after receiving a congestion mark")
Davide Pesavento97a33b22019-10-17 22:10:47 -040090 ("disable-cwa", po::bool_switch(&options.disableCwa),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070091 "disable Conservative Window Adaptation, i.e., reduce the window on "
92 "each timeout or congestion mark instead of at most once per RTT")
Davide Pesavento97a33b22019-10-17 22:10:47 -040093 ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070094 "after a timeout or congestion mark, reset the window "
95 "to the initial value instead of resetting to ssthresh")
Davide Pesavento97a33b22019-10-17 22:10:47 -040096 ("init-cwnd", po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070097 "initial congestion window in segments")
Davide Pesavento97a33b22019-10-17 22:10:47 -040098 ("init-ssthresh", po::value<double>(&options.initSsthresh),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070099 "initial slow start threshold in segments (defaults to infinity)")
Davide Pesavento97a33b22019-10-17 22:10:47 -0400100 ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
schneiderklausd8197df2019-03-16 11:31:40 -0700101 "additive-increase step")
Davide Pesavento97a33b22019-10-17 22:10:47 -0400102 ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700103 "multiplicative decrease factor (AIMD)")
104 ("rto-alpha", po::value<double>(&rtoAlpha)->default_value(rtoAlpha),
Davide Pesaventoba560662019-06-26 22:45:44 -0400105 "alpha value for RTO calculation")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700106 ("rto-beta", po::value<double>(&rtoBeta)->default_value(rtoBeta),
Davide Pesaventoba560662019-06-26 22:45:44 -0400107 "beta value for RTO calculation")
Davide Pesavento97a33b22019-10-17 22:10:47 -0400108 ("rto-k", po::value<int>(&rtoK)->default_value(rtoK),
Davide Pesaventoba560662019-06-26 22:45:44 -0400109 "k value for RTO calculation")
Davide Pesavento0da1f442019-07-26 17:38:13 -0400110 ("min-rto", po::value<time::milliseconds::rep>(&minRto)->default_value(minRto),
111 "minimum RTO value, in milliseconds")
112 ("max-rto", po::value<time::milliseconds::rep>(&maxRto)->default_value(maxRto),
113 "maximum RTO value, in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700114 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
115 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
116 ;
117
118 po::options_description cubicPipeDesc("CUBIC pipeline options");
119 cubicPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -0400120 ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
121 ("fast-conv", po::bool_switch(&options.enableFastConv), "enable fast convergence")
Weiwei Liu245d7912016-07-28 00:04:25 -0700122 ;
123
124 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700125 visibleDesc.add(basicDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700126 .add(fixedPipeDesc)
127 .add(adaptivePipeDesc)
128 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700129
130 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100131 hiddenDesc.add_options()
132 ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
133
Davide Pesavento6752d942019-07-26 16:26:32 -0400134 po::options_description optDesc;
135 optDesc.add(visibleDesc).add(hiddenDesc);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700136
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100137 po::positional_options_description p;
138 p.add("ndn-name", -1);
139
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 po::variables_map vm;
141 try {
142 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
143 po::notify(vm);
144 }
145 catch (const po::error& e) {
146 std::cerr << "ERROR: " << e.what() << std::endl;
147 return 2;
148 }
149 catch (const boost::bad_any_cast& e) {
150 std::cerr << "ERROR: " << e.what() << std::endl;
151 return 2;
152 }
153
154 if (vm.count("help") > 0) {
155 std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
156 std::cout << visibleDesc;
157 return 0;
158 }
159
160 if (vm.count("version") > 0) {
161 std::cout << "ndncatchunks " << tools::VERSION << std::endl;
162 return 0;
163 }
164
165 if (vm.count("ndn-name") == 0) {
166 std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
167 std::cerr << visibleDesc;
168 return 2;
169 }
170
Davide Pesavento97a33b22019-10-17 22:10:47 -0400171 if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100172 std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
173 return 2;
174 }
175
176 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
177 std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
178 return 2;
179 }
180
Davide Pesavento0da1f442019-07-26 17:38:13 -0400181 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
182 if (options.interestLifetime < 0_ms) {
Klaus Schneider7072e162017-09-16 13:43:00 -0700183 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
184 return 2;
185 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100186
Davide Pesaventof6991e12018-01-08 20:58:50 -0500187 if (options.isQuiet && options.isVerbose) {
188 std::cerr << "ERROR: cannot be quiet and verbose at the same time" << std::endl;
189 return 2;
190 }
191
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100192 try {
193 Face face;
Davide Pesavento97a33b22019-10-17 22:10:47 -0400194 auto discover = make_unique<DiscoverVersion>(face, Name(uri), options);
Weiwei Liue4765012016-06-01 00:10:29 -0700195 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700196 unique_ptr<StatisticsCollector> statsCollector;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400197 unique_ptr<RttEstimatorWithStats> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700198 std::ofstream statsFileCwnd;
199 std::ofstream statsFileRtt;
200
Weiwei Liue4765012016-06-01 00:10:29 -0700201 if (pipelineType == "fixed") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400202 pipeline = make_unique<PipelineInterestsFixed>(face, options);
Weiwei Liue4765012016-06-01 00:10:29 -0700203 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700204 else if (pipelineType == "aimd" || pipelineType == "cubic") {
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400205 auto optionsRttEst = make_shared<RttEstimatorWithStats::Options>();
206 optionsRttEst->alpha = rtoAlpha;
207 optionsRttEst->beta = rtoBeta;
Davide Pesavento97a33b22019-10-17 22:10:47 -0400208 optionsRttEst->k = rtoK;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400209 optionsRttEst->initialRto = 1_s;
210 optionsRttEst->minRto = time::milliseconds(minRto);
211 optionsRttEst->maxRto = time::milliseconds(maxRto);
212 optionsRttEst->rtoBackoffMultiplier = 2;
Davide Pesavento70576402019-06-07 16:42:21 -0400213 if (options.isVerbose) {
Davide Pesaventoba560662019-06-26 22:45:44 -0400214 using namespace ndn::time;
Davide Pesavento70576402019-06-07 16:42:21 -0400215 std::cerr << "RTT estimator parameters:\n"
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400216 << "\tAlpha = " << optionsRttEst->alpha << "\n"
217 << "\tBeta = " << optionsRttEst->beta << "\n"
218 << "\tK = " << optionsRttEst->k << "\n"
219 << "\tInitial RTO = " << duration_cast<milliseconds>(optionsRttEst->initialRto) << "\n"
220 << "\tMin RTO = " << duration_cast<milliseconds>(optionsRttEst->minRto) << "\n"
221 << "\tMax RTO = " << duration_cast<milliseconds>(optionsRttEst->maxRto) << "\n"
222 << "\tBackoff multiplier = " << optionsRttEst->rtoBackoffMultiplier << "\n";
Davide Pesavento70576402019-06-07 16:42:21 -0400223 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400224 rttEstimator = make_unique<RttEstimatorWithStats>(std::move(optionsRttEst));
Davide Pesavento70576402019-06-07 16:42:21 -0400225
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700226 unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
227 if (pipelineType == "aimd") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400228 adaptivePipeline = make_unique<PipelineInterestsAimd>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700229 }
230 else {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400231 adaptivePipeline = make_unique<PipelineInterestsCubic>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700232 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700233
234 if (!cwndPath.empty() || !rttPath.empty()) {
235 if (!cwndPath.empty()) {
236 statsFileCwnd.open(cwndPath);
237 if (statsFileCwnd.fail()) {
238 std::cerr << "ERROR: failed to open " << cwndPath << std::endl;
239 return 4;
240 }
241 }
242 if (!rttPath.empty()) {
243 statsFileRtt.open(rttPath);
244 if (statsFileRtt.fail()) {
245 std::cerr << "ERROR: failed to open " << rttPath << std::endl;
246 return 4;
247 }
248 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400249 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700250 }
251
schneiderklausd8197df2019-03-16 11:31:40 -0700252 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700253 }
Weiwei Liue4765012016-06-01 00:10:29 -0700254 else {
255 std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
256 return 2;
257 }
258
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400259 Consumer consumer(security::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100260 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700261 BOOST_ASSERT(pipeline != nullptr);
262 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700263 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100264 }
265 catch (const Consumer::ApplicationNackError& e) {
266 std::cerr << "ERROR: " << e.what() << std::endl;
267 return 3;
268 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500269 catch (const Consumer::DataValidationError& e) {
270 std::cerr << "ERROR: " << e.what() << std::endl;
271 return 5;
272 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100273 catch (const std::exception& e) {
274 std::cerr << "ERROR: " << e.what() << std::endl;
275 return 1;
276 }
277
278 return 0;
279}
280
281} // namespace chunks
282} // namespace ndn
283
284int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400285main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100286{
287 return ndn::chunks::main(argc, argv);
288}