blob: 59ea8d829f4f6c2284b96c81ebb2d071b5ae3a38 [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>
Davide Pesaventof8a14d82021-03-12 00:42:03 -050042#include <ndn-cxx/util/rtt-estimator.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050044#include <boost/program_options/options_description.hpp>
45#include <boost/program_options/parsers.hpp>
46#include <boost/program_options/variables_map.hpp>
47
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048namespace ndn {
49namespace chunks {
50
Davide Pesaventof8a14d82021-03-12 00:42:03 -050051namespace po = boost::program_options;
52
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040054main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050056 const std::string programName(argv[0]);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057
Davide Pesavento97a33b22019-10-17 22:10:47 -040058 Options options;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050059 std::string prefix, nameConv, pipelineType("cubic");
60 std::string cwndPath, rttPath;
61 auto rttEstOptions = make_shared<util::RttEstimator::Options>();
62 rttEstOptions->k = 8; // increased from the ndn-cxx default of 4
Weiwei Liu245d7912016-07-28 00:04:25 -070063
Weiwei Liu245d7912016-07-28 00:04:25 -070064 po::options_description basicDesc("Basic Options");
65 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066 ("help,h", "print this help message and exit")
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 Pesaventof8a14d82021-03-12 00:42:03 -050073 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
74 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Davide Pesavento296b3852019-10-20 16:00:16 -040075 ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
Davide Pesaventof8a14d82021-03-12 00:42:03 -050076 "skip version discovery even if the name does not end with a version component")
77 ("naming-convention,N", po::value<std::string>(&nameConv),
78 "encoding convention to use for name components, either 'marker' or 'typed'")
Davide Pesaventof6991e12018-01-08 20:58:50 -050079 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
80 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010081 ("version,V", "print program version and exit")
82 ;
83
Weiwei Liu245d7912016-07-28 00:04:25 -070084 po::options_description fixedPipeDesc("Fixed pipeline options");
85 fixedPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040086 ("pipeline-size,s", po::value<size_t>(&options.maxPipelineSize)->default_value(options.maxPipelineSize),
Weiwei Liu245d7912016-07-28 00:04:25 -070087 "size of the Interest pipeline")
88 ;
89
Klaus Schneider9e5122b2019-03-19 17:03:25 -070090 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070091 adaptivePipeDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050092 ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
93 "do not reduce the window after receiving a congestion mark")
94 ("disable-cwa", po::bool_switch(&options.disableCwa),
95 "disable Conservative Window Adaptation (reduce the window "
96 "on each congestion event instead of at most once per RTT)")
Davide Pesavento97a33b22019-10-17 22:10:47 -040097 ("init-cwnd", po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070098 "initial congestion window in segments")
Davide Pesavento97a33b22019-10-17 22:10:47 -040099 ("init-ssthresh", po::value<double>(&options.initSsthresh),
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700100 "initial slow start threshold in segments (defaults to infinity)")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500101 ("rto-alpha", po::value<double>(&rttEstOptions->alpha)->default_value(rttEstOptions->alpha),
Davide Pesaventoba560662019-06-26 22:45:44 -0400102 "alpha value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500103 ("rto-beta", po::value<double>(&rttEstOptions->beta)->default_value(rttEstOptions->beta),
Davide Pesaventoba560662019-06-26 22:45:44 -0400104 "beta value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500105 ("rto-k", po::value<int>(&rttEstOptions->k)->default_value(rttEstOptions->k),
Davide Pesaventoba560662019-06-26 22:45:44 -0400106 "k value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500107 ("min-rto", po::value<time::milliseconds::rep>()->default_value(
108 time::duration_cast<time::milliseconds>(rttEstOptions->minRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400109 "minimum RTO value, in milliseconds")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500110 ("max-rto", po::value<time::milliseconds::rep>()->default_value(
111 time::duration_cast<time::milliseconds>(rttEstOptions->maxRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400112 "maximum RTO value, in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700113 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
114 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
115 ;
116
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500117 po::options_description aimdPipeDesc("AIMD pipeline options");
118 aimdPipeDesc.add_options()
119 ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
120 "additive increase step")
121 ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
122 "multiplicative decrease factor")
123 ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
124 "after a congestion event, reset the window to the "
125 "initial value instead of resetting to ssthresh")
126 ;
127
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700128 po::options_description cubicPipeDesc("CUBIC pipeline options");
129 cubicPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -0400130 ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
131 ("fast-conv", po::bool_switch(&options.enableFastConv), "enable fast convergence")
Weiwei Liu245d7912016-07-28 00:04:25 -0700132 ;
133
134 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700135 visibleDesc.add(basicDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700136 .add(fixedPipeDesc)
137 .add(adaptivePipeDesc)
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500138 .add(aimdPipeDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700139 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700140
141 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500143 ("name", po::value<std::string>(&prefix), "NDN name of the requested content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100144
Davide Pesavento6752d942019-07-26 16:26:32 -0400145 po::options_description optDesc;
146 optDesc.add(visibleDesc).add(hiddenDesc);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700147
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100148 po::positional_options_description p;
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500149 p.add("name", -1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151 po::variables_map vm;
152 try {
153 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
154 po::notify(vm);
155 }
156 catch (const po::error& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500157 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100158 return 2;
159 }
160 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500161 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100162 return 2;
163 }
164
165 if (vm.count("help") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500166 std::cout << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100167 std::cout << visibleDesc;
168 return 0;
169 }
170
171 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500172 std::cout << "ndncatchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100173 return 0;
174 }
175
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500176 if (prefix.empty()) {
177 std::cerr << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100178 std::cerr << visibleDesc;
179 return 2;
180 }
181
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500182 if (nameConv == "marker" || nameConv == "m" || nameConv == "1") {
183 name::setConventionEncoding(name::Convention::MARKER);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100184 }
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500185 else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") {
186 name::setConventionEncoding(name::Convention::TYPED);
187 }
188 else if (!nameConv.empty()) {
189 std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100190 return 2;
191 }
192
Davide Pesavento0da1f442019-07-26 17:38:13 -0400193 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
194 if (options.interestLifetime < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500195 std::cerr << "ERROR: --lifetime cannot be negative\n";
196 return 2;
197 }
198
199 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
200 std::cerr << "ERROR: --retries must be between -1 and 1024\n";
Klaus Schneider7072e162017-09-16 13:43:00 -0700201 return 2;
202 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100203
Davide Pesaventof6991e12018-01-08 20:58:50 -0500204 if (options.isQuiet && options.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500205 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
206 return 2;
207 }
208
209 if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
210 std::cerr << "ERROR: --pipeline-size must be between 1 and 1024\n";
211 return 2;
212 }
213
214 if (rttEstOptions->k < 0) {
215 std::cerr << "ERROR: --rto-k cannot be negative\n";
216 return 2;
217 }
218
219 rttEstOptions->minRto = time::milliseconds(vm["min-rto"].as<time::milliseconds::rep>());
220 if (rttEstOptions->minRto < 0_ms) {
221 std::cerr << "ERROR: --min-rto cannot be negative\n";
222 return 2;
223 }
224
225 rttEstOptions->maxRto = time::milliseconds(vm["max-rto"].as<time::milliseconds::rep>());
226 if (rttEstOptions->maxRto < rttEstOptions->minRto) {
227 std::cerr << "ERROR: --max-rto cannot be smaller than --min-rto\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500228 return 2;
229 }
230
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100231 try {
232 Face face;
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500233 auto discover = make_unique<DiscoverVersion>(face, Name(prefix), options);
Weiwei Liue4765012016-06-01 00:10:29 -0700234 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700235 unique_ptr<StatisticsCollector> statsCollector;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400236 unique_ptr<RttEstimatorWithStats> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700237 std::ofstream statsFileCwnd;
238 std::ofstream statsFileRtt;
239
Weiwei Liue4765012016-06-01 00:10:29 -0700240 if (pipelineType == "fixed") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400241 pipeline = make_unique<PipelineInterestsFixed>(face, options);
Weiwei Liue4765012016-06-01 00:10:29 -0700242 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700243 else if (pipelineType == "aimd" || pipelineType == "cubic") {
Davide Pesavento70576402019-06-07 16:42:21 -0400244 if (options.isVerbose) {
Davide Pesaventoba560662019-06-26 22:45:44 -0400245 using namespace ndn::time;
Davide Pesavento70576402019-06-07 16:42:21 -0400246 std::cerr << "RTT estimator parameters:\n"
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500247 << "\tAlpha = " << rttEstOptions->alpha << "\n"
248 << "\tBeta = " << rttEstOptions->beta << "\n"
249 << "\tK = " << rttEstOptions->k << "\n"
250 << "\tInitial RTO = " << duration_cast<milliseconds>(rttEstOptions->initialRto) << "\n"
251 << "\tMin RTO = " << duration_cast<milliseconds>(rttEstOptions->minRto) << "\n"
252 << "\tMax RTO = " << duration_cast<milliseconds>(rttEstOptions->maxRto) << "\n"
253 << "\tBackoff multiplier = " << rttEstOptions->rtoBackoffMultiplier << "\n";
Davide Pesavento70576402019-06-07 16:42:21 -0400254 }
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500255 rttEstimator = make_unique<RttEstimatorWithStats>(std::move(rttEstOptions));
Davide Pesavento70576402019-06-07 16:42:21 -0400256
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700257 unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
258 if (pipelineType == "aimd") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400259 adaptivePipeline = make_unique<PipelineInterestsAimd>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700260 }
261 else {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400262 adaptivePipeline = make_unique<PipelineInterestsCubic>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700263 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700264
265 if (!cwndPath.empty() || !rttPath.empty()) {
266 if (!cwndPath.empty()) {
267 statsFileCwnd.open(cwndPath);
268 if (statsFileCwnd.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500269 std::cerr << "ERROR: failed to open '" << cwndPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700270 return 4;
271 }
272 }
273 if (!rttPath.empty()) {
274 statsFileRtt.open(rttPath);
275 if (statsFileRtt.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500276 std::cerr << "ERROR: failed to open '" << rttPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700277 return 4;
278 }
279 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400280 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700281 }
282
schneiderklausd8197df2019-03-16 11:31:40 -0700283 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700284 }
Weiwei Liue4765012016-06-01 00:10:29 -0700285 else {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500286 std::cerr << "ERROR: '" << pipelineType << "' is not a valid pipeline type\n";
Weiwei Liue4765012016-06-01 00:10:29 -0700287 return 2;
288 }
289
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400290 Consumer consumer(security::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100291 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700292 BOOST_ASSERT(pipeline != nullptr);
293 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700294 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100295 }
296 catch (const Consumer::ApplicationNackError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500297 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100298 return 3;
299 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500300 catch (const Consumer::DataValidationError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500301 std::cerr << "ERROR: " << e.what() << "\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500302 return 5;
303 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100304 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500305 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100306 return 1;
307 }
308
309 return 0;
310}
311
312} // namespace chunks
313} // namespace ndn
314
315int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400316main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100317{
318 return ndn::chunks::main(argc, argv);
319}